java程式實現滑鼠繪圖

來源:互聯網
上載者:User

標籤:

 1 import java.awt.*; 2  3 import javax.swing.*; 4 class Gstudy extends JFrame{ 5      6     private int x1,y1,x2,y2; 7     private newPanel panel=new newPanel(); 8     private JButton btn=new JButton("清除"); 9     public Gstudy()10     {11         setTitle("互動式繪圖");12         setBounds(10,10,750,700);13         btn.addActionListener(new ClearList());14         panel.add(btn,BorderLayout.SOUTH);15         add(panel);16     }17     public static void main(String[] args)18     {19         Gstudy frm=new Gstudy();20         frm.setVisible(true);21         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);22     }23     private class ClearList  implements ActionListener24     {25         @Override26         public void actionPerformed(ActionEvent e) {27             28             panel.paintComponent(panel.getGraphics());29         }30     }31     private class MyMouseList extends MouseAdapter32     {33         public void mousePressed(MouseEvent e)34         {35             x1=e.getX();36             y1=e.getY();37         }38     }39     private class MyMouseMotionList extends MouseMotionAdapter40     {41         public void mouseDragged(MouseEvent e)42         {43             x2=e.getX();44             y2=e.getY();45             Graphics g=panel.getGraphics();46             g.drawLine(x1, y1, x2, y2);47             x1=x2;48             y1=y2;49         }50     }51     private class newPanel extends JPanel52     {53         54         public newPanel()55         {56             this.addMouseMotionListener(new MyMouseMotionList());57             this.addMouseListener(new MyMouseList());58         }59         @Override60         protected void paintComponent(Graphics g) {61             // TODO Auto-generated method stub62             super.paintComponent(g);63         }64         65     }

 

java程式實現滑鼠繪圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.