public static void main(String[] args) { Canvas canvas = new Canvas(); canvas.setTitle("canvas sample"); canvas.setSize(600, 200); canvas.setLocationRelativeTo(null); canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CanvasData dd = new CanvasData(300); NewPanel p = new NewPanel(dd); canvas.add(p); canvas.setVisible(true); }
private Image loadImage( Image img, double x, double y, double width, double height, double alpha, boolean draw) { if (width != 0) img.setWidth(width); if (height != 0) img.setHeight(height); switch (imageMode) { case CORNER: double w = img.getWidth(); double h = img.getHeight(); img.setX(x + w / 2); img.setY(y + h / 2); break; case CENTER: img.setX(x); img.setY(y); } // todo: differentiate between newly constructed objects and copies. img.setTransformDelegate(new ContextTransformDelegate(this)); inheritFromContext(img); if (alpha != 1.0) img.setAlpha(alpha); if (draw) canvas.add(img); return img; }
private static boolean testCanvas() { boolean pass = true; int test = 1; double area; Canvas canvas = new Canvas(); Circle[] circles = new Circle[3]; Rectangle[] rects = new Rectangle[3]; Triangle[] tris = new Triangle[3]; ConvexPolygon[] polys = new ConvexPolygon[3]; ArrayList<Circle> circleList; ArrayList<Rectangle> rectList; ArrayList<Triangle> triList; ArrayList<ConvexPolygon> polyList; ArrayList<Shape> shapeList; circles[0] = new Circle(1.1, new Point(1, 2), Color.cyan, false); circles[1] = new Circle(2.2, new Point(2, 3), Color.red, false); circles[2] = new Circle(3.3, new Point(3, 4), Color.black, false); rects[0] = new Rectangle(1.1, 1.11, new Point(1, 2), Color.cyan, false); rects[1] = new Rectangle(2.2, 2.22, new Point(2, 3), Color.red, false); rects[2] = new Rectangle(3.3, 3.33, new Point(3, 4), Color.black, false); Point a = new Point(1, 1); Point b = new Point(0, 2); Point c = new Point(0, 0); Point aa = new Point(2, 2); Point bb = new Point(1, 3); Point cc = new Point(1, 1); Point aaa = new Point(3, 3); Point bbb = new Point(2, 4); Point ccc = new Point(2, 2); tris[0] = new Triangle(a, b, c, Color.cyan, false); tris[1] = new Triangle(aa, bb, cc, Color.red, false); tris[2] = new Triangle(aaa, bbb, ccc, Color.black, false); Point[] aVertices = new Point[5]; Point[] bVertices = new Point[5]; Point[] cVertices = new Point[5]; aVertices[0] = new Point(4, 0); aVertices[1] = new Point(2, 2); aVertices[2] = new Point(-2, -2); aVertices[3] = new Point(-4, 0); aVertices[4] = new Point(0, -2); bVertices[0] = new Point(4, 1); bVertices[1] = new Point(2, 3); bVertices[2] = new Point(-2, -1); bVertices[3] = new Point(-4, 1); bVertices[4] = new Point(0, -1); cVertices[0] = new Point(4, -1); cVertices[1] = new Point(2, 1); cVertices[2] = new Point(-2, -3); cVertices[3] = new Point(-4, -1); cVertices[4] = new Point(0, -3); polys[0] = new ConvexPolygon(aVertices, Color.cyan, false); polys[1] = new ConvexPolygon(bVertices, Color.red, false); polys[2] = new ConvexPolygon(cVertices, Color.black, false); System.out.println("Canvas tests..."); // Test an empty Canvas... pass &= test(canvas.size() == 0, test++); pass &= test(canvas.getCircles().size() == 0, test++); pass &= test(canvas.getRectangles().size() == 0, test++); pass &= test(canvas.getTriangles().size() == 0, test++); pass &= test(canvas.getConvexPolygons().size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 0, test++); pass &= test(canvas.getAreaOfAllShapes() == 0, test++); // Add a shape and test a Canvas with one shape in it... canvas.add(circles[0]); pass &= test(canvas.size() == 1, test++); pass &= test(canvas.getCircles().size() == 1, test++); pass &= test(canvas.getRectangles().size() == 0, test++); pass &= test(canvas.getTriangles().size() == 0, test++); pass &= test(canvas.getConvexPolygons().size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 1, test++); pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++); pass &= test(canvas.getShapesByColor(Color.cyan).get(0).equals(circles[0]), test++); pass &= test(canvas.get(0).equals(circles[0]), test++); pass &= test(approx(canvas.getAreaOfAllShapes(), circles[0].getArea(), 0.000001), test++); // Remove a shape and test an empty Canvas... pass &= test(canvas.remove(0).equals(circles[0]), test++); pass &= test(canvas.size() == 0, test++); pass &= test(canvas.getCircles().size() == 0, test++); pass &= test(canvas.getRectangles().size() == 0, test++); pass &= test(canvas.getTriangles().size() == 0, test++); pass &= test(canvas.getConvexPolygons().size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 0, test++); pass &= test(canvas.getAreaOfAllShapes() == 0, test++); // Add one of each shape and test... canvas.add(circles[0]); canvas.add(rects[0]); canvas.add(tris[0]); canvas.add(polys[0]); pass &= test(canvas.size() == 4, test++); pass &= test(canvas.getCircles().size() == 1, test++); pass &= test(canvas.getRectangles().size() == 1, test++); pass &= test(canvas.getTriangles().size() == 1, test++); pass &= test(canvas.getConvexPolygons().size() == 1, test++); pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 4, test++); pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++); pass &= test(canvas.getRectangles().get(0).equals(rects[0]), test++); pass &= test(canvas.getTriangles().get(0).equals(tris[0]), test++); pass &= test(canvas.getConvexPolygons().get(0).equals(polys[0]), test++); area = circles[0].getArea(); area += rects[0].getArea(); area += tris[0].getArea(); area += polys[0].getArea(); pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++); // Remove a shape and test again... canvas.remove(2); pass &= test(canvas.size() == 3, test++); pass &= test(canvas.getCircles().size() == 1, test++); pass &= test(canvas.getRectangles().size() == 1, test++); pass &= test(canvas.getTriangles().size() == 0, test++); pass &= test(canvas.getConvexPolygons().size() == 1, test++); pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 3, test++); pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++); pass &= test(canvas.getRectangles().get(0).equals(rects[0]), test++); pass &= test(canvas.getConvexPolygons().get(0).equals(polys[0]), test++); area -= tris[0].getArea(); pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++); // Add more shapes and test again... canvas.add(tris[0]); area += tris[0].getArea(); for (int i = 1; i < 3; i++) { canvas.add(circles[i]); canvas.add(rects[i]); canvas.add(tris[i]); canvas.add(polys[i]); area += circles[i].getArea(); area += rects[i].getArea(); area += tris[i].getArea(); area += polys[i].getArea(); } pass &= test(canvas.size() == 12, test++); pass &= test(canvas.getCircles().size() == 3, test++); pass &= test(canvas.getRectangles().size() == 3, test++); pass &= test(canvas.getTriangles().size() == 3, test++); pass &= test(canvas.getConvexPolygons().size() == 3, test++); pass &= test(canvas.getShapesByColor(Color.black).size() == 4, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 4, test++); pass &= test(canvas.getShapesByColor(Color.white).size() == 0, test++); pass &= test(canvas.get(8).equals(circles[2]), test++); pass &= test(canvas.get(9).equals(rects[2]), test++); pass &= test(canvas.get(10).equals(tris[2]), test++); pass &= test(canvas.get(11).equals(polys[2]), test++); pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++); // Remove a couple of shapes and test again... pass &= test(canvas.remove(0).equals(circles[0]), test++); pass &= test(canvas.remove(10).equals(polys[2]), test++); pass &= test(canvas.remove(7).equals(circles[2]), test++); pass &= test(canvas.size() == 9, test++); pass &= test(canvas.getCircles().size() == 1, test++); pass &= test(canvas.getRectangles().size() == 3, test++); pass &= test(canvas.getTriangles().size() == 3, test++); pass &= test(canvas.getConvexPolygons().size() == 2, test++); pass &= test(canvas.getShapesByColor(Color.black).size() == 2, test++); pass &= test(canvas.getShapesByColor(Color.cyan).size() == 3, test++); pass &= test(canvas.getShapesByColor(Color.white).size() == 0, test++); area -= circles[0].getArea(); area -= polys[2].getArea(); area -= circles[2].getArea(); pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++); return pass; }
protected void addImage(Image i) { canvas.add(i); }
@Override protected void addText(Text t) { canvas.add(t); }
@Override protected void addPath(Path p) { canvas.add(p); }
/** * The draw method doesn't actually draw anything, but rather appends grobs to the canvas. When * the canvas gets drawn, this grob will be drawn also. * * @param grob the grob to append to the canvas */ public void draw(Grob grob) { canvas.add(grob); }