多媒体包括绘图_第1页
多媒体包括绘图_第2页
多媒体包括绘图_第3页
多媒体包括绘图_第4页
多媒体包括绘图_第5页
已阅读5页,还剩62页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、多媒體 多媒體包括繪圖 (graphics) 、影像 (image)聲音 (sound) 、,以及視聽 (video) 等。 Graphics類別 Graphics 為抽象類別,是所有繪圖圖形或字型的 基礎類別,它允許您在元件上製作圖形或字型 Graphics方法 當 applet 程式 啟動時執行完 init()、start() 之後,會自動執行 paint() ,後來若要更改畫面必須產生事件,例如改變 applet 視窗大小等。若要強迫執行 paint() ,您可執行 repaint() ,它會先執行 update() 清除畫面之後再呼 叫 paint() 。 paint() 要在容器上繪

2、圖,在您的程式裡頭必須實作一個 paint() ,並且提供一個 Graphics 類別的物件供繪圖之用,repaint(), update() public void repaint() 本元件重新繪圖。 public void repaint(int x, int y, int width, int height) 本元件重新繪圖。(x,y) 左上角座標,width 寬,height 高。 public void repaint(long tm) 本元件重新繪圖,在 tm 毫秒之內。 public void repaint(long tm, int x, int y, int width,

3、int height) 本元件重新繪圖。(x,y) 左上角座標,width 寬,height 高, 在 tm 毫秒之內。 public void update(Graphics g) 以 g 物件更新本元件。 repain事件處理模式範例這個程式是有一個Button,Button上面寫著請按我,下面有一行字”Button not yet clicked”,當我們按下去之後,下面的字串就會改變成”Button clicked”:/TestButton.javaimport java.applet.Applet;import java.awt.*;import java.awt.event.*;p

4、ublic class TestButtom extends Applet implements ActionListener private Button btn; private String str; public void init() str = new String(Button not yet clicked.);btn = new Button(按下我按下我);btn.addActionListener(this);add(btn); public void actionPerformed(ActionEvent ae) str = Button clicked.; repai

5、nt(); public void paint(Graphics g) g.drawString(str,50,50); 可以使用下列三個建構子,自己建立一個顏色物件 public Color(float r, float g, float b) 建立一個新的 r 紅 g 綠 b 藍所構成的 Color 物件。r、g、b 介於 0.0 (含) 與 1.0 (含) 之間。 public Color(int rgb) 建立一個新的 rgb 所構成的 Color 物件。rgb 第 0-7 位元表藍色, 第 8-15 位元表綠色, 第 16-23 位 元表紅色。 public Color(int r,

6、 int g, int b) 建立一個新的 r 紅 g 綠 b 藍所構成的 Color 物件。r、g、b 介於 0 (含) 與 255 (含) 之間。 設定顏色 public final static Color black = new Color(0,0,0); public final static Color blue = new Color(0,0,255); public final static Color cyan = new Color(0,255,255); public final static Color darkGray = new Color(64,64,64); p

7、ublic final static Color gray = new Color(128,128,128); public final static Color green = new Color(0,255,0); public final static Color lightGray = new Color(192,192,192); public final static Color magenta = new Color(255,0,255); public final static Color orange = new Color(255,200,0); public final

8、static Color pink = new Color(255,175,175); public final static Color red = new Color(255,0,0); public final static Color white = new Color(255,255,255); public final static Color yellow = new Color(255,255,0); 在 Color 類別中有下列方法可取得顏色的成分 public int getBlue() 傳回藍色成分,介於 0-255 間。 public int getGreen() 傳回

9、綠色成分,介於 0-255 間。 public int getRed() 傳回紅色成分,介於 0-255 間。 public int getRGB() 傳回紅綠藍色成分,第 0-7 位元表藍色,第 8-15 位 元表綠色,第 16-23 位元表紅色。下列兩個抽象方法 getColor() 及 setColor() 屬於 Graphics 類別,它的衍生類別必須實作這兩個方法。 public abstract void setColor(Color c) 設定顏色為 c。 public abstract Color getColor() 傳回 Color 物件。 【程式MyWindow3.jav

10、a】 import java.awt.*; import javax.swing.*; public class MyWindow3 extends JFrame public MyWindow3() / Constructor super(底色為藍色的視窗底色為藍色的視窗); setBounds(50, 100, 400, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.blue); public void paint(Graphics g) g.setFon

11、t(new Font(Serif, Font.BOLD, 32); g.setColor(Color.RED); g.drawString(我喜歡我喜歡Java!, 100,100); public static void main(String args) MyWindow3 frm = new MyWindow3(); frm.setVisible(true); 【執行結果】 MyColor.java import java.awt.*; import javax.swing.*; public class MyColor extends JFrame public MyColor() s

12、uper(MyColor: Using colors); setSize(300, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); public void paint( Graphics g ) Color color = Color.black; g.setColor(color); g.fillRect(25, 45, 100, 20); g.drawString(RGB值值: + color.getRed() + (紅紅), + color.getGreen() + (綠綠), + color.

13、getBlue()+(藍藍), , 130,60); public static void main( String args ) MyColor myColor = new MyColor(); MyColor2.java 使用 JColorChooser 元件讓您挑選一 個顏色,並將該顏色設為版面的底色。您在 改變背景顏色 的 按鈕上按一下滑鼠左鍵,產生一個按鈕事件,接著就執行 actionPerformed() 方法。 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyColo

14、r2 extends JFrame implements ActionListener private JButton changeColor; private Color color = Color.lightGray; private Container c; public MyColor2() super(MyColor2: 使用使用JColorChooser元件元件); c = getContentPane(); c.setLayout(new FlowLayout(); changeColor = new JButton(改變背景顏色改變背景顏色); changeColor.addA

15、ctionListener(this); c.add( changeColor ); setSize(300, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); public void actionPerformed(ActionEvent e) color = JColorChooser.showDialog(this, 選色選色, color); if (color = null) color = Color.lightGray; c.setBackground(color); c.repaint(

16、); public static void main( String args ) MyColor2 app = new MyColor2(); 【執行結果】 繪線及方圓 public abstract void drawLine(int x1, int y1, int x2, int y2) 以起點座標 (x1,y1) 至終點 (x2,y2) 畫直線。 public void drawRect(int x, int y, int width, int height) 畫一個長方形。左上角座標為 (x,y),寬為 width,高為 height。 public void fillRect(in

17、t x, int y, int width, int height) 畫並填滿一個長方形。左上角座標為 (x,y),寬為 width, 高為 height。 public abstract void drawOval(int x, int y, int width, int height) 畫一個橢圓。中心座標為 (x,y),長徑(寬)為 width,短徑 (高)為 height。若長短徑值相同即為圓。 public abstract void fillOval(int x, int y, int width, int height) 畫並填滿一個橢圓。中心座標為 (x,y),長徑(寬)為 w

18、idth ,短徑(高)為 height。若長短徑值相同即為圓。 程式MyLine.java】 import java.awt.*; import javax.swing.*; public class MyLine extends JFrame private String s = Using drawString!; public MyLine() super(“MyLine: 繪線條繪線條,矩形矩形,橢圓橢圓”); setSize(400, 170); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); pub

19、lic void paint(Graphics g) g.setColor(Color.black); g.drawLine(220, 30, 350, 30); /水平線水平線 g.drawLine(220, 30, 220, 160); /垂直線垂直線 g.drawLine(220, 30, 350, 160); /斜線斜線 g.drawRect(20, 40, 90, 55 ); /矩形矩形(空心空心) g.fillRect(120, 40, 90, 55 ); /矩形矩形(實心實心) g.drawOval(20, 100, 90, 55 ); /橢圓橢圓(空心空心) g.fillOva

20、l(120, 100, 90, 55 ); /橢圓橢圓(實心實心) public static void main( String args ) MyLine app = new MyLine(); 【執行結果】 繪弧 在 Graphics 類別提供下列兩個繪弧方法 drawArc() 及 fillArc()。弧形是圓或橢圓形的一部分,以時鐘三點鐘方向為 基準 0 度,反時鐘方向為正,順時鐘方向為負。 public abstract void fillArc(int x, int y, nt width, int height, int startAngle, int arcAngle) 畫並

21、填滿一個弧形。 中心座標為 (x,y), 長徑 (寬) 為 width, 短徑 (高) 為 height。 起角為 startAngle 度, 角度為 arcAngle 度, 0 度為 3 點鐘位置, 反時鐘方向 為正, 順時鐘方向為負。【程式MyArc.java】 import java.awt.*; import javax.swing.*; public class MyArc extends JFrame public MyArc() super(MyArc: Drawing Arcs ); setSize(260, 120); setDefaultCloseOperation(JFr

22、ame.EXIT_ON_CLOSE); setVisible(true); public void paint( Graphics g ) g.drawArc(15, 60, 80, 40, 0, 360); /0至至360度度 g.fillArc(100, 60, 80, 40, 270, -90); /270至至-90度度 g.drawArc(145, 60, 80, 40, 0, -270 ); /0至至-270度度 public static void main( String args ) MyArc app = new MyArc(); 【執行結果】 繪多邊形 public abs

23、tract void drawPolygon(int xPoints, int yPoints, int nPoints) 畫一個封閉多邊形。由 nPoints 點所構成,其座標存於陣 列 xPoints 及 yPoints 中。 public abstract void drawPolygon(Polygon p) 以 Polygon 類別的物件 p 畫一個封閉多邊形。 public abstract void drawPolyline(int xPoints, int yPoints, int nPoints); 以點陣列資料畫一個多邊線條。 public abstract void fi

24、llPolygon(int xPoints, int yPoints, int nPoints) 畫並填滿一個封閉多邊形。由 nPoints 點所構成,其座標 存於陣列 xPoints 及 yPoints 中。 public abstract void fillPolygon(Polygon p) 以 Polygon 類別的物件 p 畫並填滿一個封閉多邊形。 多邊形類別 Polygon 包含一系列的點座標 (x,y),相鄰兩 點所構成的邊,為多邊形的一邊。 Polygon 類別其常用的方法說明如下: public Polygon() 建構子。建立一個空白的 Polygon 類別物件。 publ

25、ic Polygon(int xpoints, int ypoints, int npoints) 建構子。建立一個 npoints 個頂點的 Polygon 類別物件。 其 i 頂點座標為 (xpointsi, ypointsi)。 public void addPoint(int x, int y) 附加一點 (x,y) 至本多邊形物件。 public boolean contains(int x, int y) 若 (x,y) 含於本物件則傳回 true,否則傳回 false。 public Rectangle getBounds() 傳回包含本多邊形物件的最小長方形。 public v

26、oid translate(int deltaX, int deltaY) 沿著 x 軸移動 deltaX,沿著 y 軸移動 deltaY 像點。 【程式MyPolygon.java】 import java.awt.*; import javax.swing.*; public class MyPolygon extends JFrame public MyPolygon() super(“MyPolygon:繪多邊形繪多邊形”); setSize(275, 230); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(tr

27、ue); public void paint( Graphics g ) int xValues = 20, 40, 100, 50; int yValues = 50, 150, 100, 30; Polygon poly1 = new Polygon( xValues, yValues, 4); g.drawPolygon(poly1); Polygon poly2 = new Polygon(); poly2.addPoint(100,135); poly2.addPoint(175,50); poly2.addPoint(230,100); poly2.addPoint(200,190

28、); poly2.addPoint(130,180); g.fillPolygon(poly2); public static void main( String args ) MyPolygon app = new MyPolygon(); 【執行結果】 線上互動繪圖工具:線上互動繪圖工具的程式碼如下:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class c8_02_03 extends JApplet implements ActionListener,MouseMotionListener,

29、MouseListener int px,py,status,tno=3; int tx=new int20; int ty=new int20; int rtx=new int20; int rty=new int20; JButton cp=new JButton5; JColorChooser ch; Color nco; Graphics2D g2; public void init() Container c=getContentPane(); c.setLayout(new FlowLayout(FlowLayout.LEFT); c.setBackground(Color.whi

30、te); c.setSize(500,400); /建立工具列建立工具列 JToolBar tb1=new JToolBar(); tb1.setFloatable(false); String str1=三角形三角形,正方形正方形,五邊形五邊形,六邊形六邊形,選擇顏色選擇顏色; /建立元件建立元件 for(int i=0;i5;i+) cpi=new JButton(str1i); cpi.addActionListener(this); tb1.add(cpi); c.add(tb1,BorderLayout.NORTH); ch=new JColorChooser(); addMouse

31、MotionListener(this); addMouseListener(this); status=0; nco=new Color(0,255,255); public void actionPerformed(ActionEvent e) for(int i=0;i4;i+) if(e.getSource()=cpi) tno=i+3; if(e.getSource()=cp4) nco=ch.showDialog(this,選擇顏色選擇顏色,nco); /移動滑鼠指標移動滑鼠指標 public void mouseMoved(MouseEvent e) /取得座標位置取得座標位置

32、px=e.getX(); py=e.getY(); status=0; /拖移滑鼠指標拖移滑鼠指標 public void mouseDragged(MouseEvent e) Graphics g=getGraphics(); g2=(Graphics2D) g; g2.setStroke(new BasicStroke(3.0f); g2.setColor(nco); g2.setXORMode(Color.black); /判斷是否為新的圖形判斷是否為新的圖形 if(status=1) Polygon pg1=new Polygon(rtx,rty,tno); g2.drawPolygo

33、n(pg1); else px=e.getX(); py=e.getY(); status=1; double s=(e.getX()-px)*(e.getX()-px)+(e.getY()-py)*(e.getY()-py); double r=Math.sqrt(s); int w=360/tno; /計算正多邊形的頂點座標位置計算正多邊形的頂點座標位置 for(int i=0;itno;i+) txi=px+(int)(r*Math.sin(180+i*w)*Math.PI/180); tyi=py+(int)(r*Math.cos(180+i*w)*Math.PI/180); rtxi

34、=txi; rtyi=tyi; Polygon pg2=new Polygon(tx,ty,tno); g2.drawPolygon(pg2); /放掉滑鼠鍵放掉滑鼠鍵 public void mouseReleased(MouseEvent e) Graphics g=getGraphics(); g2=(Graphics2D) g; g2.setColor(nco); g2.setStroke(new BasicStroke(3.0f); Polygon pg1=new Polygon(rtx,rty,tno); g2.drawPolygon(pg1); public void mouse

35、Pressed(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e)統計圖表: 統計圖表這個程式,可以經由HTML傳遞圖表內容的數值參數。 Applet程式會根據數值內容,繪製統計圖表。統計圖表程式碼如下:import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.applet.*; public cl

36、ass c8_03_02 extends JApplet private int count; private int max_value; private int v=new int10; private Color co = Color.red,Color.blue,Color.pink, Color.yellow,Color.green; public void init() String str1; Container c=getContentPane(); c.setLayout(null); c.setSize(320,240); count=Integer.parseInt(ge

37、tParameter(count); max_value=0; for(int i=0;imax_value) max_value=vi; public void paint(Graphics g) int px,py,width,dh; width=300/count; dh=220/max_value; for(int i=0;icount;i+) g.setColor(coi%5); g.fillRect(10+i*width,210-vi*dh,width,vi*dh);/x,y座標座標,寬寬,高高 g.setColor(Color.black); g.drawString(Strin

38、g.valueOf(vi),10+i*width+width*1/3,230); public String getAppletInfo() return Title: 統計圖表統計圖表 n+Author: Wu, 2002 n; public String getParameterInfo() String info = count, 1 - 10,資料總數資料總數, v1, 0-100,第一個數值第一個數值, v2, 0-100,第二個數值第二個數值, v3, 0-100,第三個數值第三個數值, vn, 0-100,第第n個數值個數值 ; return info; HTML文件與參數內容如

39、下: 動畫製作:在瀏覽器中執行畫面如下:動畫製作的程式碼如下: import javax.swing.*;import java.awt.*;import java.awt.event.*; import .*;public class c8_02_02 extends JApplet implements ActionListener Timer timer; Image img=new Image6; int ptr; Graphics2D g2; public void init() String str1=a0.gif,a1.gif,a2.gif,a3.gif,a4.gif,a5.gi

40、f; Container c=getContentPane(); c.setLayout(null); c.setBackground(Color.white); c.setSize(320,240);/設定視窗大小設定視窗大小 for(int i=0;i6;i+) try imgi = getImage(new URL(getCodeBase(),str1i); catch(MalformedURLException e) System.out.println(下載圖形檔發生錯誤下載圖形檔發生錯誤 URL:+e+n); return; ptr=0; timer=new Timer(200,t

41、his); timer.start(); public void actionPerformed(ActionEvent e) if(e.getSource() = timer) Graphics g=getGraphics(); g.drawImage(imgptr%6,10,50,this); ptr+; 電子時鐘: 設計出顯示時間的Applet程式。電子時鐘的程式碼如下: import java.awt.*; import javax.swing.*;import java.awt.event.*; import .*;import java.util.*; import java.te

42、xt.*;public class c8_03_01 extends JApplet implements Runnable JLabel lb1=new JLabel8; ImageIcon icon=new ImageIcon11; private volatile Thread timer; public void init() String str1=0.jpg,1.jpg,2.jpg,3.jpg,4.jpg,5.jpg, 6.jpg,7.jpg,8.jpg,9.jpg,x.jpg; Container c=getContentPane(); c.setLayout(null); c.

43、setSize(140,40);/設定視窗大小 for(int i=0;i11;i+) try iconi = new ImageIcon(new URL(getCodeBase(),str1i); catch(MalformedURLException e) System.out.println(下載圖形檔發生錯誤 URL:+e+n); return; for(int i=0;i8;i+) lb1i=new JLabel(icon10); lb1i.setSize(15,28); lb1i.setLocation(5+i*16,5); c.add(lb1i); public void sta

44、rt() timer = new Thread(this); timer.start(); public void stop() timer = null; public void run() int n; Thread me = Thread.currentThread(); while (timer = me) try Thread.currentThread().sleep(100); catch (InterruptedException e) Calendar now = Calendar.getInstance(); n=now.get(now.HOUR); lb10.setIco

45、n(iconn/10); lb11.setIcon(iconn%10); n=now.get(now.MINUTE); lb13.setIcon(iconn/10); lb14.setIcon(iconn%10); n=now.get(now.SECOND); lb16.setIcon(iconn/10); lb17.setIcon(iconn%10); 載入影像並放大 再 javax.swing 程式套件中 有一個 ImageIcon 影像圖示類別,讓您可輕易的建立一個影像 圖示物件,您可以建立一個空的物件,也可以藉由 image 影像 物件建立一個 ImageIcon 影像圖示物件,您同時

46、可附加說明的 文字。 您可透過 getIconHeight() 及 getIconWidth() 方法取 得影像圖示的高度及寬度。 您也可以透過 getImageLoadStatus() 取得影像載入的狀態,可為 ABORTED放棄、錯誤 ERRORED、或完 成 COMPLETE。 ImageJava可以顯示的圖檔只有.jpg以及.gif兩種格式的檔案。 import java.applet.Applet;import java.awt.*;public class TestImage extends Applet private Image testImage;/宣告一個Image變數,t

47、estImage。 private int W, H; public void init() testImage=getImage(getDocumentBase(),test.jpg); public void paint(Graphics g) g.drawImage(testImage,0,0,this); W = testImage.getWidth(this); H = testImage.getHeight(this); g.drawImage(testImage,0, H+1,W/2, H/2,this); 執行結果testImage = getImage(getDocument

48、Base(),test.jpg);對testImage做初始化的動作,不過Image的初始化動作和一般的不一樣,一般都是用new,而因為Image指的是一個圖檔,所以必須利用getImage()去得到這一個圖檔。再抓取圖檔時,首先必須要知道那個圖檔目前所在的位置,所以可以利用getDocumentBase()或是getCodeBase()這兩個method來定址,兩者的差別在於前者是以目前所使用的這一個html檔所在的目錄為工作目錄,而後者是以這一個程式所在的目錄為工作目錄。在getImage()裡的第二個參數給的就是那張圖檔所在的路徑,而路徑也會因為之前是使用getDocumentBase(

49、)還是getCodeBase()而改變。g.drawImage(testImage,0,0,this); 將testImage利用Graphics裡面的drawImage()這一個method畫在applet上面,drawImage()的第一個參數就是所要畫的Image,而第二及第三個參數即為這張Image的左上角的點要放在這一個applet裡的那個位置,最後一個變數要傳入的是一個IamgeObserver的物件,而一般來講,ImageObserver就是要顯示這一個Image的地方,所以傳this表示說是要將Image顯示在目前這一個applet上面。ImageIcon 類別常用方法列於下:

50、 public ImageIcon(Image image, String description) 從 image 物件建立一個影像圖示物件。description 為說 明文字。 public ImageIcon (Image image) 從 image 物件建立一個影像圖示物件。 public ImageIcon() 建立一個空白的影像圖示物件。 public int getIconHeight() 傳回影像高度。 public int getIconWidth() 傳回影像寬度。 public Image getImage() 傳回影像物件。 public int getImageL

51、oadStatus() 傳回載入影像物件的狀態,可為 MediaTracker.ABORTED、 MediaTracker.ERRORED、及 MediaTracker.COMPLETE。 protected void loadImage(Image image) 載入影像物件 image。 public synchronized void paintIcon(Component c, Graphics g, int x, int y) 以 c 元件及 g 繪圖工具在座標點 (x,y) 繪製影像。 public void setImage(Image image) 將影像設為 image。 【

52、程式ImageScale.java】 /= ImageScale.java = import java.applet.*; import java.awt.*; import javax.swing.*; public class ImageScale extends JApplet private Image narrow; private ImageIcon narrow2; private int w, h, iw, ih; public void init() narrow = getImage(getDocumentBase(), narrow.gif); narrow2 = new

53、 ImageIcon(narrow.gif); w = getWidth(); h = getHeight(); iw = narrow2.getIconWidth(); ih = narrow2.getIconHeight(); public void paint(Graphics g) narrow2.paintIcon(this, g, (w-iw)/2, 0); g.drawImage(narrow, 0, ih+10, w, h-(ih+10), this); showStatus(tt狹橋); 網頁ImageScale.html 【執行結果】 javac ImageScale.ja

54、va appletviewer ImageScale.html Graphics Double Buffering 如果在程式中有使用到很多的Image或是利用Graphics畫圖的話,那畫面可能就會一閃一閃的,看起來就很不舒服。而這個問題就可以利用Graphics Double Buffering來解決。當畫面上顯示出一張Image時,可以先將下一個要重繪畫面所要用到的Image先貼到buffer裡面,等到要用的時候再將這一個buffer貼到畫面上來,如此就可以很順的顯示出這一張Image,而不會一閃一閃的。 import java.applet.Applet;import java.awt

55、.*;import java.awt.event.*;public class TestDoubleBuffering extends Appletprivate int imageNum;private int dx,dy;private Image OffscreenImage;private Graphics OffscreenGraphics;public void init() imageNum = 0; dx = dy = 10;public void update(Graphics g) paint(g);public void paint(Graphics g) if(Offs

56、creenImage = null)OffscreenImage = createImage(200,200); OffscreenGraphics = OffscreenImage.getGraphics(); OffscreenGraphics.setColor(Color.white);/將將OffscreenGraphics的顏色設定成為白色的顏色設定成為白色 OffscreenGraphics.fillRect(0,0,200,200); /將顏色設成黑色,在畫出一個實心圓。將顏色設成黑色,在畫出一個實心圓。 OffscreenGraphics.setColor(Color.blac

57、k); OffscreenGraphics.fillOval(dx,dy,dx+30,dy+30); g.drawImage(OffscreenImage,0,0,this); / OffscreenImage貼在目前所使用的這一個貼在目前所使用的這一個Applet上面。上面。 dx += 10; dy += 10; try Thread.sleep(1000); /sleep()這一個這一個method如果有發生錯誤的話會丟出一個如果有發生錯誤的話會丟出一個InterruptedException,所以必須使用,所以必須使用try這一個格式來寫。這一個格式來寫。 catch(Interrup

58、tedException e) repaint(); if(OffscreenImage = null) OffscreenImage = createImage(200,200); 首先檢查看OffscreenImage之前是否已經被產生出來了,如果沒有的話則OffscreenImage就會等於null,這時就必須要利用createImage()來產生一個新的OffscreenImage,因為這一個Image並不是直接儲存檔案的內容,所以必須使用createImage()來產生,而裡面的參數分別為寬和長。OffscreenGraphics = OffscreenImage.getGraphi

59、cs(); 利用Image裡面的getGraphics()這一個method來得到一個graphics context。 而OffscreenGraphics和OffscreenImage兩者的用途何在?OffscreenImage是真正所要顯示的圖所存放的地方,而OffscreenGraphics是使用來將這些圖畫在OffscreenImage上面的。而每一個Image都有一個相對應的Graphics,所以其所對應的那一個Graphics就可以在那個Image上面作畫。OffscreenGraphics.setColor(Color.white);OffscreenGraphics.fill

60、Rect(0,0,200,200);先將OffscreenGraphics的顏色設定成為白色的,這樣之後所畫出來的圖形都會是白色的。在將畫面整個都用矩形來覆蓋過去,這兩行的目的在於將之前所畫在OffscreenImage上面的話給覆蓋過去,使得顯示出來的時候不會把之前畫的也一起顯示出來。MediaTracker 當我們在程式中有要顯示Image的時候,在第一次顯示時通常都會有小小的不順,這是因為當第一次顯示時,這一張Image事實上是還沒有被完全的讀取進來的,因此此時可以利用MediaTracker來偵測這個Image是否已經被完全的讀取進來了。 private MediaTracker me

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论