程序功能在Applet上使用鼠標在屏幕上隨意畫直線和畫點_第1頁
程序功能在Applet上使用鼠標在屏幕上隨意畫直線和畫點_第2頁
程序功能在Applet上使用鼠標在屏幕上隨意畫直線和畫點_第3頁
程序功能在Applet上使用鼠標在屏幕上隨意畫直線和畫點_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、1 程序功能:在Applet上使用鼠標在屏幕上隨意畫直線和畫點。用戶點擊“畫線”按鈕可畫直線,點擊“畫點”按鈕可畫連續(xù)點,點擊“清除”按鈕可清除畫面上的所有內容。 2 編寫LX8_9.java程序文件,源代碼如下。Button line, point, clear; MyCanvas2 c; public void init() c=new MyCanvas2(); c.setSize(350,200); c.setBackground(Color.green); line=new Button("畫線"); point=new Button("畫點")

2、; clear=new Button("清除"); add(line); add(point); add(clear); add(c); line.addActionListener(this); point.addActionListener(this); clear.addActionListener(this); public void actionPerformed(ActionEvent e) if (e.getSource()=line) / 設為畫直線模式 c.mode=0; else if (e.getSource()=point) / 設為畫連續(xù)點模式 c

3、.mode=1; else if (e.getSource()=clear) / 清除畫面 c.points=new Vector(); c.x1= -1; c.repaint(); class MyCanvas2 extends Canvas implements MouseListener,MouseMotionListener int x1, y1, x2, y2, mode; Vector points=new Vector(); MyCanvas2() addMouseListener(this); addMouseMotionListener(this); public void

4、paint(Graphics g) for (int i=0;i<points.size();i+) / 所有操作結果被重新畫出 Rectangle r=(Rectangle)points.elementAt(i); g.drawLine(r.x, r.y, r.width, r.height); if (x1!= -1 && mode =0) /畫當前直線 g.drawLine(x1, y1, x2, y2); public void mousePressed(MouseEvent e) / 記錄起點坐標 x1=e.getX(); y1=e.getY(); public

5、 void mouseDragged(MouseEvent e) if (mode=0) / 記錄當前坐標 x2=e.getX(); y2=e.getY(); else / 畫連續(xù)點時保存每一個筆畫的起點和當前坐標 points.addElement(new Rectangle(x1, y1, e.getX(), e.getY(); x1=e.getX(); y1=e.getY(); repaint(); public void mouseReleased(MouseEvent e) if (mode=0) / 保存當前直線的起點和終點坐標 points.addElement(new Rectangle(x1, y1, e.getX(), e.getY(); public void mouseClicked(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseMoved(MouseEvent e) 3 編譯源程

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論