<JAVA>paint(Graphics g)與Graphics g=getGraphics()的兩種畫筆擷取

來源:互聯網
上載者:User

標籤:his   int()   tst   tco   容器   TE   AC   UI   擷取   

 1.  paint(Graphics g)

    java繪圖時,最常使用到的就是paint(Graphics g){...內容...}方法擷取畫筆,然後利用JPanel等容器作為畫布,在JFrame內呈現出內容,很多情況下這種方式都還是很實用,下附執行個體:

import java.awt.Color;  import java.awt.Graphics;  import javax.swing.JFrame;  import javax.swing.JPanel;    public class Test //Test為自訂的主類名,一般大寫  {  public static void main(String args[]) {  JFrame newFrame=new JFrame("funBox");         newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //定義JFrame關閉時的操作(必需),有效避免不能關閉後台當前框體進程的問題      newFrame.setSize(400, 400);      //定義JFrame的相關屬性  newFrame.setLocation(200, 200);  newFrame.setVisible(true);   newFrame.add(new FangKuai());     //將需要呈現的映像添加進JFrame中  }  }    class FangKuai  extends JPanel //FangKuai  為自訂建立類名  {        public void paint(Graphics g)  //重寫實現panit()方法  {  g.setColor(Color.green);   //(0,0)位置繪製一個20*20的綠色方塊  g.fillRect(0, 0, 20, 20);  }  }

  2.  Graphics g=getGraphics()

   有的時候,需要Graphics對象進行更多的操作(例如下面需要在run()中調用Graphics對象)而不能使用paint(Graphics g)方法,這個時候,就得擷取自己定義的Graphics對象來完成需求,下附執行個體

import java.applet.Applet;  import java.awt.Color;  import java.awt.Graphics;  import javax.swing.JFrame;  import javax.swing.JPanel;    //以下為一個框體小程式  public class _001// _001為自訂的主類名   {  public static void main(String[] args) {  JFrame newFrame=new JFrame("funBox");  newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          //定義JFrame關閉時的操作(必需),有效避免不能關閉後台當前框體進程的問題     newFrame.setSize(400, 400);         //定義JFrame的相關屬性  newFrame.setLocation(200, 200);  newFrame.setVisible(true);   newThread n1= new newThread();           //線程的運行,將需要呈現的映像添加進JFrame中  newFrame.add(n1);  Thread t1 = new Thread(n1);  t1.start();    }  }    class newThread extends JPanel implements Runnable //Java類中只能繼承一個類,但是可以實現多個介面,此處newThread 為自訂建立類名  {  Graphics g;    //此處定義Graphics對象 g;  private static final long serialVersionUID = 1L;     public void run()   //進程run()方法重寫  {    g=getGraphics();   //Graphics對象 g的擷取  for(int i=0;i<100;)  {  try {  Thread.sleep(2000);  } catch (InterruptedException e) {  // TODO Auto-generated catch block  e.printStackTrace();  }    this.update(g) ;       //update()方法的調用,重新整理映像,使得映像不會重疊顯現     g.setColor(Color.green);   //繪製(0,0)開始移動的20*20綠色小塊  g.fillRect(i, i, 20, 20);  i+=20;  }   }  } 

 3.  總結

    簡單的調用repaint()方法確實可以完成很多java繪圖需求,但是當需要靈活得到Graphics對象時,就必須用getGraphics()擷取了,自己Java繪圖的一點小小經驗,希望得到更多點評得以改進。

<JAVA>paint(Graphics g)與Graphics g=getGraphics()的兩種畫筆擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.