基于Java实现的绘图软件工具设计 演示视频
做一个简单的绘图工具,以CAD的方式操作,能放置直线、矩形、圆和文字,能选中图形,修改参数,如颜色等,能拖动图形和调整大小,可以保存和恢复。功能请参考视频演示。
要求上传:
做一个简单的绘图工具,以CAD的方式操作。

铅笔 & 刷子
回退撤销回退橡皮喷枪
橡皮直线 & 圆 & 矩形
选择后拖动加粗 变细放大 缩小回退 撤销回退删除文字
放大 缩小橡皮
选择
删除
回退
撤销回退
保存
打开
颜色面板
案例


Shape类,用来描述一个图形
Circle Eraser Rect String GUm Line MyString 继承Shape抽象类
public abstract void draw(Graphics2D g)画图形public abstract boolean click( double curX, double curY ) 判断是否被选中Draw类分配
ActionListener, MouseListener, MouseMotionListener接口监听鼠标操作
public void actionPerformed(ActionEvent e)public void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e)public void mouseDragged(MouseEvent e)以Circle为例
设置抽象了你Shape,用来描述一个图形
(x1,y1)表示左上角坐标,(x2,y2)表示右下角坐标,color表示颜色信息,stroke表示字体粗细。
需要实现两个虚函数draw(Graphics2D g) click( double curX, double curY ),分别表示图形绘制和判断鼠标是否点击该图形。
// 函数的具体实现详见代码
public abstract class Shape implements Serializable{
private int x1, x2, y1, y2;
private Color color;
private int stroke;
public Shape( int x1, int y1, int x2, int y2, Color c, int s ){
stroke = s;
color = c;
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public abstract void draw(Graphics2D g);
public abstract boolean click( double curX, double curY );
public void move( int dx, int dy ){}
public int getX1(){}
public void setX1(int x1){}
public int getX2(){}
public void setX2(int x2){}
public int getY1(){}
public void setY1(int y1){}
public int getY2(){}
public void setY2(int y2){}
public Color getColor(){}
public void setColor( Color c){}
public BasicStroke getStroke() {}
public void setStroke( int s ){}
public void addX1(int dx){}
public void addX2(int dx){}
public void addY1(int dy){}
public void addY2(int dy){}
public void enlarge() {}
public void narrow() {}
}
六个类Circle, Eraser, Gum, Line, MyString, Rect,均继承自Shape类
public class Circle extends Shape {
public Circle(int x1, int y1, int x2, int y2, Color color,
int s) {
super(x1, y1, x2, y2, color, s);
}
public void draw(Graphics2D g) {
int x1 = Math.min(getX1(), getX2());
int x2 = Math.max(getX1(), getX2());
int y1 = Math.min(getY1(), getY2());
int y2 = Math.max(getY1(), getY2());
g.setStroke(getStroke());
g.setColor(getColor());
Ellipse2D circle = new Ellipse2D.Double(x1, y1, x2 - x1, y2 - y1);
g.draw(circle);
}
public boolean click(double x, double y){
int x1 = Math.min(getX1(), getX2());
int x2 = Math.max(getX1(), getX2());
int y1 = Math.min(getY1(), getY2());
int y2 = Math.max(getY1(), getY2());
return ( x >= x1 && x <= x2 && y >= y1 && y <= y2);
}
}
StackDoList 用来存储所有已绘制的图形。Dolist 里用 remove 函数删掉对应的对象。所以说,删除操作不支持撤销。StackTodoList 用来恢复绘图。具体地,每次撤销时,将 DoList 栈顶元素弹出,压入 TodoList 里。每次重做时,重复相反过程。paint(),每当进行完撤销操作后就清空画布并重绘整个 DolistLoad(JFrame prentFrame, String title, Stack<Shape> shapes, Main panel )
{
super(prentFrame, title, true );
this.shapes = shapes;
this.panel = panel;
JPanel p1 = new JPanel();
JLabel label = new JLabel("请输入文本:");
p1.add(label);
text = new JTextField(30);
text.addActionListener(this);
p1.add(text);
getContentPane().add("Center", p1);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton cancelButton = new JButton("取 消");
cancelButton.addActionListener(this);
button = new JButton("确 定");
button.addActionListener(this);
p2.add(button);
p2.add(cancelButton);
getContentPane().add("South", p2);
pack();
}
