Lab 10: Two Dimensional Drawing


Introduction

In lecture, we discussed the background mechanism used by Java GUI programs to render windows and process events. By creating a subclass of a Swing component and override the paintComponent method, as shown below, we can insert our own custom processing into this system.

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		...
	}

The Graphics object provides a representation of the screen along with methods to draw primitive shapes, such as those shown below. In addition, the Java 2D API defines a number of Shape objects that represent primitive shapes which can be passed to a Graphics2D object to be rendered on the screen.

Graphics.drawLine(int startX, int startY, int endX, int endY)
Graphics.drawRect(int upperLeftX, int upperLeftY, int width, int height)
Graphics.drawOval(int upperLeftX, int upperLeftY, int width, int height)

Line2D line = new Line2D.Double(int startX, int startY, int endX, int endY);
Shape s = line;
Graphics2D.draw(Shape s);

In this lab, you'll create a simple drawing program that lets the user add a series of shapes to a window using the mouse.


Lab10

Create the Java GUI program shown below by creating a subclass of JPanel and overriding the paintComponent() method. The program should maintain a list of Shape objects that should be drawn in the window whenever it needs to be repainted. New shapes can be added using the mouse. The user first selects the type of shape that should be added from the combo box. When the user presses the mouse in the window, the point clicked should be the starting endpoint or corner of the shape, which could be the endpoint of a line, the corner of a Rectangle, or the corner of a bounding rectangle around an ellipse. As the user drags the mouse, the new point under the mouse should be temporarily used as the other corner or endpoint, and the screen should be repainted at the earliest convenience to reflect the change in shape. When the screen is redrawn, in the shape in its last size must be erased and the new size be drawn, meaning the program must remember the old position of the shape as the user is dragging it. Once the user releases the mouse, the final dimensions of the shape should used to create a new shape object, which is then added to the list.

To accomplish this, the program needs to respond to several events. Separate tasks have to be done when the user presses, drags, or releases the mouse. Fortunately, the MouseListener and MouseMotionListener interfaces provide methods that let us do just that: mousePressed(), mouseDragged(), and mouseReleased(). The combo box will also need an ActionListener.

Since you should already be familiar with creating GUIs and implementing event listeners, I've created the Lab10.java file below that sets up the GUI and registers all the listeners. I've also provided you with a helper method that will draw a shape using a Graphics2D object.

Lab10.java


Hints