public void paint(Graphics g) { Graphics2D g_2d = (Graphics2D) g; Ellipse2D ellipse = new Ellipse2D.Double(0, 2, 80, 80); Rectangle2D rect = new Rectangle2D.Double(40, 2, 80, 80); Area a1 = new Area(ellipse); Area a2 = new Area(rect); a1.intersect(a2); // "Óë" g_2d.fill(a1); ellipse.setFrame(130, 2, 80, 80); rect.setFrame(170, 2, 80, 80); a1 = new Area(ellipse); a2 = new Area(rect); a1.add(a2); // "»ò" g_2d.draw(a1); ellipse.setFrame(0, 90, 80, 80); rect.setFrame(40, 90, 80, 80); a1 = new Area(ellipse); a2 = new Area(rect); a1.subtract(a2); // "²î" g_2d.draw(a1); ellipse.setFrame(130, 90, 80, 80); rect.setFrame(170, 90, 80, 80); a1 = new Area(ellipse); a2 = new Area(rect); a1.exclusiveOr(a2); // "Òì»ò" g_2d.fill(a1); }
/** the paintComponent -- paints the board every repaint() call. */ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; for (int i = 0; i < B_WIDTH; i++) { for (int j = 0; j < B_HEIGHT; j++) { Rectangle2D rect = new Rectangle2D.Double((i * 40) + 1, (j * 40) + 1, 38, 38); g2.setPaint(balls[i][j].getColor()); g2.fill(rect); Ellipse2D circle = new Ellipse2D.Double(); circle.setFrame(rect); g2.setPaint(balls[i][j].getcColor()); g2.fill(circle); } } }
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // draw a rectangle double leftX = 100; double topY = 100; double width = 200; double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.setPaint(Color.BLACK); g2.draw(rect); g2.setPaint(Color.RED); g2.fill(rect); // Note that the right and bottom edge are not painted // over // draw the enclosed ellipse Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.setPaint(new Color(0, 128, 128)); // a dull blue-green g2.fill(ellipse); }