📄 drawingboard.java
字号:
/* * DrawingBoard.java * * Created on April 29, 2005, 7:13 PM */package flow.graph.gui.graph.item.unit;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.ArrayList;/** * * @author hysun */public class DrawingBoard extends JPanel implements MouseListener, MouseMotionListener { 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 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 int tool; private int strokeIndex, eraserIndex; public DrawingBoard() { shapes = new ArrayList(); tool = TOOL_LINE; currentShape = null; strokeIndex = 0; eraserIndex = 0; setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); setOpaque(true); setForeground(Color.black); setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); } public void setTool(int t) { if (t < TOOL_LINE || t > TOOL_POLYGON) throw new IllegalArgumentException("Invaild Tool Specified!"); tool = t; } 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(); 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(); } public void setShapes(ArrayList list) throws Exception { try { int size = list.size(); for (int i=0; i<size; i++) { String[] split2 = ((String) list.get(i)).split("\t"); IShape shape; if (split2[0].equals("hysun.draw.Line")) { shape = new Line(); } else if (split2[0].equals("hysun.draw.Rect")) { shape = new Rect(); } else if (split2[0].equals("hysun.draw.Oval")) { shape = new Oval(); } else if (split2[0].equals("hysun.draw.Diamond")) { shape = new Diamond(); } else if (split2[0].equals("hysun.draw.PolyLine")) { shape = new PolyLine(); } else if (split2[0].equals("hysun.draw.Eraser")) { shape = new Eraser(this); } else if (split2[0].equals("hysun.draw.PolyGon")) { shape = new PolyGon(); } else { throw new Exception("Invalid Shape Data!"); } shape.setShapeData(split2[1]); list.set(i, shape); } shapes = list; repaint(); } catch (Exception e) { e.printStackTrace(); throw e; } } protected void paintComponent(Graphics g) { super.paintComponent(g); int size = shapes.size(); Graphics2D g2d = (Graphics2D) g; for (int i=0; i<size; i++) { ((IShape) shapes.get(i)).draw(g2d); } } public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { switch (tool) { case TOOL_LINE: currentShape = new Line(this.getForeground(), STROKES[strokeIndex], e.getX(), e.getY()); break; case TOOL_RECT: currentShape = new Rect(this.getForeground(), STROKES[strokeIndex], e.getX(), e.getY()); break; case TOOL_OVAL: currentShape = new Oval(this.getForeground(), STROKES[strokeIndex], e.getX(), e.getY()); break; case TOOL_DIAMOND: currentShape = new Diamond(this.getForeground(), STROKES[strokeIndex], e.getX(), e.getY()); break; case TOOL_PENCIL: currentShape = new PolyLine(this.getForeground(), 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: currentShape = new PolyGon(this.getForeground(), STROKES[strokeIndex], e.getX(), e.getY()); 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); 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) {} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -