📄 paintpanel.java
字号:
/**
* @(#)PaintPanel.java
*/
package project.paint;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class PaintPanel extends JPanel {
public static final int GETCOLOR = 99;
public Project instance;
private static final int OBJECTCOUNT = 65536;
PopMenu popMenu;
JPanel c;
Color backColor;
int width = 400;
int height = 300;
int changeSize;
File fileName;
Color nowColor;
Color forColor;
Color bacColor;
Color midColor;
Point p1;
Point p2;
boolean isFill;
float stroke;
int nowType;
LinkedList<BaseObject> graphics;
LinkedList<BaseObject> undoGraphics;
boolean isDrawing;
boolean isChanging;
boolean isFilling;
boolean isRotating;
boolean isSquare; // 是否正二维图形
int gradientPaint; // 渐变涂色样式
int nowObject; // 保存选中物体
int nowChangeGraphics;
int nowRotate;
int nowGraphics;
int xOffset, yOffset;
int preX;
/**
* 构造出绘制区域
*
* @param o
* 画图板的实例对象
*/
public PaintPanel(Project o) {
if (o == null)
return;
instance = o;
setLayout(null);
// 初始化参数
initParam();
c = new JPanel() {
public void paint(Graphics g) {
// super.paint(g);
if (nowType == BaseObject.PENCIL && isDrawing) { // 如果是铅笔的话
((BaseObject) (graphics.getLast()))
.drawLastLine((Graphics2D) g);
// graphics.get(graphics.size()-1)
} else {
Image buffer = createImage(width, height);
Graphics2D gg = (Graphics2D) (buffer.getGraphics());
// Graphics2D gg=(Graphics2D)g;
gg.setColor(backColor);
gg.fillRect(0, 0, width, height);
for (int j = 0; j < graphics.size(); j++) {
draw(gg, (BaseObject) (graphics.get(j)));
}
if (nowGraphics >= 0) {
nowObject = nowGraphics;
drawFocus(gg, nowGraphics);
setDelable(true);
} else if (nowChangeGraphics >= 0) {
drawFocus(gg, nowChangeGraphics);
} else if (nowRotate >= 0) {
BaseObject gh = (BaseObject) (graphics.get(nowRotate));
gh.drawRotateFocus(gg);
} else {
nowObject = -1;
setDelable(false);
}
g.drawImage(buffer, 0, 0, this);
}
}
void draw(Graphics2D g2d, BaseObject g) {
g.draw(g2d);
}
private void drawFocus(Graphics2D gg, int index) {
if (nowType == -1) {
try {
BaseObject gh = (BaseObject) (graphics.get(index));
gg.setColor(Color.BLACK);
gh.drawMoveFocus(gg, Color.WHITE);
} catch (Exception e) { // 可能产生Exception,比如操作太快,
// 不知道为什么会产生NullPointerException
System.out.println("==============================");
System.out.println("drawFocus(Graphics2D,int)方法出现异常:");
}
}
}
};
c.setFocusable(true);
c.setBackground(backColor);
c.setBounds(6, 6, width, height);
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
c_mouseClicked(e);
}
public void mousePressed(MouseEvent e) {
c_mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
c_mouseReleased(e);
}
public void mouseExited(MouseEvent e) {
c_mouseExited(e);
}
});
c.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
c_mouseDragged(e);
}
public void mouseMoved(MouseEvent e) {
c_mouseMoved(e);
}
});
c.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
c_keyTyped(e);
}
});
// c.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
popMenu = new PopMenu(this);
c.add(popMenu);
add(c);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
p_mouseDragged(e);
}
public void mouseMoved(MouseEvent e) {
p_mouseMoved(e);
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
p_mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
p_mouseReleased(e);
}
});
setBackground(Color.DARK_GRAY);
setType(-1);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(new Color(255, 255, 255));
g.drawRect(1, 1, 3, 3);
g.drawRect(width / 2, 1, 3, 3);
g.drawRect(width + 5, 1, 3, 3);
g.drawRect(1, height + 5, 3, 3);
g.drawRect(1, height / 2, 3, 3);
g.fillRect(width + 6, height / 2, 4, 4);
g.fillRect(width + 6, height + 6, 4, 4);
g.fillRect(width / 2, height + 6, 4, 4);
// redraw();
}
public void c_mouseClicked(MouseEvent e) {
if (e.getButton() == e.BUTTON3) {
if (nowObject >= 0) {
popMenu.show(this, e.getX(), e.getY());
}
} else if (e.getModifiersEx() == e.CTRL_DOWN_MASK) {
/*
* BaseObject gh=new ObjPolygon((int)(Math.random()9+3)); int
* size=(int)(Math.random()50+50); graphics.add(gh); int
* iR=(int)(Math.random()255); int iG=(int)(Math.random()255); int
* iB=(int)(Math.random()255); gh.color=new Color(iR,iG,iB);
* gh.color2=new Color(iB,iG,iR); gh.gradientPaint=3;
* gh.stroke=stroke;
*
* gh.addPoint(e.getX(),e.getY());
* gh.changeSize(e.getX()+size,e.getY()+size); if
* ((Math.random()10)>=5) gh.filled=true; else gh.filled=false; /
*
* redraw();
*
* instance.setState("创建了一个彩蛋~~ ^_^ Copyright 2008 (c) 黑色珊瑚");
*/
}
}
public void c_mousePressed(MouseEvent e) {
if (isFilling && nowGraphics >= 0) {
if (gradientPaint == 4) {
if (p1 == null) {
p1 = new Point();
p2 = new Point();
}
p1.x = e.getX();
p1.y = e.getY();
return;
}
BaseObject gh = (BaseObject) (graphics.get(nowGraphics));
gh.color = forColor;
gh.color2 = bacColor;
gh.gradientPaint = gradientPaint;
if (gradientPaint > 0) {
if (gh.p1 == null) {
gh.p1 = new Point(gh.X.get(0), gh.Y.get(0));
gh.p2 = new Point(gh.X.get(0), gh.Y.get(0));
} else {
gh.p1.x = gh.X.get(0);
gh.p1.y = gh.Y.get(0);
gh.p2.x = gh.X.get(0);
gh.p2.y = gh.Y.get(0);
}
if (gh.X.size() == 2) {
int xB = gh.X.get(0);
int yB = gh.Y.get(0);
if (gradientPaint == 1) {
gh.p2.x += gh.X.get(1) - xB;
} else if (gradientPaint == 2) {
gh.p2.y += gh.Y.get(1) - yB;
} else if (gradientPaint == 3) {
gh.p2.x += gh.X.get(1) - xB;
gh.p2.y += gh.Y.get(1) - yB;
}
}
gh.setGradientPaint(gh.p1, gh.p2);
}
gh.fill();
redraw();
} else if (nowType == -1 || nowType == BaseObject.FILL
|| nowType == BaseObject.ROTATE) {
redraw();
if (nowChangeGraphics >= 0) {
isChanging = true;
BaseObject gh = (BaseObject) (graphics.get(nowChangeGraphics));
xOffset = e.getX() - gh.getPointX(1); // gh.getMaxX();
// //graphics
// [nowChangeGraphics
// ].getPointX(1);
yOffset = e.getY() - gh.getPointY(1);
} else if (nowGraphics >= 0) {
BaseObject gh = (BaseObject) (graphics.get(nowGraphics));
xOffset = e.getX() - gh.getMinX();
yOffset = e.getY() - gh.getMinY();
instance.setState("选中物体 " + String.valueOf(nowGraphics) + " : "
+ gh.toString());
}
} else {
if (e.getButton() == e.BUTTON1) { // 左键
if ((e.getModifiersEx() & e.CTRL_DOWN_MASK) == e.CTRL_DOWN_MASK) { // 按Ctrl键了
nowColor = midColor;
} else {
nowColor = forColor;
}
} else if (e.getButton() == e.BUTTON3) { // 右键
nowColor = bacColor;
}
BaseObject gh = createObject(e.getX(), e.getY());
isDrawing = true;
instance.setState("创建物体 " + String.valueOf(graphics.size() - 1)
+ " : " + gh.toString());
}
}
public void c_mouseReleased(MouseEvent e) {
if (isFilling && nowGraphics >= 0 && gradientPaint == 4) {
BaseObject gh = (BaseObject) (graphics.get(nowGraphics));
p2.x = e.getX();
p2.y = e.getY();
gh.color = forColor;
gh.color2 = bacColor;
gh.gradientPaint = gradientPaint;
gh.setGradientPaint(p1, p2);
gh.fill();
redraw();
} else if (isDrawing) {
isDrawing = false;
BaseObject gh = (BaseObject) (graphics.getLast());
if (e.getX() == gh.getPointX(0) && e.getY() == gh.getPointY(0)) {
graphics.removeLast(); // graphics.remove(counts);
return;
}
instance.setState("一共创建了 " + String.valueOf(graphics.size())
+ " 个物体");
} else if (isChanging) {
isChanging = false;
c_mouseMoved(e);
}
redraw();
}
public void c_mouseExited(MouseEvent e) {
if (nowGraphics < 0 && nowChangeGraphics < 0 && nowObject < 0) {
instance.setState("准备就绪");
}
}
public void c_mouseDragged(MouseEvent e) {
if (nowType == -1 || nowType == BaseObject.ROTATE) {
int x = e.getX();
int y = e.getY();
if ((e.getModifiers() & e.BUTTON1_MASK) == e.BUTTON1_MASK) { // 左键
if (nowChangeGraphics >= 0) {
BaseObject gh = (BaseObject) (graphics
.get(nowChangeGraphics));
gh.changeSize(x - xOffset, y - yOffset);
int x1 = gh.getPointX(0);
int y1 = gh.getPointY(0);
if ((x >= x1 && y >= y1) || (x <= x1 && y <= y1)) {
c.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
} else if ((x > x1 && y < y1) || (x < x1 && y > y1)) {
c.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
}
instance.setState("正在改变大小 物体 "
+ String.valueOf(nowChangeGraphics) + " : "
+ String.valueOf(gh.getWidth()) + " * "
+ String.valueOf(gh.getHeight()));
} else if (nowGraphics >= 0) {
BaseObject gh = (BaseObject) (graphics.get(nowGraphics));
int iBuffX = x - xOffset - gh.getMinX(); // .getPointX(0);
int iBuffY = y - yOffset - gh.getMinY();
gh.moveObject(iBuffX, iBuffY);
instance.setState("正在移动物体 " + String.valueOf(nowGraphics)
+ " : " + gh.toString());
} else if (nowRotate >= 0) {
BaseObject gh = (BaseObject) (graphics.get(nowRotate));
if (preX - e.getX() > 0)
gh.rotateNum += 1 * Math.PI / 180;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -