import javax。swing。*;import t。*;import t。event。*;public class HuaTu extends JPanel implements MouseListener, MouseMotionListener, ActionListener{ private JFrame f; private int xBegin=0,yBegin=0,xEnd=0,yEnd=0;//开始和结束点坐标 private JButton butLine,butRect,butOval,butPen,butEraser;//线,方,圆,笔,橡皮 private JToolBar tb; public HuaTu() { f = new JFrame("画图板"); butLine = new JButton("\"); butRect = new JButton("□"); butOval = new JButton("○"); butPen = new JButton("Pen"); butEraser = new JButton("Eraser"); tb = new JToolBar(); d(butLine); d(butRect); d(butOval); d(butPen); d(butEraser); dActionListener(this); dActionListener(this); dActionListener(this); dActionListener(this); dActionListener(this); tLayout(new BorderLayout()); d(tb,BorderLayout。
NORTH); dMouseListener(this); dMouseMotionListener(this); d(this); tBounds(100,100,600,500); tVisible(true); } public void paintComponent(Graphics g) { //手工强制清空面板 intComponent(g); g。
drawLine(xBegin,yBegin,xEnd,yEnd); } public static void main(String args[]) { new HuaTu(); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) {//按下鼠标的时候,记录开始点的坐标 this。
xBegin = tX(); this。yBegin = tY(); } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) {//记录拖动的时候的结束点 this。
xEnd = tX(); this。yEnd = e。
getY(); //每次拖动,都需要重绘面板,显示最的图形 paint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } /** * Method actionPerformed * * * @param e * */ public void actionPerformed(ActionEvent e) { // TODO: Add your code here }}。
全部