📄 drawpanel.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.awt.Color;import java.awt.Paint;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Stroke;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import javax.swing.JLabel;import javax.swing.JPanel;/** * * @author Diallo */public class DrawPanel extends JPanel{ MyShape shape[]; private int shapeCount=0; private int shapeType; private MyShape currentShape; private Paint currentColor; private boolean filledShape; private JLabel statusLabel; private Stroke currentStroke; @Override public void paintComponent(Graphics g){ Graphics2D g2=(Graphics2D) g; super.paintComponent(g); for(int i=0;i<shapeCount;i++){ currentShape=shape[i]; if(currentShape instanceof MyOval){ MyOval oval=(MyOval) currentShape; oval.draw(g2); }else if(currentShape instanceof MyRectangle){ MyRectangle rect=(MyRectangle) currentShape; rect.draw(g2); }else if(currentShape instanceof MyLine){ MyLine line=(MyLine) currentShape; line.draw(g2); } } } public void setShapeType(int shapeType){ this.shapeType=shapeType; } public void setStroke(Stroke stroke){ this.currentStroke=stroke; } public void setCurrentColor(Paint currentColor){ this.currentColor=currentColor; } public void setFilledShape(boolean filledShape){ this.filledShape=filledShape; } public void clearLastShape(){ shapeCount=(shapeCount>0)?((shapeCount==1)?(shapeCount-1):(shapeCount-2)):(shapeCount=0); repaint(); } public void clearDrawing(){ shapeCount=0; repaint(); } private class MouseClickHandler extends MouseAdapter implements MouseMotionListener{ @Override public void mousePressed(MouseEvent event){ if(shapeType==2){ currentShape=new MyRectangle(event.getX(),event.getY(),event.getX(),event.getY(), currentColor,currentStroke,filledShape); }else if(shapeType==1){ currentShape=new MyOval(event.getX(),event.getY(),event.getX(),event.getY(), currentColor,currentStroke,filledShape); }else if(shapeType==0){ currentShape=new MyLine(event.getX(),event.getY(),event.getX(),event.getY(),currentColor,currentStroke); } } @Override public void mouseReleased(MouseEvent event){ currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); shape[shapeCount++]=currentShape; currentShape=null; repaint(); } @Override public void mouseMoved(MouseEvent event){ statusLabel.setText("("+event.getX()+","+event.getY()+")"); } @Override public void mouseDragged(MouseEvent event){ currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); repaint(); statusLabel.setText("("+event.getX()+","+event.getY()+")"); } } public DrawPanel(JLabel statusBar){ statusLabel=statusBar; shape=new MyShape[100]; shapeCount=0; shapeType=0; currentShape=null; currentColor=Color.BLACK; setBackground(Color.WHITE); setVisible(true); addMouseListener(new MouseClickHandler()); addMouseMotionListener(new MouseClickHandler()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -