 |
|

|
 |

09/15/09 - Drawing Panel Class
|
 |
 |
 |
  | // A simple interface for drawing persistent images. // Final version with events, pp. 773-775, Building Java Programs, Reges/Stepp.
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*;
public class DrawingPanel extends MouseInputAdapter implements ActionListener { private JFrame frame; // overall window frame private JPanel panel; // drawing surface private JLabel statusBar; // status bar private Graphics g; // drawing pen
// constructs a drawing panel of given size public DrawingPanel(int width, int height) { // set up the empty image onto which we will draw BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); g = image.getGraphics(); g.setColor(Color.BLACK); // enclose the image in a label inside a panel JLabel label = new JLabel(); label.setIcon(new ImageIcon(image)); panel = new JPanel(new FlowLayout()); panel.setBackground(Color.WHITE); panel.setPreferredSize(new Dimension(width, height)); panel.add(label); // the status bar that shows the mouse position statusBar = new JLabel(" "); // attach listener to observe mouse movement panel.addMouseListener(this); panel.addMouseMotionListener(this); // set up the JFrame frame = new JFrame( "Drawing Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setLayout(new BorderLayout()); frame.add(panel, BorderLayout.CENTER); frame.add(statusBar, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); // start a repaint timer to refresh the screen Timer timer = new Timer(250, this); timer.start(); } // obtains the Graphics object to draw on the panel public Graphics getGraphics() { return g; } // sets the background color of the drawing panel public void setBackground(Color c) { panel.setBackground(c); } // shows or hides the drawing panel on the screen public void setVisible(boolean visible) { frame.setVisible(visible); } // used for timer that repeatedly repaints screen public void actionPerformed(ActionEvent e) { panel.repaint(); } // draws status bar text when mouse moves public void mouseMoved(MouseEvent e) { statusBar.setText("(" + e.getX() + ", " + e.getY() + ")"); } }
|
 |
 |
 |
 |
  | Sample code using the DrawingPanel class
|
 |
 |
 |
 |
  | /** * Draw a triangle on a DrawingPanel (p.178, BJP, Reges/Stepp). * * @author Chuck Iverson * @version 6/13/09 */
import java.awt.*; // for graphics
public class DrawLine2 { public static void main(String [] args) { // create drawing panel object DrawingPanel panel = new DrawingPanel(200,100); // get its graphics tool Graphics g = panel.getGraphics(); // use the graphics tool to draw a line on the panel g.drawLine(25,75,100,25); g.drawLine(100,25,175,75); g.drawLine(25,75,175,75); } }
|
 |
 |
 |
 |
  | /** * Draw and fill several shapes (p.180, BJP, Reges/Stepp). * * @author Chuck Iverson * @version 6/13/09 */
import java.awt.*; // for graphics
public class DrawShapes2 { public static void main(String [] args) { // create drawing panel object DrawingPanel panel = new DrawingPanel(200,100); // get its graphics tool Graphics g = panel.getGraphics(); // use the graphics tool to draw shapes on the panel g.fillRect(25,50,20,20); g.drawRect(150,10,40,20); g.setColor(Color.red); g.drawOval(50,25,20,20); g.setColor(Color.green); g.fillOval(150,50,40,20); } }
|
 |
 |
 |
 |
  | /** * Draw a smooth color gradient from black to white (pp.184, BJP, Reges/Stepp). * * @author Chuck Iverson * @version 6/13/09 */
import java.awt.*; // for graphics
public class DrawColorGradient { public static final int RECTS = 32; public static void main(String [] args) { // create drawing panel object DrawingPanel panel = new DrawingPanel(268,272); panel.setBackground(new Color(255,128,0)); // orange // get its graphics tool Graphics g = panel.getGraphics(); // from black to white, top left to bottom right int shift; for (int i = 0; i < RECTS; ++i) { shift = i * 256/RECTS; g.setColor(new Color(shift,shift,shift)); g.fillRect(shift,shift,20,20); } } }
|
 |
 |
 |
 |
  | Do Programming Project 1, p. 203.
|
 |
 |
 |
 |
  | Write a program that draws the pattern shown below onto a DrawingPanel:
|
 |
 |
 |
 |
  | The drawing panel's size is 400 x 400 and its background color is cyan. It contains four figures of concentric yellow circles with black outlines, all surrounded by a green rectangle with a black outline. The four figures on your drawing pael should have the properties shown in the table below.
|
 |
 |
 |
 |
  | Break down your program into methods for drawing one subfigure as well as larger grids of subfigures, such as the 5 x 5 grid at (10, 120).
|
 |
 |
|


 |
 |
 |