PaintPanel.java
Appearance
Back to Into Java - Part XVI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class PaintPanel extends JPanel
implements MouseMotionListener {
/* static class variables to determine shapes */
public static int LINE = 0;
public static int RECTANGLE = 1;
public static int OVAL = 2;
private boolean filledShape = true;
private int shape = 1; // default is rectangle
private int startX;
private int startY;
private int stopX;
private int stopY;
private Color paintColor;
private boolean mousePressed;
private PaintShape tempShape;
private Vector storedShapes;
public PaintPanel() {
super(true); // being double buffered
paintColor = Color.black;
storedShapes = new Vector();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mp) {
if ((mp.getModifiers()
& InputEvent.BUTTON1_MASK) != 0) {
startX = mp.getX();
startY = mp.getY();
mousePressed = true;
}
}
public void mouseReleased(MouseEvent mr) {
if ((mr.getModifiers()
& InputEvent.BUTTON1_MASK) != 0) {
mousePressed = false;
stopX = mr.getX();
stopY = mr.getY();
tempShape = new PaintShape(filledShape,
paintColor, shape, startX, startY);
tempShape.setStop(stopX, stopY);
storedShapes.addElement(tempShape);
repaint();
}
}
public void mouseClicked(MouseEvent mc) {
if (mc.getClickCount() > 1 && (mc.getModifiers()
& InputEvent.BUTTON3_MASK) != 0) {
Point p = new Point(mc.getX(), mc.getY());
removeShape(p);
}
}
});
addMouseMotionListener(this);
}
private void removeShape(Point xy) {
for (int i = 0; i < storedShapes.size(); i++) {
PaintShape tempS = (PaintShape)
storedShapes.elementAt(i);
if (tempS.contains(xy)) {
storedShapes.removeElement(tempS);
repaint();
break;
}
}
}
public void mouseMoved(MouseEvent skipIt) { /* empty */ }
public void mouseDragged(MouseEvent me) {
stopX = me.getX();
stopY = me.getY();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < storedShapes.size(); i++) {
PaintShape tempObj = (PaintShape)storedShapes.elementAt(i);
tempObj.paintComponent(g);
}
Color oldColor = g.getColor();
g.setColor(paintColor);
if (shape == 0) { // that is line
g.drawLine(startX, startY, stopX, stopY);
} else {
/* make four temp variables to avoid trouble */
int startXX = startX;
int startYY = startY;
int width = stopX - startX;
int height = stopY - startY;
if (width < 0) { // not good
startXX = stopX;
width = -width;
} else {
startXX = startX;
}
if (height < 0) { // not good
startYY = stopY;
height = -height;
} else {
startYY = startY;
}
if (shape == 1) { // that is rectangle
if (filledShape) {
g.fillRect(startXX, startYY, width, height);
} else {
g.drawRect(startXX, startYY, width, height);
}
} else { // remaining is oval
if (mousePressed) {
g.drawRect(startXX, startYY, width, height);
}
if (filledShape) {
g.fillOval(startXX, startYY, width, height);
} else {
g.drawOval(startXX, startYY, width, height);
}
}
}
g.setColor(oldColor);
}
public void setFilled(boolean b) {
filledShape = b;
}
public void setShape(int sh) {
shape = sh;
}
public void setColor(Color c) {
paintColor = c;
}
}