📄 drawingboard.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import javax.swing.*;
import java.util.ArrayList;
public class DrawingBoard extends JLabel implements MouseListener,
MouseMotionListener, Printable {
public static final int TOOL_LINE = 0;
public static final int TOOL_RECT = 1;
public static final int TOOL_OVAL = 2;
public static final int TOOL_DIAMOND = 3;
public static final int TOOL_PENCIL = 4;
public static final int TOOL_ERASER = 5;
public static final int TOOL_POLYGON = 6;
public static final int TOOL_CLIPRECT = 7;
public static final int TOOL_TEXT = 8;
public static final int TOOL_ROUNDRECT = 9;
public static final int TOOL_BRUSH = 10;
public static final int TOOL_PENQIANG = 11;
public static final int TOOL_QUSHE = 12;
public static final int TOOL_CURVE = 13;
public static final int TOOL_FANGDAJING = 14;
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 Stroke[] ERASER_STROKES = new Stroke[] {
new BasicStroke(15.0f), new BasicStroke(20.0f),
new BasicStroke(30.0f), new BasicStroke(50.0f),
new BasicStroke(100.0f) };
private ArrayList shapes;
private IShape currentShape;
private static int tool;
protected static int strokeIndex, eraserIndex;
@SuppressWarnings("unchecked")
public DrawingBoard() {
shapes = new ArrayList();
tool = TOOL_LINE;
currentShape = null;
strokeIndex = 0;
eraserIndex = 0;
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
setOpaque(true);
setForeground(Color.black);
setBackground(Color.white);
addMouseListener(this);
addMouseMotionListener(this);
}
public void setTool(int t) {
if (t < TOOL_LINE || t > TOOL_FANGDAJING)
throw new IllegalArgumentException("Invaild Tool Specified!");
tool = t;
}
public void clearBoard() {
shapes.clear();
repaint();
}
public String getShapes() {
int size = shapes.size();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < size; i++) {
IShape shape = (IShape) shapes.get(i);
buffer.append("\n");
buffer.append(shape.getClass().getName());
buffer.append("\t");
buffer.append(shape.getShapeData());
}
return buffer.toString();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int size = shapes.size();
Graphics2D g2d = (Graphics2D) g;
repaint();
if(picture!=null){
setIcon(new ImageIcon(picture));
}
for (int i = 0; i < size; i++) {
((IShape) shapes.get(i)).draw(g2d);
}
}
public int print(Graphics g, PageFormat pf, int page) {
return Printable.PAGE_EXISTS;
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
switch (tool) {
case TOOL_LINE:
Panel.judgmentDraw = true;
currentShape = new Line(Panel.shapeColor, STROKES[strokeIndex],
e.getX(), e.getY());
break;
case TOOL_RECT:
Panel.judgmentDraw = true;
currentShape = new Rect(Panel.shapeColor, STROKES[strokeIndex],
e.getX(), e.getY());
break;
case TOOL_OVAL:
Panel.judgmentDraw = true;
currentShape = new Oval(Panel.shapeColor, STROKES[strokeIndex],
e.getX(), e.getY());
break;
case TOOL_DIAMOND:
Panel.judgmentDraw = true;
currentShape = new Diamond(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_PENCIL:
Panel.judgmentDraw = true;
currentShape = new PolyLine(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_ERASER:
currentShape = new Eraser(this, ERASER_STROKES[eraserIndex], e
.getX(), e.getY());
break;
case TOOL_POLYGON:
Panel.judgmentDraw = true;
currentShape = new PolyGon(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_CLIPRECT:
currentShape = new ClipRect(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_TEXT:
currentShape = new Text(Panel.shapeColor,
MenuEventClass.drawBoarFont, e.getX(), e.getY());
break;
case TOOL_ROUNDRECT:
Panel.judgmentDraw = true;
currentShape = new RoundRect(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_BRUSH:
Panel.judgmentDraw = true;
currentShape = new Brush(Panel.shapeColor,
StrokeComponent.stroke, e.getX(), e.getY());
break;
case TOOL_PENQIANG:
Panel.judgmentDraw = true;
currentShape = new PenQiang(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_QUSHE:
Panel.judgmentDraw = true;
currentShape = new QuShe(Panel.shapeColor,
MenuEventClass.drawBoarFont, e.getX(), e.getY());
break;
case TOOL_CURVE:
Panel.judgmentDraw = true;
currentShape = new Curce(Panel.shapeColor,
STROKES[strokeIndex], e.getX(), e.getY());
break;
case TOOL_FANGDAJING:
MenuEventClass.isFDOrSX=true;
if (MenuEventClass.isFDOrSX) {
GraphicsEnvironment environment = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice screen = environment
.getDefaultScreenDevice();
try {
Robot robot = new Robot(screen);
FrameDesign.run(robot);
} catch (AWTException k) {
k.printStackTrace();
}
}
Panel.drawCanvas.clearBoard();
picture = FrameDesign.image;
MenuEventClass.scale(picture, 2, MenuEventClass.isFDOrSX);
break;
}
shapes.add(currentShape);
repaint();
} else if (e.getButton() == MouseEvent.BUTTON3 && currentShape != null) {
currentShape.processCursorEvent(e, IShape.RIGHT_PRESSED);
repaint();
}
}
public void mouseDragged(MouseEvent e) {
if (currentShape != null) {
currentShape.processCursorEvent(e, IShape.CURSOR_DRAGGED);
ctrlx = e.getX();
ctrly = e.getY();
repaint();
}
}
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && currentShape != null) {
currentShape.processCursorEvent(e, IShape.LEFT_RELEASED);
currentShape = null;
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
protected static Image picture = null;
protected static int mouseX;
protected static int mouseY;
protected static boolean quShe = false;
protected static int ctrlx;
protected static int ctrly;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -