/*
可以绘制图案的区域
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class PaintPanel extends JComponent implements Serializable {
private ArrayList<Shape> shapes;
private Shape currentShape;
private String currentText;
private Stack<Operation> operationStack;
private Stack<Operation> redoStack;
public enum tools {OVAL, RECTANGLE, LINE, SEGMENT, SELECT, TEXT}//0 圆形 1 方形 2 自由画线 3 橡皮 4 选择 5 线段
public enum cursors {DEFAULT, CROSS, MOVE}// 0 普通鼠标 1 十字架 2 移动
private Font font;
private tools tool;
private Color fore;
private float stroke;
private cursors cursor;
private int x1 = 0;
private int y1 = 0;
private boolean fill;
private boolean delete;
private boolean saved;
public PaintPanel() {
tool = tools.OVAL;
fill = false;
stroke = 3.0f;
fore = Color.BLACK;
font = new Font("黑体", 0, 20);
shapes = new ArrayList<>();
currentShape = null;
currentText = null;
operationStack = new Stack<>();
redoStack = new Stack<>();
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
cursor = cursors.CROSS;
saved = false;
}
public boolean isSaved() { return saved; }
public void setTool(tools t) { this.tool = t; }
public void setCurrentText(String t) {
this.currentText = t;
}
public void setStroke(float stroke) {
this.stroke = stroke;
}
public void setFore(Color fore) {
this.fore = fore;
}
public void setFont(Font font) {
this.font = font;
}
public Color getFore() {
return fore;
}
public void setFill(boolean isFilled) {this.fill = isFilled;}
public void setDelete(boolean del) {this.delete = del;}
/**ArrayList
* 将存储的所有图形绘制出来
*/
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Shape s : shapes) {
s.draw(g2);
}
}
public void undo() {
saved = false;
if(!operationStack.empty()) {
Operation o = operationStack.pop();
o.undo();
redoStack.push(o);
repaint();
}
}
public void redo() {
saved = false;
if(!redoStack.empty()) {
Operation o = redoStack.pop();
o.redo();
operationStack.push(o);
repaint();
}
}
/**
* 查找坐标(x,y)处是否有图形,如有,返回这个图形,否则返回null
*/
private Shape find(int x, int y) {
for (Shape r : shapes) {
if (r.contain(x, y) && !r.isDeleted)
return r;
}
return null;
}
/**
* 将目前的内容画到一个BufferedImage中并返回
*/
public BufferedImage getImage () {
saved = true;
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = image.createGraphics();
graphics2d.fillRect(0,0, image.getWidth(), image.getHeight());
paint(graphics2d);
return image;
}
/**
* 清除所存的所有图形信息并重绘
*/
public void clear () {
saved = false;
shapes.clear();
operationStack.clear();
redoStack.clear();
repaint();
}
/**
* 将目前的图形信息保存到一个文件中
*/
public void writeImage (File file) {
saved = true;
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(shapes);
oos.flush();
oos.close();
fos.close();
} catch (IOException exce) {
JOptionPane.showMessageDialog(this, "保存出错");
exce.printStackTrace();
}
}
/**
* 从一个文件中读取图形信息,将其保存到程序中并重绘
*/
public void readImage (File file) {
saved = false;
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
shapes = (ArrayList<Shape>) ois.readObject();
repaint();
ois.close();
fis.close();
} catch (IOException exce) {
JOptionPane.showMessageDialog(this, "打开出错");
exce.printStackTrace();
} catch (ClassNotFoundException exce) {
exce.printStackTrace();
}
}
private class MouseHandler extends MouseAdapter {
private int pressX;
private int pressY;
private int releasedX;
private int releasedY;
/**
* 鼠标点击时执行的操作
*/
public void mousePressed(MouseEvent mouseEvent) {
switch (tool) {
case OVAL:
saved = false;
currentShape = new Oval(fore, mouseEvent.getX(), mouseEvent.getY(), stroke, fill);
shapes.add(currentShape);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.ADD, currentShape));
break;
case RECTANGLE:
saved = false;
currentShape = new Rectangle(fore, mouseEvent.getX(), mouseEvent.getY(), stroke, fill);
shapes.add(currentShape);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.ADD, currentShape));
break;
case LINE:
saved = false;
pressX = x1 = mouseEvent.getX();
pressY = y1 = mouseEvent.getY();
currentShape = new LineSet(fore, mouseEvent.getX(), mouseEvent.getY(), stroke);
shapes.add(currentShape);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.ADD, currentShape));
break;
case SELECT:
pressX = x1 = mouseEvent.getX();
pressY = y1 = mouseEvent.getY();
if (cursor == cursors.MOVE) {
currentShape = find(mouseEvent.getX(), mouseEvent.getY());
if(delete) {
saved = false;
assert currentShape != null;
currentShape.setDeleted(true);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.DEL, currentShape));
}
}
break;
case SEGMENT:
saved = false;
currentShape = new Line(fore, mouseEvent.getX(), mouseEvent.getY(), stroke);
shapes.add(currentShape);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.ADD, currentShape));
break;
case TEXT:
saved = false;
if(currentText == null)
break;
currentShape = new Text(fore, mouseEvent.getX(), mouseEvent.getY(), currentText, font);
shapes.add(currentShape);
redoStack.clear();
operationStack.push(new Operation(Operation.Type.ADD, currentShape));
break;
default:
break;
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
东北大学-java课程作业-Paint.zip (19个子文件)
src
Text.java 915B
Shape.java 3KB
Line.java 818B
Tools.java 2KB
LineSet.java 983B
Rectangle.java 1KB
Operation.java 1KB
MainFrame.java 6KB
PaintPanel.java 11KB
FileIO.java 2KB
MenuBar.java 1KB
FontDialog.java 1KB
Oval.java 1KB
Main.java 336B
.idea
Paint.iml 336B
vcs.xml 167B
misc.xml 406B
modules.xml 250B
encodings.xml 135B
共 19 条
- 1
资源评论
AI拉呱
- 粉丝: 2872
- 资源: 5510
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Android系统的手机地图应用软件开发中文3.78MB最新版本
- AndroidStudio环境下的jni调用(NDK)的方法中文最新版本
- Vue + UEditor + v-model 实体绑定.zip
- 最新版本ArcGISForAndroidEclipse环境配置中文最新版本
- VS Code 的 Vue 工具 .zip
- AndroidStudio快捷键中文最新版本
- TypeScript 和 Vue 的入门模板,带有详细的 README,描述了如何将两者结合使用 .zip
- The Net Ninja YouTube 频道上的 Vue.js 2 播放列表的课程文件.zip
- TDesign 的 Vue3.x UI 组件库 .zip
- 机器学习,深度学习,卷积神经网络ppt详细说明,详细推导
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功