// Zorah Fung, CSE 142 // Some code to show an example of using DrawingPanel and Graphics import java.awt.*; // Abstract Windowing Toolkit // must load awt package to use graphics and Color public class GraphicsDemo { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(500, 300); Graphics g = panel.getGraphics(); // x, y, width, height g.drawRect(10, 10, 90, 90); g.setColor(Color.MAGENTA); // x, y, width, height g.fillOval(10, 10, 90, 90); g.setColor(Color.GREEN); g.drawOval(10, 10, 90, 90); g.setColor(Color.BLACK); // x1, y1, x2, y2 g.drawLine(10, 10, 100, 100); g.setColor(Color.RED); // For graphics, we can start our loops at 0, so that out starting values are // clear and easy to read // Here, our ovals start being drawn at x = 200 and y = 10 for (int i = 0; i < 3; i++) { g.fillOval(20 * i + 200, 20 * i + 10, 20, 20); } /* These method calls are redundant! Because we have a repetitive, predictable pattern, we can use a for loop to reduce the redundancy g.fillOval(200, 10, 20, 20); g.fillOval(220, 30, 20, 20); g.fillOval(240, 50, 20, 20); */ } }