📄 paintbrush.java
字号:
createNewItem();
repaint();
drawArea.getGraphics().setColor(new Color(R, G, B));
Point seed2 = new Point(px + 1, py);
stack.push(seed1);
stack.push(seed2);
while (!stack.empty()) {
Point point;
point = (Point) stack.pop();
int tempx, tempy;
tempx = px = point.getX();
tempy = py = point.getY();
if (color.equals(drawArea.getPixleColor(tempx, tempy - 1))) {
System.out.print("a");
Point ul_point = new Point(tempx, tempy - 1);
px = tempx;
py = tempy - 1;
currentChoice = 6;
drawArea.getGraphics().drawLine(px, py, px, py);
stack.push(ul_point);
}
if (color.equals(drawArea.getPixleColor(tempx, tempy + 1))) {
System.out.print("b");
Point dl_point = new Point(tempx, tempy + 1);
px = tempx;
py = tempy + 1;
currentChoice = 6;
drawArea.getGraphics().drawLine(px, py, px, py);
stack.push(dl_point);
}
if (color.equals(drawArea.getPixleColor(tempx + 1, tempy))) {
System.out.print("c");
Point ur_point = new Point(tempx + 1, tempy);
px = tempx + 1;
py = tempy;
currentChoice = 6;
drawArea.getGraphics().drawLine(px, py, px, py);
stack.push(ur_point);
}
if (color.equals(drawArea.getPixleColor(tempx - 1, tempy))) {
System.out.print("d");
Point dr_point = new Point(tempx - 1, tempy);
px = tempx - 1;
py = tempy;
currentChoice = 6;
drawArea.getGraphics().drawLine(px, py, px, py);
stack.push(dr_point);
}
}
}
else if (currentChoice == 12 || currentChoice == 13
|| currentChoice == 14) {
xpoint[length] = event.getX();
ypoint[length] = event.getY();
length++;
polygon.addPoint(event.getX(), event.getY());
if (length != 1 && Math.abs(event.getX() - xpoint[0]) < 10
&& Math.abs(event.getY() - ypoint[0]) < 10) {
itemList[index].polygon = polygon;
length = 0;
// polygon.reset();
polygon = new java.awt.Polygon();
index++;
createNewItem();
repaint();
}
}
}
public void mouseReleased(MouseEvent event) {
checkForTriggerEvent(event);
if (currentChoice == 12 || currentChoice == 13
|| currentChoice == 14) {
repaint();
return;
}
if (currentChoice == 1 || currentChoice == 2 || currentChoice == 3
|| currentChoice == 4 || currentChoice == 5) {
itemList[index].x1 = event.getX();
itemList[index].y1 = event.getY();
}
undoStep[undoIndex++] = index;
information.setText("");
index++;
createNewItem();
repaint();
}
private void checkForTriggerEvent(MouseEvent event) {
if (event.isPopupTrigger())
popupMenu
.show(event.getComponent(), event.getX(), event.getY());
checkMenuItemEnabled();
}
}
/**
* @author 周新飞
* @version 1.0 07-6-14
* @version 2.0 07-6-18
* @see MouseB 功能:实现鼠标事件功能
*/
public class MouseB extends MouseMotionAdapter {
public void mouseDragged(MouseEvent event) {
coordinate.setText(event.getX() + "," + event.getY());
if (currentChoice == 1 || currentChoice == 2 || currentChoice == 3
|| currentChoice == 4 || currentChoice == 5) {
itemList[index - 1].x1 = itemList[index].x2 = itemList[index].x1 = event
.getX();
itemList[index - 1].y1 = itemList[index].y2 = itemList[index].y1 = event
.getY();
index++;
createNewItem();
itemList[index].x2 = itemList[index].x1 = event.getX();
itemList[index].y2 = itemList[index].y1 = event.getY();
}
if (currentChoice == 12 || currentChoice == 13
|| currentChoice == 14) {
return;
} else {
itemList[index].x2 = event.getX();
itemList[index].y2 = event.getY();
}
end_x = event.getX();
end_y = event.getY();
information.setText((end_x - start_x) + "*" + (end_y - start_y));
repaint();
}
public void mouseMoved(MouseEvent event) {
coordinate.setText(event.getX() + "," + event.getY());
}
}
/**
* author 周新飞<br>
* version 1.0 07-6-14<br>
* version 2.0 07-6-18<br>
* 功能:设置菜单是否为可用项
*/
public void checkMenuItemEnabled() {
if (undoIndex == 1) {
undoItem.setEnabled(false);
pUndoItem.setEnabled(false);
redoItem.setEnabled(true);
} else {
undoItem.setEnabled(true);
pUndoItem.setEnabled(true);
}
if (undoStep[undoIndex] == 0)
redoItem.setEnabled(false);
if (currentChoice != 7) {
textEditItem.setEnabled(false);
pTextEditItem.setEnabled(false);
} else {
textEditItem.setEnabled(true);
pTextEditItem.setEnabled(true);
}
}
/**
* @author 周新飞
* @version 1.0 07-6-12
* @see javax.swing.JPanel
* @see MyPanel 功能:设置画布
*/
class MyPanel extends JPanel {
public MyPanel() {
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new MouseA());
addMouseMotionListener(new MouseB());
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g; // 定义画笔
if (img == null) {
img = (BufferedImage) createImage(getWidth(), getHeight());
img_g2d = img.createGraphics();
img_g2d.setColor(Color.white);
img_g2d.fillRect(0, 0, getWidth(), getHeight());
img_g2d.setColor(Color.black);
}
if (img != null)
g2d.drawImage(img, 0, 0, this);
int i = 0;
while (i <= index) {
draw(g2d, itemList[i]);
i++;
}
if (currentChoice == 12 || currentChoice == 13
|| currentChoice == 14) {
g2d.drawPolyline(xpoint, ypoint, length);
}
}
public void draw(Graphics2D g2d, Drawings a) {
a.draw(g2d); // 将画笔传入到各个子类中,用来完成各自的绘图
}
public Dimension getPreferredSize() {// 设置窗口大小
return new Dimension(500, 400);
}
public Color getPixleColor(int x, int y) {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException event) {
event.printStackTrace();
}
return robot.getPixelColor(drawArea.getLocationOnScreen().x + x,
drawArea.getLocationOnScreen().y + y);// 对坐标进行调整
}
}
/**
* author 周新飞, 王宇新,胡如兴<br>
* version 1.0 07-6-12<br>
* 选择所画类型并调用画图类进行画图
*/
public void createNewItem() {
if (currentChoice == 7)// 进行相应的游标设置
drawArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
else
drawArea.setCursor(Cursor
.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
switch (currentChoice) {
case 1:
itemList[index] = new Pen();
break;
case 2:
itemList[index] = new Brush();
break;
case 3:
itemList[index] = new RightBrush();
break;
case 4:
itemList[index] = new LeftBrush();
break;
case 5:
itemList[index] = new Eraser();
break;
case 6:
itemList[index] = new Spray();
break;
case 7:
itemList[index] = new Character();
break;
case 8:
itemList[index] = new Line();
break;
case 9:
itemList[index] = new Rectangle();
break;
case 10:
itemList[index] = new EmptyRectangle();
break;
case 11:
itemList[index] = new SolidRectangle();
break;
case 12:
itemList[index] = new Polygon();
break;
case 13:
itemList[index] = new EmptyPolygon();
break;
case 14:
itemList[index] = new SolidPolygon();
break;
case 15:
itemList[index] = new Ellipse();
break;
case 16:
itemList[index] = new EmptyEllipse();
break;
case 17:
itemList[index] = new SolidEllipse();
break;
case 18:
itemList[index] = new Roundrectangle();
break;
case 19:
itemList[index] = new EmptyRoundrectangle();
break;
case 20:
itemList[index] = new SolidRoundrectangle();
break;
}
itemList[index].type = currentChoice;// 保存
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
itemList[index].stroke = stroke1;
}
/**
* @author 王宇新, 王德恩
* @version 1.0 07-6-12
* @see Drawings 功能:定义画图的基元-点
*/
class Drawings implements Serializable {
int x1, y1, x2, y2; // 定义坐标属性
int R, G, B; // 定义色彩属性
float stroke; // 定义线条粗细属性
int type; // 定义字体属性
String fonts; // 定义字体风格属性
String string;
java.awt.Polygon polygon;
Drawings() {
this.polygon = new java.awt.Polygon();
polygon.addPoint(0, 0);
}
void draw(Graphics2D g2d) {
};// 定义绘图函数
}
/**
* @author 王宇新, 王德恩
* @version 1.0 07-6-14
* @see Pen 功能:定义铅笔类实现铅笔画功能
*/
class Pen extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1, y1, x2, y2);
}
}
/**
* @author 王宇新, 王德恩
* @version 1.0 07-6-14
* @see Brush 功能:定义普通画刷类功能,效果同铅笔画
*/
class Brush extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
g2d.drawLine(x1, y1, x2, y2);
}
}
/**
* @author 王宇新, 王德恩,胡如兴
* @version 1.0 07-6-14
* @see RightBrush 功能:定义右画刷类实现铅笔画-右粗左细 方法:定义每一点为右斜45度的细直线,然后再以这样的点画直线
*/
class RightBrush extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER));
g2d.drawLine((int) (x1 + Math.sqrt(2) * stroke / 4),
(int) (y1 - Math.sqrt(2) * stroke / 4), (int) (x2 - Math
.sqrt(2)
* stroke / 4), (int) (y2 + Math.sqrt(2) * stroke
/ 4));
g2d.drawLine(x1, y1, (int) (x2 + Math.sqrt(2) * stroke / 4),
(int) (y2 - Math.sqrt(2) * stroke / 4));
g2d.drawLine((int) (x2 - Math.sqrt(2) * stroke / 4),
(int) (y2 + Math.sqrt(2) * stroke / 4), x1, y1);
}
}
/**
* @author 王宇新, 王德恩,胡如兴
* @version 1.0 07-6-14
* @see LeftBrush 功能:定义左画刷类实现左画刷-右细左粗 方法:定义每一点为左斜45度的细直线,然后再以这样的点画直线
*/
class LeftBrush extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R, G, B));
g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER));
g2d.drawLine((int) (x1 - Math.sqrt(2) * stroke / 4),
(int) (y1 - Math.sqrt(2) * stroke / 4), (int) (x2 + Math
.sqrt(2)
* stroke / 4), (int) (y2 + Math.sqrt(2) * stroke
/ 4));
g2d.drawLine(x1, y1, (int) (x2 - Math.sqrt(2) * stroke / 4),
(int) (y2 - Math.sqrt(2) * stroke / 4));
g2d.drawLine((int) (x2 + Math.sqrt(2) * stroke / 4),
(int) (y2 + Math.sqrt(2) * stroke / 4), x1, y1);
}
}
/**
* @author 王宇新, 王德恩
* @version 1.0 07-6-14
* @see Eraser 功能:定义橡皮类实现橡皮擦功能 方法: 定义画白色粗直线
*/
class Eraser extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(255, 255, 255));
g2d.setStroke(new BasicStroke((stroke + 4), BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL));
g2d.drawLine(x1, y1, x2, y2);
}
}
/**
* @author 周新飞
* @version 1.0 07-6-14,@version 2.0 07-6-18
* @see Spray 功能:定义喷枪类实现喷枪功能
* 方法:先把当前点设置成要涂的颜色,压栈而后出栈与周围点颜色比较,如果相同不压栈,否则先涂色后压栈
*/
class Spray extends Drawings {
void draw(Graphics2D g2d) {
g2d.setPaint(new Color(R, G, B));
System.out.println(new Color(R, G, B));
g2d.setStroke(new BasicStroke(stroke));
// g2d.drawLine(px,py,px,py);
Point seed1 = new Point(x1, y1);
Color color = drawArea.getPixleColor(x1, y1);
Stack stack = new Stack();
stack.push(seed1);
// stack.push(seed2);
/*
* while(!stack.empty()){ Point point;
*
* point = (Point) stack.pop(); int tempx, tempy; tempx = px =
* point.getX(); tempy = py = point.getY();
*
* if (color.equals(drawArea.getPixleColor(tempx, tempy - 1))) {
* System.out.print("a"); Point ul_point = new Point(tempx, tempy -
* 1); px = tempx; py = tempy - 1; // currentChoice = 6;
*
* g2d.fillRect(x1, y1, 10, 10); //System.out.print(g2d.getColor());
* //System.out.println(drawArea.getPixleColor(x1 + 5, y1 + 5));
* System.out.println(drawArea.getPixleColor(tempx,tempy+1));
*
* stack.push(ul_point); } if
* (color.equals(drawArea.getPixleColor(tempx, tempy + 1))) {
* System.out.print("b"); Point dl_point = new Point(tempx, tempy +
* 1);
*
* px = tempx; py = tempy + 1; // currentChoice = 6;
*
* g2d.drawLine(px, py, px, py);
*
* stack.push(dl_point); } if
* (color.equals(drawArea.getPixleColor(tempx + 1, tempy))) {
* System.out.print("c"); Point ur_point = new Point(tempx + 1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -