// GraphicsHelperMethodDemo.java import java.awt.*; import javax.swing.*; class MyGraphics extends JComponent { // Here is a helper method that takes the page, and paints // a blue background in it. public void paintBlueBackground(Graphics page) { int width = getWidth(); int height = getHeight(); page.setColor(Color.blue); page.fillRect(0, 0, width, height); } // This helper method takes a page, an x,y position, and a radius, // and fills a circle with that radius at that location. public void fillCircle(Graphics page, int x, int y, int radius) { page.fillOval(x-radius, y-radius, 2*radius, 2*radius); } // This helper method takes a page, an x,y position, and a radius, // and paints a smiley face with that radius at that location. // It uses page.drawArc, which you are not yet responsible for. public void paintSmileyFace(Graphics page, int x, int y, int radius) { page.setColor(Color.yellow); fillCircle(page, x, y, radius); page.setColor(Color.black); fillCircle(page, x-radius/4, y-radius/4, radius/6); // left eye fillCircle(page, x+radius/4, y-radius/4, radius/6); // right eye setLineThickness(page, 3); page.drawArc(x-radius/3, y, radius*2/3, radius/2, 180, 180); } // This helper method sets the line thickness. You are not responsible // for how it works! public void setLineThickness(Graphics page, int thickness) { if (thickness < 0) thickness = 0; ((Graphics2D)page).setStroke(new BasicStroke(thickness)); } public void paint(Graphics page) { paintBlueBackground(page); page.setColor(Color.yellow); fillCircle(page, 50, 50, 10); page.setColor(Color.green); fillCircle(page, 100, 100, 30); paintSmileyFace(page, 300, 100, 50); paintSmileyFace(page, 300, 300, 100); } ////////////////////////////////////////////// /// END OF YOUR CODE /// /// (you may ignore all the code below!!! /// ////////////////////////////////////////////// public Dimension getPreferredSize() { int initialWidth = 500; int initialHeight = 400; return new Dimension(initialWidth, initialHeight); } public static void main(String[] args) { JComponent jc = newInstance(); JFrame frame = new JFrame(jc.getClass().getName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(jc); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); } // Returns an instance of this class as a JComponent. This is necessary so // students can rename this class without changing the "main" method's body. public static JComponent newInstance() { StackTraceElement[] trace = null; try { throw new RuntimeException(); } catch (Exception e) { trace = e.getStackTrace(); } try { return (JComponent)Class.forName(trace[0].getClassName()).newInstance(); } catch (Exception e) { return null; } } }