📄 jpaint.java
字号:
//这个程序为java 实现的画图编辑器程序
//创建时间为2006年12月15日
//代码编写人为: 李慧琼 王乃蓁
//备注: 本代码分别用java JDK 1.4.2 /1.5 编译
import java.io.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
class Shape {
public static final int LINE = 1;
public static final int SQUARE = 2;
public static final int ELLIPSE = 3;
public static final int ERASER =4;
protected int startX;
protected int startY; //开始坐标
protected int endX;
protected int endY; //终止坐标
protected boolean isFill;
protected boolean isSelected;
protected Color color;
protected int shape;
public Shape()
{
isFill = false;
isSelected = false;
color = Color.BLACK;
}
public Shape(int sx, int sy, int ex, int ey)
{
this();
startX = sx;
startY = sy;
endX = ex;
endY = ey;
}
public void setStart(int x, int y)
{
startX = x;
startY = y;
}
public void setEnd(int x, int y)
{
endX = x;
endY = y;
}
public Point2D getStart()
{
return new Point2D.Double(startX, startY);
}
public Point2D getEnd() {
return new Point2D.Double(endX, endY);
}
protected int maxX() {
return (startX >= endX) ? startX : endY;
}
protected int minX() {
return (startX <= endX) ? startX : endX;
}
protected int maxY() {
return (startY >= endY) ? startY : endY;
}
protected int minY() {
return (startY <= endY) ? startY : endY;
}
public boolean isContain(int x, int y) {
if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
return true;
else
return false;
}
//显示颜色选择
public void writeData(PrintWriter out)throws IOException {
int fill, rgb;
rgb = color.getRGB();
fill = isFill ? 1 : 0;
out.println(startX + "|" + startY + "|" + endX + "|"
+ endY + "|" + fill + "|" + rgb + "|" + shape);
}
public void readData(BufferedReader in)throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, "|");
startX = Integer.parseInt(t.nextToken());
startY = Integer.parseInt(t.nextToken());
endX = Integer.parseInt(t.nextToken());
endY = Integer.parseInt(t.nextToken());
isFill = (Integer.parseInt(t.nextToken()) == 1) ? true : false;
color = new Color(Integer.parseInt(t.nextToken()));
shape = Integer.parseInt(t.nextToken());
isSelected = false;
}
}
class Line extends Shape
{
public Line()
{
super();
shape = LINE;
}
public Line(int sx, int sy, int ex, int ey)
{
super(sx, sy, ex, ey);
shape = LINE;
}
public boolean isContain(int x, int y) {
double c = Math.sqrt((endX - startX) * (endX - startX) +
(endY - startY) * (endY - startY));
double c1 = Math.sqrt((x - startX) * (x - startX) +
(y - startY) * (y - startY));
double c2 = Math.sqrt((x - endX) * (x - endX) +
(y - endY) * (y - endY));
if(c1+c2-c <= 0.25*c)
return true;
else
return false;
}
}
class Square extends Shape
{
public Square()
{
super();
shape = SQUARE;
}
public Square(int sx, int sy, int ex, int ey)
{
super(sx, sy, ex, ey);
shape = SQUARE;
}
public boolean isContain(int x, int y)
{
if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
return true;
else
return false;
}
}
class Eraser extends Shape
{
public Eraser()
{
super();
shape = ERASER;
}
}
class Ellipse extends Shape {
public Ellipse() {
super();
shape = ELLIPSE;
}
public Ellipse(int sx, int sy, int ex, int ey) {
super(sx, sy, ex, ey);
shape = ELLIPSE;
}
public boolean isContain(int x, int y) {
double dx = (double)(startX + endX) / 2;
double dy = (double)(startY + endY) / 2;
double a = (maxX() - minX()) / 2;
double b = (maxY() - minY()) / 2;
double x1 = (x - dx)*(x - dx) / (a*a);
double y1 = (y - dy)*(y - dy) / (b*b);
if(x1+y1 <= 1)
return true;
else
return false;
}
}
class ButtonPanel extends JPanel
{
JButton cusorButton = new JButton("移动");
JButton lineButton = new JButton("直线");
JButton squareButton = new JButton("矩形");
JButton ellipseButton = new JButton("圆");
JButton colorButton = new JButton("颜色");
JButton eraserButton = new JButton("橡皮");
public ButtonPanel()
{
cusorButton.setToolTipText("Move the shape"); //当鼠标移动到此按钮上时显示 Move the shape
lineButton.setToolTipText("Draw a line");
squareButton.setToolTipText("Draw a rectangle");
ellipseButton.setToolTipText("Draw a ecllipse");
colorButton.setToolTipText("Choose the draw color");
eraserButton. setToolTipText("Clean the Window");
add(cusorButton);
add(lineButton);
add(squareButton);
add(ellipseButton);
add(colorButton);
add(eraserButton);//将按钮 添加到按钮面板上
}
}
class PaintPanel extends JPanel
{
private int currentShape;
int eraser;
private boolean currentFill;
private Color currentColor;
private Color currentBackColor;
private boolean paintEnabled;
private ArrayList polygon;
private int moveX, moveY;
private int currentX, currentY;
public PaintPanel()
{
paintEnabled = false;
currentFill = false;
currentColor = Color.BLACK;
currentBackColor = Color.WHITE;
polygon = new ArrayList(25);
setLayout(new BorderLayout()); //设置布局格式
ButtonPanel buttonPanel = new ButtonPanel();
add(buttonPanel, BorderLayout.NORTH);
add(new LabelPanel(), BorderLayout.SOUTH);
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
buttonPanel.cusorButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
paintEnabled = false;
setCursor(
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
});
buttonPanel.lineButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
paintEnabled = true;
setCursor(Cursor.getDefaultCursor());
currentShape = Shape.LINE;
}
});
buttonPanel.squareButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
paintEnabled = true;
setCursor(Cursor.getDefaultCursor());
currentShape = Shape.SQUARE;
}
});
buttonPanel.ellipseButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
paintEnabled = true;
setCursor(Cursor.getDefaultCursor());
currentShape = Shape.ELLIPSE;
}
});
buttonPanel.colorButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
Color defaultColor = Color.BLACK;
Color selected = JColorChooser.showDialog(
PaintPanel.this, "Change Color", defaultColor);
currentColor = selected;
}
});
buttonPanel.eraserButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
paintEnabled = true;
currentShape = Shape.ERASER;
}
}); //各个按钮的事件监听
} //public PaintPanel() end
public void paintComponent(Graphics g) //重写绘制方法
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int w = getWidth();
int h = getHeight();
int x,y;
Rectangle2D scale = new Rectangle2D.Double(0, 0, w, h);
g2.setColor(currentBackColor);
g2.fill(scale);
for(int i = 0; i < polygon.size(); i++) {
Shape temp = (Shape)polygon.get(i);
g2.setColor(temp.color);
switch(temp.shape) {
case Shape.LINE: //画线
Line2D showLine = new Line2D.Double(
temp.getStart(), temp.getEnd());
g2.draw(showLine);
break;
case Shape.SQUARE: //画矩形
Rectangle2D showSquare = new Rectangle2D.Double();
showSquare.setFrameFromDiagonal(
temp.getStart(), temp.getEnd());
if(temp.isFill)
g2.fill(showSquare);
else
g2.draw(showSquare);
break;
case Shape.ELLIPSE: //画圆
Ellipse2D showEllipse = new Ellipse2D.Double();
showEllipse.setFrameFromDiagonal(
temp.getStart(), temp.getEnd());
if(temp.isFill)
g2.fill(showEllipse);
else
g2.draw(showEllipse);
break;
case Shape.ERASER://橡皮擦除
{
x=(int)temp.getStart().getX();
y=(int)temp.getStart().getY();
g.setColor(currentBackColor);
g.setPaintMode();
g.fillRect(temp.startX,temp.startY,30,30);
temp.startX=x;
temp.startY=y;
}
}
}
}// paintComponent() end
public void setBackColor(Color color) //设置背景色
{
currentBackColor = color;
}
public void setForeColor(Color color) //设置前景色
{
currentColor = color;
}
public void setDraw() {
currentFill = false;
}
public void setFill() {
currentFill = true; //设置填充
}
public void writeShapes(String file) {
try {
PrintWriter out = new PrintWriter(new
FileWriter(file));
out.println(polygon.size());
out.println(getWidth() + "|" + getHeight() + "|" +
currentBackColor.getRGB());
for(int i = 0; i < polygon.size(); i++)
((Shape)(polygon.get(i))).writeData(out);
out.close();
} catch(IOException exception)
{
exception.printStackTrace();
}
}
public void readShapes(String file)//文件操作
{
try {
BufferedReader in = new BufferedReader(new
FileReader(file));
int size = Integer.parseInt(in.readLine());
String back = in.readLine();
StringTokenizer t = new StringTokenizer(back, "|");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -