📄 mycanvas.java
字号:
package main;
/*
软件作者: 熊锡君,时守刚
软件版权归作者所有,其他人可以对软件进行修改,可以使用软件代码,(按类使用请保留作者信息)
*/
import Painter.Rectangle;
import Painter.Circle;
import Painter.Shape;
import Painter.Command;
import Painter.Line;
import Painter.Pencil;
import Painter.Eraser;
import Painter.CusString;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import java.util.ArrayList;
public class MyCanvas extends Canvas implements MouseMotionListener, MouseListener{
public float blc=1; //绘图时的起点坐标呵比例尺
Dimension size = getSize();
int m_wScreen=828,m_hScreen=426; // 当前视图的高度and宽度
Point ZoomPoint = new Point();
PaintBoard pd = null;
/* public Point Zoom(int x,int y)
{
Point point = new Point();
point.x =(int) (x * blc);
point.y =(int) (y * blc);
point.x = (int) (point.x + ZoomPoint.x * (1 - blc));
point.y = (int) (point.y + ZoomPoint.y * (1 - blc));
return point;
}
*/
// 记录鼠标点击的起始位置,x、y坐标
int beginX = 0;
int beginY = 0;
// 记录鼠标点击的当前位置,x、y坐标
int currentX = 0;
int currentY = 0;
int PastX = 0;
int PastY = 0;
//记录橡皮擦和清除
int eraser = 0;
int clear = 0;
//是否填充
boolean fill = false;
// 表示当前鼠标是否被按下
boolean bMousePressing = false;
Pencil pencil = null;
JTextArea statusTextArea = null;
private ArrayList Shapes = new ArrayList(), DelShapes = new ArrayList();
BufferedImage offScreenImg = null;
Graphics2D offScreenG = null;
// 记录当前的命令,是画圆、画线、还是画矩形
private int command = Command.LINE;
// 画笔的颜色
private Color fgColor = Color.BLACK;
private Color bgColor = Color.WHITE;
private Color brushColor = Color.BLACK;
public static final Stroke[] STROKES = new Stroke[] {
new BasicStroke(1.0f),
new BasicStroke(2.0f),
new BasicStroke(5.0f),
new BasicStroke(7.5f),
new BasicStroke(10.0f)
};
public static final int[] ERASER_STROKES = new int[] {
7,15,20,25,30
};
private int strokeIndex = 0, eraserIndex = 0;
private int n_GraphSelect = 0; //选中图形元素的总数
//存放选中图形位置
private int[] GraphSelect = new int[200];
//设置画板当前的绘图命令
public String getCommandString(int command){
switch(command){
case Command.LINE:
return "Line";
case Command.CIRCLE:
return "Circle";
case Command.PENCIL:
return "Pencil";
case Command.RECTANGLE:
return "Rectangle";
case Command.ERASER:
return "ERASER";
case Command.SELECT:
return "SELECT";
case Command.DELETE:
return "DELETE";
case Command.UNSELECTED:
return "UNSELECTED";
case Command.TEXTINSERT:
return "TEXTINSERT";
}
return "none";
}
// 实现对所有图形元素判断是否被选中
public int PointSelect(int x,int y,float j1)
{
int i = 0;
Shape shape = null;
for(; i < Shapes.size();i++)
{
shape = (Shape)Shapes.get(i);
if(shape.IsPoint(x,y,j1))
{
return i;
}
}
return -1;
}
// 存储选中图形元素
public boolean AddSelectList(int Index)
{
for(int i = 0; i < n_GraphSelect; i++)
{
if(Index == GraphSelect[i])
return false;
}
GraphSelect[n_GraphSelect++] = Index;
return true;
}
// 绘制图形元素特殊显示
public void DrawGraph(Graphics g,int Index)
{
if(Index < Shapes.size()){
Shape shape =(Shape) Shapes.get(Index);
shape.draw(g,1,bgColor);
}
}
//设置画刷颜色
public void setBrushColor(Color c){
brushColor = c;
}
//删除指定编号的已绘图形
public void Delete(int Index)
{
if(Index < Shapes.size()){
Shape shape =(Shape) Shapes.get(Index);
Shapes.remove(Index);
DelShapes.add(shape);
}
}
//保存图像为JPG格式
public void SaveFile(String fileName) throws Exception {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(Shapes); //写存储图形对象的数组
out.writeObject(this.bgColor); //写背景颜色
out.close();
}
// 打开jwd格式文件
public void OpenFile(String fileName) throws Exception{
DelShapes.clear(); //清空存储删除图形对象的数组
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
Shapes =(ArrayList) (in.readObject()); //读存储图形对象的数组
this.bgColor = (Color)(in.readObject());//读背景颜色
in.close();
repaint();
}
/**
* 构造方法
*/
public MyCanvas(JTextArea statusTextArea, PaintBoard pd) {
this.pd = pd;
strokeIndex = 0;
eraserIndex = 0;
this.statusTextArea = statusTextArea;
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
setForeground(Color.black);
setBackground(Color.white);
// 添加事件处理器
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void setStrokeIndex(int i) {
if (i < 0 || i > 4)
throw new IllegalArgumentException("Invaild Weight Specified!");
strokeIndex = i;
}
public void setEraserIndex(int i) {
if (i < 0 || i > 4)
throw new IllegalArgumentException("Invaild Size Specified!");
eraserIndex = i;
}
public void clearBoard() {
Shapes.clear();
DelShapes.clear();
n_GraphSelect = 0;
Graphics g=getGraphics();
paint(g);
}
/**
* 撤销操作,取消刚刚画的图形。
*/
public void undo() {
if (Shapes.size() > 0) {
// 将vShapes中的最后画的一个图形取出
Object shape = Shapes.remove(Shapes.size() - 1);
// 放入到vDelShapes中
DelShapes.add(shape);
// 重画画板
// repaint();
Graphics g=getGraphics();
paint(g);
}
}
/**
* 恢复操作,取消刚刚撤销的图形
*/
public void redo() {
if (DelShapes.size() > 0) {
// 将vDelShapes中最后一个被删除的图形取出
Object shape = DelShapes.remove(DelShapes.size() - 1);
// 放到vShapes中
Shapes.add(shape);
// 重画画板
Graphics g=getGraphics();
paint(g);
}
}
/**
* 设置命令,画线、画圆、画矩形
* @param command
*/
public void setCommand(int command) {
this.command = command;
}
/**
* 设置画笔颜色
* @param color
*/
public void setForegroundColor(Color color) {
this.fgColor = color;
}
/**
* 设置背景颜色
* @param color
*/
public void setBackgroundColor(Color color) {
this.bgColor = color;
}
public Color getBackgroundColor() {
return bgColor;
}
public void setn_GraphSelect(int n) {
n_GraphSelect = n;
Graphics g=getGraphics();
paint(g);
}
public void setSelectDel() {
for(int i = 0; i < n_GraphSelect; i++)
Delete(GraphSelect[i]);
// System.out.println("n_GraphSelect:" +canvases[canvas_control].n_GraphSelect);
n_GraphSelect = 0;
Graphics g=getGraphics();
paint(g);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -