/* Stuart Reges and Marty Stepp 07/01/2005 The DrawingPanel class provides a simple interface for drawing persistent images using a Graphics object. An internal BufferedImage object is used to keep track of what has been drawn. A client of the class simply constructs a DrawingPanel of a particular size and then draws on it with the Graphics object, setting the background color if they so choose. To ensure that the image is always displayed, a timer calls repaint at regular intervals. */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { private JFrame frame; // overall window frame private JPanel panel; // overall drawing surface private BufferedImage image; // remembers drawing commands private Graphics2D g2; // graphics context for painting private JLabel statusBar; // status bar showing mouse position public static final int DELAY = 250; // delay between repaints in millis // construct a drawing panel of given width and height enclosed in a window public DrawingPanel(int width, int height) { image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); statusBar = new JLabel(" "); statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK)); panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); panel.setBackground(Color.WHITE); panel.setPreferredSize(new Dimension(width, height)); panel.add(new JLabel(new ImageIcon(image))); // listen to mouse movement MouseInputAdapter listener = new MouseInputAdapter() { public void mouseMoved(MouseEvent e) { statusBar.setText("(" + e.getX() + ", " + e.getY() + ")"); } public void mouseExited(MouseEvent e) { statusBar.setText(" "); } }; panel.addMouseListener(listener); panel.addMouseMotionListener(listener); g2 = (Graphics2D)image.getGraphics(); g2.setColor(Color.BLACK); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(new BasicStroke(1.1f)); frame = new JFrame("CSE 142 Drawing Panel"); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(panel); frame.getContentPane().add(statusBar, "South"); frame.pack(); frame.setVisible(true); new Timer(DELAY, this).start(); } // obtain the Graphics object to draw on the panel public Graphics2D getGraphics() { return g2; } // makes drawing panel become the frontmost window on the screen public void toFront() { frame.toFront(); } // set the background color of the drawing panel public void setBackground(Color c) { panel.setBackground(c); } // makes the program pause for the given amount of time, // allowing for animation public void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) {} } // used for an internal timer that keeps repainting public void actionPerformed(ActionEvent e) { panel.repaint(); } }