Command.java
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public interface Command {
public final static int LINE = 2;
public final static int CIRCLE = 4;
public final static int RECTANGLE = 8;
}
ICircle.java
import java.awt.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ICircle implements Shape {
private Color color;
private int x;
private int y;
private int width;
private int height;
public ICircle(int x, int y, int width, int height, Color color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawArc(x, y, width, height, 0, 360);
}
}
ILine.java
import java.awt.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ILine implements Shape {
private int x1;
private int y1;
private int x2;
private int y2;
private Color color;
public ILine(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
IRectangle.java
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class IRectangle implements Shape {
private int x;
private int y;
private int width;
private int height;
private Color color;
public IRectangle(int x, int y, int width, int height, Color color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
PaintApp.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class PaintApp extends Frame {
Panel commandPanel = new Panel();
Panel colorPanel = new Panel();
Panel shapePanel = new Panel();
Button btnRed = new Button("Red");
Button btnGreen = new Button("Green");
Button btnBlue = new Button("Blue");
Button btnLine = new Button("Line");
Button btnRectangle = new Button("Rectangle");
Button btnCircle = new Button("Circle");
Button btnUndo = new Button("Undo");
Button btnRedo = new Button("Redo");
Button btnExit = new Button("Exit");
PaintBoard paintBoard = new PaintBoard();
public PaintApp() {
this.setLayout(new BorderLayout());
btnLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnLine_actionPerformed(e);
}
});
btnRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRectangle_actionPerformed(e);
}
});
btnCircle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCircle_actionPerformed(e);
}
});
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnExit_actionPerformed(e);
}
});
btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnUndo_actionPerformed(e);
}
});
btnRedo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRedo_actionPerformed(e);
}
});
shapePanel.setLayout(new FlowLayout());
shapePanel.add(btnLine, null);
shapePanel.add(btnRectangle, null);
shapePanel.add(btnCircle, null);
shapePanel.add(btnUndo, null);
shapePanel.add(btnRedo, null);
shapePanel.add(btnExit,null);
btnRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnRed_actionPerformed(e);
}
});
btnGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnGreen_actionPerformed(e);
}
});
btnBlue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnBlue_actionPerformed(e);
}
});
colorPanel.setLayout(new FlowLayout());
colorPanel.add(btnRed, null);
colorPanel.add(btnGreen, null);
colorPanel.add(btnBlue, null);
commandPanel.setLayout(new BorderLayout());
commandPanel.add(shapePanel, BorderLayout.NORTH);
commandPanel.add(colorPanel, BorderLayout.CENTER);
this.add(commandPanel, BorderLayout.SOUTH);
this.add(paintBoard, BorderLayout.CENTER);
///////////////////////////////////////////////////////////////////////////
btnLine.setForeground(Color.red);
paintBoard.setCommand(Command.LINE);
btnRed.setForeground(Color.red);
paintBoard.setColor(Color.red);
///////////////////////////////////////////////////////////////////////////
}
void btnExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
void btnUndo_actionPerformed(ActionEvent e) {
paintBoard.undo();
}
void btnRedo_actionPerformed(ActionEvent e) {
paintBoard.redo();
}
void btnLine_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.red);
btnRectangle.setForeground(Color.black);
btnCircle.setForeground(Color.black);
paintBoard.setCommand(Command.LINE);
}
void btnRectangle_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.black);
btnRectangle.setForeground(Color.red);
btnCircle.setForeground(Color.black);
paintBoard.setCommand(Command.RECTANGLE);
}
void btnCircle_actionPerformed(ActionEvent e) {
btnLine.setForeground(Color.black);
btnRectangle.setForeground(Color.black);
btnCircle.setForeground(Color.red);
paintBoard.setCommand(Command.CIRCLE);
}
void btnRed_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.red);
btnGreen.setForeground(Color.black);
btnBlue.setForeground(Color.black);
paintBoard.setColor(Color.red);
}
void btnGreen_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.black);
btnGreen.setForeground(Color.red);
btnBlue.setForeground(Color.black);
paintBoard.setColor(Color.green);
}
void btnBlue_actionPerformed(ActionEvent e) {
btnRed.setForeground(Color.black);
btnGreen.setForeground(Color.black);
btnBlue.setForeground(Color.red);
paintBoard.setColor(Color.blue);
}
public static void main(String[] args) {
PaintApp paintApp = new PaintApp();
paintApp.setSize(600, 500);
paintApp.setVisible(true);
}
}
PaintBoard.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class PaintBoard extends Canvas implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener {
int srcX = 0;
int srcY = 0;
int lineToX = 0;
int lineToY = 0;
boolean bMousePressing = false;
java.util.Stack vShapes = new java.util.Stack();
java.util.Stack vDelShapes = new java.util.Stack();
private int command = 0;
private Color color;
public void undo() {
if (vShapes.size() > 0) {
Object shape = vShapes.pop();
vDelShapes.push(shape);
repaint();
}
}
public void redo() {
if (vDelShapes.size() > 0) {
Object shape = vDelShapes.pop();
vShapes.push(shape);
repaint();
}
}
public void setCommand(int command) {
this.command = command;
}
public void setColor(Color color) {
this.color = color;
}
public PaintBoard() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paint(Graphics g) {
Dimension size = size();
int width = size.width ;
int height = size.height;
g.setColor(Color.white);
g.fillRect(0,0, width,height);
Shape shape = null;
java.util.Enumeration enum = vShapes.elements();
g.setColor(Color.blue);
while (enum.hasMoreElements()) {
shape = (Shape) enum.nextElement();
shape.draw(g);
}
if (bMousePressing) {
g.setColor(color);
switch (command) {
case Command.LINE:
g.drawLine(srcX, srcY, lineToX, lineToY);
break;
case Command.RECTANGLE:
if (lineToX < srcX) {
if (lineToY < srcY) {
g.drawRect(lineToX, lineToY, srcX - lineToX , srcY - lineToY);
}
else {
g.drawRect(lineToX, srcY, srcX - lineToX, lineToY - srcY);
}
}
else {
if (lineToY < srcY) {
g.drawRect(srcX, lineToY, lineToX - srcX, srcY - lineToY);
}
else {
g.drawRect(srcX, srcY, lineToX - srcX, lineToY - srcY);
}
}
break;
case Command.CIRCLE:
int w = (int)java.lang.Math.sqrt((srcX - lineToX) * (srcX - lineToX) + (srcY - lineToY) * (srcY - lineToY));
g.drawArc(srcX - w/2, srcY - w/2, w, w, 0, 360);
break;
}//End switch(command)
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
srcX = e.getX();
srcY = e.getY();
bMousePressing = true;
}
public void mouseReleased(MouseEvent e) {
lineToX = e.getX();
lineToY = e.getY();
bMousePressing = false;
switch (command) {
case Command.LINE:
ILine line = new ILine(srcX, srcY, lineToX, lineToY, color);
vShapes.push(line);
break;
case Command.RECTANGLE:
IRectangle rectangle = null;
if (lineToX < srcX) {
if (lineToY < srcY) {
rectangle = new IRectangle(lineToX, lineToY, srcX - lineToX , srcY - lineToY, color);
}
else {
rectangle = new IRectangle(lineToX, srcY, srcX - lineToX, lineToY - srcY, color);
}
}
else {
if (lineToY < srcY) {
rectangle = new IRectangle(srcX, lineToY, lineToX - srcX, srcY - lineToY, color);
}
else {
rectangle = new IRectangle(srcX, srcY, lineToX - srcX, lineToY - srcY, color);
}
}
vShapes.push(rectangle);
break;
case Command.CIRCLE:
int w = (int)java.lang.Math.sqrt((srcX - lineToX) * (srcX - lineToX) + (srcY - lineToY) * (srcY - lineToY));
ICircle circle = new ICircle(srcX - w/2, srcY - w/2, w, w, color);
vShapes.push(circle);
break;
} //End switch(command)
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
lineToX = e.getX();
lineToY = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}
Shape.java
import java.awt.*;
import java.awt.event.*;
/**
* @author aassd
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public interface Shape {
public void draw(Graphics g);
}
本文地址:http://com.8s8s.com/it/it15463.htm