// MoreGraphics.java import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class MoreGraphics extends JComponent { //////////////////////////////////////////// // Class and instance variables //////////////////////////////////////////// // array of colors: access with colors.length and colors[i] public static final Color[] colors = { Color.yellow, Color.black, Color.green, Color.blue, Color.orange, Color.magenta, Color.gray }; public static final String appName = "MoreGraphics"; public static final int WIN_WIDTH = 400; public static final int WIN_HEIGHT = 400; public static final int TIMER_DELAY = 1000; // milliseconds //////////////////////////////////////////// // Paint //////////////////////////////////////////// public void paintMoreGraphics(Graphics page) { // draw a medium yellow rectangle page.setColor(new Color(220,220,0)); page.fillRect(50,50,40,40); // draw an overlapping semi-transparent red oval page.setColor(new Color(255,0,0,127)); page.fillOval(70,70,40,40); // draw a string in a specific font page.setColor(Color.black); page.setFont(new Font("Arial",Font.BOLD,36)); page.drawString("Carpe diem",100,100); // draw a thick horizontal line (45 pixels wide!) Graphics2D page2d = (Graphics2D) page; page2d.setStroke(new BasicStroke(4.5f)); page.drawLine(50,50,200,50); // draw a thick, dashed, diagonal polygon outline with nice end caps page2d.setStroke(new BasicStroke(2.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f, new float[]{5f}, 0f)); Polygon p = new Polygon(); p.addPoint(30,30); p.addPoint(100,200); p.addPoint(100,30); page.drawPolygon(p); } public void paintComponent(Graphics page) { // get the dimensions of the draw area int width = this.getWidth(); int height = this.getHeight(); // clear the background page.setColor(Color.white); page.fillRect(0,0,width,height); // and show off some more painting tricks! paintMoreGraphics(page); } //////////////////////////////////////////// // Event Handlers //////////////////////////////////////////// public void onMousePressed(int x, int y) { System.out.println("mousePressed at " + x + "," + y); } public void onMouseDragged(int x, int y) { System.out.println("mouseDragged at " + x + "," + y); } public void onMouseReleased(int x, int y) { System.out.println("mouseReleased at " + x + "," + y); } public void onKeyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); System.out.println("keyPressed, code = " + keyCode + ", char = '" + keyChar + "'"); } public void onTimer() { // System.out.println("onTimer called"); } //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// // DO NOT MODIFY CODE BELOW HERE! //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////// // addEventListeners //////////////////////////////////////////// public void addEventListeners() { // add actions in response to mouse events addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { onMousePressed(e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { onMouseReleased(e.getX(),e.getY()); } }); // and for mouse motion events addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { onMouseDragged(e.getX(),e.getY()); } }); // and for keyboard events addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { onKeyPressed(e); } }); // and for timer events int timerMilliseconds = TIMER_DELAY; // 1000ms = 1 second new Timer(timerMilliseconds, new ActionListener() { public void actionPerformed(ActionEvent evt) { onTimer(); } }).start(); } //////////////////////////////////////////// // Main //////////////////////////////////////////// public static void main(String[] args) { MoreGraphics component = new MoreGraphics(); component.addEventListeners(); final JFrame frame = new JFrame(appName); frame.getContentPane().add(component); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); component.requestFocusInWindow(); // send key events to "component" frame.setSize(WIN_WIDTH,WIN_HEIGHT); frame.show(); } }