gann-cdf /
graphics
| 1 | package example; |
||
| 2 | |||
| 3 | import org.gannacademy.cdf.graphics.Drawable; |
||
| 4 | import org.gannacademy.cdf.graphics.Image; |
||
| 5 | import org.gannacademy.cdf.graphics.Text; |
||
| 6 | import org.gannacademy.cdf.graphics.geom.Rectangle; |
||
| 7 | import org.gannacademy.cdf.graphics.geom.*; |
||
| 8 | import org.gannacademy.cdf.graphics.ui.AppWindow; |
||
| 9 | |||
| 10 | import java.awt.*; |
||
| 11 | import java.util.ArrayList; |
||
| 12 | import java.util.List; |
||
| 13 | |||
| 14 | public class GeometryDemo extends AppWindow { |
||
| 15 | |||
| 16 | public static final double COL_SPACE = 30, COL_WIDTH = 160, ROW_SPACE = 10, ROW_HEIGHT = 120, SPEED = 3; |
||
| 17 | public static final long DELAY = 5000; |
||
| 18 | private List<Drawable> components; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 19 | private List<Double> headings; |
||
| 20 | private long start; |
||
| 21 | private double scale; |
||
| 22 | private boolean scaleIsIncreasing; |
||
| 23 | |||
| 24 | public static void main(String[] args) { |
||
| 25 | new GeometryDemo(); |
||
| 26 | } |
||
| 27 | |||
| 28 | private int columns() { |
||
| 29 | return (int) ((getDrawingPanel().getWidth() - COL_SPACE) / (COL_SPACE + COL_WIDTH)); |
||
| 30 | } |
||
| 31 | |||
| 32 | private double xCoord(int i) { |
||
| 33 | int col = i % columns(); |
||
| 34 | return COL_SPACE + col * (COL_WIDTH + COL_SPACE); |
||
| 35 | } |
||
| 36 | |||
| 37 | private double yCoord(int i) { |
||
| 38 | int row = i / columns(); |
||
| 39 | return ROW_SPACE + row * (ROW_HEIGHT + ROW_SPACE); |
||
| 40 | } |
||
| 41 | |||
| 42 | private Color randomColor() { |
||
| 43 | return new Color((int) (Math.random() * 256.0), (int) (Math.random() * 256.0), (int) (Math.random() * 256.0)); |
||
|
0 ignored issues
–
show
|
|||
| 44 | } |
||
| 45 | |||
| 46 | private Color adjustColor(Color color, int adjustment) { |
||
| 47 | return new Color( |
||
| 48 | Math.max(0, Math.min(255, color.getRed() + adjustment)), |
||
| 49 | Math.max(0, Math.min(255, color.getGreen() + adjustment)), |
||
| 50 | Math.max(0, Math.min(255, color.getBlue() + adjustment)) |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | @Override |
||
| 55 | protected void setup() { |
||
| 56 | Class[] types = { |
||
| 57 | Rectangle.class, |
||
| 58 | Ellipse.class, |
||
| 59 | RoundRectangle.class, |
||
| 60 | Arc.class, |
||
| 61 | Line.class, |
||
| 62 | CubicCurve.class, |
||
| 63 | QuadCurve.class, |
||
| 64 | Path.class, |
||
| 65 | Text.class, |
||
| 66 | Image.class |
||
| 67 | }; |
||
| 68 | components = new ArrayList<>(); |
||
| 69 | headings = new ArrayList<>(); |
||
| 70 | |||
| 71 | scaleIsIncreasing = true; |
||
| 72 | scale = 1.0; |
||
| 73 | |||
| 74 | double rows = (int) (Math.sqrt(types.length)); |
||
| 75 | int cols = (int) (Math.ceil((double) types.length / rows)); |
||
| 76 | setSize((int) (COL_SPACE + cols * (COL_WIDTH + COL_SPACE)), (int) (ROW_SPACE + rows * (ROW_HEIGHT + ROW_SPACE))); |
||
| 77 | |||
| 78 | for (int i = 0; i < types.length; i++) { |
||
| 79 | headings.add(Math.random() * 360.0); |
||
| 80 | if (types[i] == Ellipse.class) { |
||
| 81 | components.add(new Ellipse(xCoord(i), yCoord(i), COL_WIDTH, ROW_HEIGHT, getDrawingPanel())); |
||
| 82 | } else if (types[i] == RoundRectangle.class) { |
||
| 83 | components.add(new RoundRectangle(xCoord(i), yCoord(i), COL_WIDTH, ROW_HEIGHT, COL_SPACE, ROW_SPACE, getDrawingPanel())); |
||
| 84 | } else if (types[i] == Arc.class) { |
||
| 85 | components.add(new Arc(xCoord(i), yCoord(i), COL_WIDTH, ROW_HEIGHT, Math.random() * 360, Math.random() * 360, getDrawingPanel())); |
||
| 86 | } else if (types[i] == Line.class) { |
||
| 87 | components.add(new Line(xCoord(i), yCoord(i), xCoord(i) + COL_WIDTH, yCoord(i) + ROW_HEIGHT, getDrawingPanel())); |
||
| 88 | } else if (types[i] == CubicCurve.class) { |
||
| 89 | components.add(new CubicCurve(xCoord(i), yCoord(i), xCoord(i), yCoord(i) + ROW_HEIGHT, xCoord(i) + COL_WIDTH, yCoord(i), xCoord(i) + COL_WIDTH, yCoord(i) + ROW_HEIGHT, getDrawingPanel())); |
||
| 90 | } else if (types[i] == QuadCurve.class) { |
||
| 91 | components.add(new QuadCurve(xCoord(i), yCoord(i), xCoord(i) + COL_WIDTH, yCoord(i), xCoord(i) + COL_WIDTH, yCoord(i) + ROW_HEIGHT, getDrawingPanel())); |
||
| 92 | } else if (types[i] == Path.class) { |
||
| 93 | Path path = new Path(getDrawingPanel()); |
||
| 94 | path.moveTo(xCoord(i), yCoord(i)); |
||
| 95 | path.lineTo(xCoord(i) + COL_WIDTH, yCoord(i) + ROW_HEIGHT); |
||
| 96 | path.curveTo(xCoord(i) + COL_WIDTH, yCoord(i), xCoord(i), yCoord(i), xCoord(i), yCoord(i) + ROW_HEIGHT); |
||
| 97 | path.quadTo(xCoord(i) + COL_WIDTH, yCoord(i) + ROW_HEIGHT, xCoord(i) + COL_WIDTH, yCoord(i)); |
||
| 98 | components.add(path); |
||
| 99 | } else if (types[i] == Text.class) { |
||
| 100 | Text text = new Text(" Gann Graphics ", xCoord(i), yCoord(i), getDrawingPanel()); |
||
| 101 | text.setWidth(COL_WIDTH); |
||
| 102 | text.translate(0, text.getMaxAscent()); |
||
| 103 | components.add(text); |
||
| 104 | } else if (types[i] == Image.class) { |
||
| 105 | Image image = new Image("/java-logo.png", xCoord(i), yCoord(i), getDrawingPanel()); |
||
| 106 | image.setWidth(COL_WIDTH); |
||
| 107 | image.setHeight(ROW_HEIGHT); |
||
| 108 | components.add(image); |
||
| 109 | } else { |
||
| 110 | components.add(new Rectangle(xCoord(i), yCoord(i), COL_WIDTH, ROW_HEIGHT, getDrawingPanel())); |
||
| 111 | } |
||
| 112 | components.get(i).setStrokeColor(randomColor()); |
||
| 113 | components.get(i).setFillColor(adjustColor(components.get(i).getStrokeColor(), 50)); |
||
| 114 | components.get(i).setStroke(new BasicStroke((float) (Math.random() * Math.min(COL_SPACE, ROW_SPACE)))); |
||
| 115 | } |
||
| 116 | start = System.currentTimeMillis(); |
||
| 117 | } |
||
| 118 | |||
| 119 | public void loop() { |
||
| 120 | if (System.currentTimeMillis() > start + DELAY) { |
||
| 121 | if (scaleIsIncreasing) { |
||
| 122 | if (scale < 1.5) { |
||
| 123 | scale += 0.1; |
||
| 124 | } else { |
||
| 125 | scaleIsIncreasing = false; |
||
| 126 | } |
||
| 127 | } else { |
||
| 128 | if (scale > 0.5) { |
||
| 129 | scale -= 0.1; |
||
| 130 | } else { |
||
| 131 | scaleIsIncreasing = true; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | for (int i = 0; i < components.size(); i++) { |
||
| 136 | components.get(i).translate(Math.cos(headings.get(i)) * SPEED, Math.sin(headings.get(i)) * SPEED); |
||
| 137 | components.get(i).setWidth(COL_WIDTH * scale); |
||
| 138 | if (!(components.get(i) instanceof Text)) { |
||
| 139 | components.get(i).setHeight(ROW_HEIGHT * scale); |
||
| 140 | } |
||
| 141 | if (components.get(i).getX() < 0 - COL_WIDTH) { |
||
| 142 | components.get(i).setLocation(getDrawingPanel().getWidth(), components.get(i).getY()); |
||
| 143 | } else if (components.get(i).getX() > getDrawingPanel().getWidth()) { |
||
| 144 | components.get(i).setLocation(0 - COL_WIDTH, components.get(i).getY()); |
||
| 145 | } |
||
| 146 | if (components.get(i).getY() < 0 - ROW_HEIGHT) { |
||
| 147 | components.get(i).setLocation(components.get(i).getX(), getDrawingPanel().getHeight()); |
||
| 148 | } else if (components.get(i).getY() > getDrawingPanel().getHeight()) { |
||
| 149 | components.get(i).setLocation(components.get(i).getX(), 0 - ROW_HEIGHT); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | sleep(100); |
||
| 154 | } |
||
| 155 | } |
||
| 156 |