draw.java Code
/*
* draw.java: Simple rubber-banding demo. Class variables (x1, y1)
* hold the anchored button-down point, while (x2, y2) hold the current
* mouse pint. Use a state variable to track drags in process through
* states WAITING - > DRAGGING -> WAITING. When dragging, mouseDrag sets
* XOR drawing mode, then erases the last figure and draws the current one
* to give the rubberband effect.
* Created on September 30, 2000, 6:33 PM
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author hunt, jan
* @version
*/
public class draw extends java.applet.Applet implements MouseListener, MouseMotionListener, ActionListener {
static final int WAITING = 0; //state when quiescent
static final int DRAGGING = 1; //state when rubber-banding
int x1, y1; //button-down point
int x2, y2; //current point
//Dragging state (WAITING or DRAGGING)
int state = WAITING;
private Button btnReset;
int tool = 3;
int drawingColor = 0;
//implement an array of objects
Rectangle FigIcon[] = new Rectangle[7];
Rectangle ColIcon[] = new Rectangle[8];
//create color array
Color myColors[] = { Color.black, Color.red, Color.blue, new Color(0,255,0), Color.yellow, new Color(128,0,128), new Color(255,128,0), new Color(128,64,0) };
Figure FigDrag;
public void init () {
setBackground(Color.white);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setSize(480, 360); //applet size
//create and add button
btnReset = new Button("Reset");
add(btnReset);
btnReset.addActionListener(this);
int k = 0;
for(int i=0; i < FigIcon.length; i++){
FigIcon[i] = new Rectangle(10+k, 30, 30, 30);
k += 30;
}//end for FigIcon
for(int j=0; j < ColIcon.length; j++) {
ColIcon[j] = new Rectangle(20+k, 30, 30, 30);
k += 30;
}//end for ColIcon
repaint();
}//end init()
public void paint(Graphics g) {
g.drawRect(0, 0, 479, 359); //framing rectangle
//draw the tool icons and the color palette
for(int i=0; i < FigIcon.length; i++) {
g.setColor(Color.black);
g.fillRect(FigIcon[i].x, FigIcon[i].y, FigIcon[i].width, FigIcon[i].height);
}//end For FigIcon
for(int j=0; j < ColIcon.length; j++) {
g.setColor(myColors[j]);
g.fillRect(ColIcon[j].x, ColIcon[j].y, ColIcon[j].width, ColIcon[j].height);
}//end FOR for colIcon
//draw the line tool pictures on the icons
g.setColor(Color.white);
g.drawRect(15, 35, 20, 20);
g.drawOval(45, 35, 20, 20);
g.drawRoundRect(75, 35, 20, 20, 5, 5);
g.drawLine(105, 35, 125, 55);
//draw the solid tool pictures on the icons
g.fillRect(135, 35, 20, 20);
g.fillOval(165, 35, 20, 20);
g.fillRoundRect(195, 35, 20, 20, 5, 5);
}//end paint
public void mouseEntered(MouseEvent event) { //enters applet
}//end mouseEntered
public void mousePressed(MouseEvent event) { //state to dragging
//If in wait state, start the drag
if(state == WAITING) {
state = DRAGGING;
x1 = x2 = event.getX();
y1 = y2 = event.getY();
}//end IF state WAITING
//setup to recognize what icon is clicked
for(int i=0; i < FigIcon.length; i++) {
if(FigIcon[i].contains(event.getX(), event.getY()))
tool = i;
}//end for FigIcon
for(int j=0; j < ColIcon.length; j++){
if(ColIcon[j].contains(event.getX(), event.getY()))
drawingColor = j;
}//end for ColIcon
//not sure what I need to get out of mousePressed now that I add this
//figDrag = new Figure(event.getX(), event.getY(), 3);
}//end mousePressed
public void mouseReleased(MouseEvent event) { //state back to waiting
//if dragging, reset the wait state
if(state == DRAGGING)
state = WAITING;
}//end mouseReleased
public void mouseExited(MouseEvent event) { //leave applet
}//end mouseExited
public void mouseClicked(MouseEvent event) {
}//end mouseClicked
public void mouseMoved(MouseEvent event) {
}//end mouseMoved
public void mouseDragged(MouseEvent event) {
Graphics g;
int left, top, width, height;
//if dragging, set XOR mode, then erase last figure and draw the current one
if(state == DRAGGING) {
g = getGraphics();
g.setXORMode(Color.white);
switch(drawingColor) {
case 0:
g.setColor(myColors[0]);
break;
case 1:
g.setColor(myColors[1]);
break;
case 2:
g.setColor(myColors[2]);
break;
case 3:
g.setColor(myColors[3]);
break;
case 4:
g.setColor(myColors[4]);
break;
case 5:
g.setColor(myColors[5]);
break;
case 6:
g.setColor(myColors[6]);
break;
case 7:
g.setColor(myColors[7]);
break;
}//end switch for colors
switch(tool) {
case 0: //draw the square
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw square with calculated values(erases last square)
g.drawRect(left, top, width, height);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rectangle at new mouse position
g.drawRect(left, top, width, height);
break;
case 1: //draw the circle
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw circle with calculated values(erases last square)
g.drawOval(left, top, width, height);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw circle at new mouse position
g.drawOval(left, top, width, height);
break;
case 2: //draw roundRectangle
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rndrect with calculated values(erases last square)
g.drawRoundRect(left, top, width, height, 25, 25);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rndrect at new mouse position
g.drawRoundRect(left, top, width, height, 25, 25);
break;
case 3: //draw the line
g.drawLine(x1, y1, x2, y2); //erase last line
x2 = event.getX(); y2 = event.getY();
g.drawLine(x1, y1, x2, y2); //draw new line
break;
//case statements for the fill tools
case 4: //draw the square
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw square with calculated values(erases last square)
g.fillRect(left, top, width, height);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rectangle at new mouse position
g.fillRect(left, top, width, height);
break;
case 5: //draw the circle
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw circle with calculated values(erases last square)
g.fillOval(left, top, width, height);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw circle at new mouse position
g.fillOval(left, top, width, height);
break;
case 6: //draw roundRectangle
//calculate left edge(smaller of x1 and x2) etc.
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rndrect with calculated values(erases last square)
g.fillRoundRect(left, top, width, height, 25, 25);
//get new mouse position into x2 and y2
x2 = event.getX(); y2 = event.getY();
//recalculate these values based on new x2 and y2
left = (x1 < x2) ? x1 : x2;
top = (y1 < y2) ? y1 : y2;
width = Math.abs(x2 - x1);
height = Math.abs(y2 - y1);
//draw rndrect at new mouse position
g.fillRoundRect(left, top, width, height, 25, 25);
break;
}//end switch for tool
}//end if state DRAGGING
}//end mouseDragged
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnReset)
repaint();
}//end actionPerformed
//created class for Figure
class Figure {
int x1, y1, x2, y2;
int type; // 0=square; 1=circle, 2=rndRect, 3=line
Color color;
Figure(int u, int v, int kind) {
x1 = x2 = u;
y1 = y2 = v;
type = kind;
color = Color.black;
}//end figure constructor
}//end class Figure
}//end class draw