// SampleGraphics.java // To use this, first do a search-and-replace of "SampleGraphics" with // your class name, say MyGraphics (if the file is MyGraphics.java). import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class SampleGraphics 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 = "SampleGraphics"; 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 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); } //////////////////////////////////////////// // 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) { SampleGraphics component = new SampleGraphics(); 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(); } }