资源描述
按一下以編輯母片標題樣式,按一下以編輯母片,第二層,第三層,第四層,第五層,*,*,*,多媒體,多媒體包括繪圖 (graphics) 、影像 (image)聲音 (sound) 、,以及視聽 (video) 等。,Graphics類別,Graphics 為抽象類別,是所有繪圖圖形或字型的 基礎類別,它允許您在元件上製作圖形或字型,Graphics方法,當 applet 程式 啟動時執行完 init()、start() 之後,會自動執行 paint() ,後來若要更改畫面必須產生事件,例如改變 applet 視窗大小等。若要強迫執行 paint() ,您可執行 repaint() ,它會先執行 update() 清除畫面之後再呼 叫 paint() 。,paint(),要在容器上繪圖,在您的程式裡頭必須實作一個 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, int height) 本元件重新繪圖。(x,y) 左上角座標,width 寬,height 高, 在 tm 毫秒之內。,public void update(Graphics g) 以 g 物件更新本元件。,repain事件處理模式範例,這個程式是有一個Button,Button上面寫著請按我,下面有一行字”Button not yet clicked”,當我們按下去之後,下面的字串就會改變成”Button clicked”:,/TestButton.java,import java.applet.Applet;,import java.awt.*;,import java.awt.event.*;,public 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.;,repaint();,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, 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);,public 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 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() 傳回綠色成分,介於 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.java,】,】,importjava.awt.*;,importjavax.swing.*;,publicclassMyWindow3extendsJFrame,publicMyWindow3()/Constructor,super(,底,底,色,色,為,為,藍,藍,色,色,的,的,視,視,窗,窗);,setBounds(50,100,400,150);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,getContentPane().setBackground(Color.blue);,publicvoidpaint(Graphicsg),g.setFont(newFont(Serif,Font.BOLD,32);,g.setColor(Color.RED);,g.drawString(,我,我喜,歡,歡Java!,100,100);,publicstaticvoidmain(Stringargs),MyWindow3frm=newMyWindow3();,frm.setVisible(true);,【執,行,行結,果,果】,MyColor.java,importjava.awt.*;,importjavax.swing.*;,publicclassMyColorextendsJFrame,publicMyColor(),super(MyColor:Usingcolors);,setSize(300,80);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,setVisible(true);,publicvoidpaint(Graphicsg),Colorcolor=Color.black;,g.setColor(color);,g.fillRect(25,45,100,20);,g.drawString(RGB值:+color.getRed()+(,紅,紅),+,color.getGreen()+(綠),+color.getBlue()+(,藍,藍),130,60);,publicstaticvoidmain(Stringargs),MyColormyColor=newMyColor();,MyColor2.java,使,使用JColorChooser,元,元,件,件讓,您,您挑,選,選一,個,個,顏,顏色,,,,並,將,將該,顏,顏色,設,設為,版,版面,的,的底,色,色。,您,您在,改,改變,背,背景,顏,顏色,的,的,按,按鈕,上,上按,一,一下,滑,滑鼠,左,左鍵,,,,產,生,生一,個,個按,鈕,鈕事,件,件,,接,接著,就,就執,行,行actionPerformed(),方,方法,。,。,importjava.awt.*;,importjavax.swing.*;,importjava.awt.event.*;,publicclassMyColor2,extendsJFrameimplementsActionListener,privateJButtonchangeColor;,privateColorcolor=Color.lightGray;,privateContainerc;,publicMyColor2(),super(MyColor2:,使,使用JColorChooser元,件,件);,c=getContentPane();,c.setLayout(newFlowLayout();,changeColor=newJButton(改,變,變背,景,景顏,色,色);,changeColor.addActionListener(this);,c.add(changeColor);,setSize(300,80);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,setVisible(true);,publicvoidactionPerformed(ActionEvente),color=JColorChooser.showDialog(this,選,色,色,color);,if(color=null),color=Color.lightGray;,c.setBackground(color);,c.repaint();,publicstaticvoidmain(Stringargs),MyColor2app=newMyColor2();,【執,行,行結,果,果】,繪線,及,及方,圓,圓,publicabstractvoid,drawLine,(intx1,inty1,intx2,inty2),以,以起,點,點座,標,標(x1,y1),至,至終,點,點(x2,y2),畫,畫直,線,線。,publicvoid,drawRect,(intx,inty,intwidth,intheight),畫,畫,一,一個,長,長方,形,形。,左,左上,角,角座,標,標為(x,y),,,,寬,為,為width,,高,高為height。,publicvoid,fillRect,(intx,inty,intwidth,intheight),畫,畫,並,並填,滿,滿一,個,個長,方,方形,。,。左,上,上角,座,座標,為,為(x,y),,寬,寬為width,,,,,高,高為height。,publicabstractvoid,drawOval,(intx,inty,intwidth,intheight),畫,畫,一,一個,橢,橢圓,。,。中,心,心座,標,標為(x,y),,,,長,徑,徑(,寬,寬),為,為width,,短,短徑(,高,高),為,為height,。,。若,長,長短,徑,徑值,相,相同,即,即為,圓,圓。,publicabstractvoid,fillOval,(intx,inty,intwidth,intheight),畫,畫,並,並填,滿,滿一,個,個橢,圓,圓。,中,中心,座,座標,為,為(x,y),,長,長徑(寬)為width,,,,,短,短徑(高)為height。,若,若長,短,短徑,值,值相,同,同即,為,為圓,。,。,程式MyLine.java】,importjava.awt.*;,importjavax.swing.*;,publicclassMyLineextendsJFrame,privateStrings=UsingdrawString!;,publicMyLine(),super(,“,“MyLine:,繪,繪線條,矩形,橢圓,”,”);,setSize(400,170);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,setVisible(true);,publicvoidpaint(Graphicsg),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.fillOval(120,100,90,55);/,橢,橢圓(,實,實心),publicstatic voidmain(String args), MyLineapp=newMyLine();,【執行,結,結果】,繪弧,在 Graphics 類,別,別提供,下,下列兩,個,個繪弧,方,方法drawArc(),及,及fillArc(),。,。弧形,是,是圓或,橢,橢圓形,的,的一部,分,分,以,時,時鐘三,點,點鐘方,向,向為,基,基準0 度,,,,反時,鐘,鐘方向,為,為正,,順,順時鐘,方,方向為,負,負。,publicabstractvoidfillArc(intx,inty,ntwidth, intheight, intstartAngle,intarcAngle),畫並填,滿,滿一個,弧,弧形。,中心座,標,標為(x,y),,長徑(寬),為,為width,,短徑(高),為,為height,。,。,起角為startAngle,度,度,,角度為arcAngle,度,度,,0 度,為,為 3,點,點鐘,位,位置,,反時鐘,方,方向,為,為正,,順時鐘,方,方向為,負,負。,【程式MyArc.java】,importjava.awt.*;,importjavax.swing.*;,publicclassMyArcextendsJFrame,publicMyArc(),super(MyArc:Drawing Arcs );,setSize(260,120);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,setVisible(true);,publicvoidpaint(Graphicsg),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度,publicstatic voidmain(String args),MyArcapp=newMyArc();,【執行,結,結果】,繪多邊,形,形,publicabstractvoid,drawPolygon,(int xPoints,int yPoints,int nPoints),畫,畫一個,封,封閉多,邊,邊形。,由,由 nPoints,點,點所,構,構成,,其,其座標,存,存於陣,列,列xPoints,及,及yPoints,中,中。,publicabstractvoid,drawPolygon,(Polygonp),以,以Polygon 類,別,別的物,件,件 p,畫,畫一,個,個封閉,多,多邊形,。,。,publicabstractvoid,drawPolyline,(int xPoints,int yPoints,int nPoints);,以,以點,陣,陣列資,料,料畫一,個,個多邊,線,線條。,publicabstractvoid,fillPolygon,(int xPoints,int yPoints,int nPoints),畫,畫並填,滿,滿一個,封,封閉多,邊,邊形。,由,由 nPoints,點,點所,構,構成,,其,其座標,存,存於,陣,陣列xPoints,及,及yPoints,中,中。,publicabstractvoid,fillPolygon,(Polygonp),以,以Polygon 類,別,別的物,件,件 p,畫,畫並,填,填滿一,個,個封閉,多,多邊形,。,。 多,邊,邊形類,別,別 Polygon,包,包含,一,一系列,的,的點座,標,標 (x,y),相,鄰,鄰兩,點,點所構,成,成的邊,,,,為多,邊,邊形的,一,一邊。,Polygon 類,別,別其常,用,用的方,法,法說明,如,如下:,public,Polygon,(),建,建構,子,子。建,立,立一個,空,空白的Polygon,類,類別物,件,件。,public,Polygon,(int xpoints,int ypoints,int npoints),建,建構子,。,。建立,一,一個npoints 個,頂,頂點的Polygon,類,類別物,件,件。,其,其 i,頂,頂點,座,座標為(xpointsi,ypointsi)。,publicvoid,addPoint,(int x, inty),附,附加,一,一點(x,y),至,至本多,邊,邊形物,件,件。,publicboolean,contains,(int x, inty),若,若(x,y),含,含於本,物,物件則,傳,傳回true,否,則,則傳回false,。,。,publicRectangle,getBounds,(),傳,傳回,包,包含本,多,多邊形,物,物件的,最,最小長,方,方形。,publicvoid,translate,(int deltaX,int deltaY),沿,沿著x,軸,軸移動deltaX,沿,著,著 y,軸,軸移,動,動 deltaY,像,像點。,【程式MyPolygon.java】,importjava.awt.*;,importjavax.swing.*;,publicclassMyPolygon extendsJFrame,publicMyPolygon(),super(,“,“MyPolygon:繪,多,多邊形,”,”);,setSize(275,230);,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,setVisible(true);,publicvoidpaint(Graphicsg),intxValues =20,40,100,50;,intyValues =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);,poly2.addPoint(130,180);,g.fillPolygon(poly2);,publicstatic voidmain(String args),MyPolygon app= newMyPolygon();,【執行,結,結果】,線上互,動,動繪圖,工,工具:,線上互,動,動繪圖,工,工具的,程,程式碼,如,如下:,importjava.awt.*;,importjavax.swing.*;,importjava.awt.event.*;,publicclassc8_02_03extendsJAppletimplements ActionListener,MouseMotionListener,MouseListener,intpx,py,status,tno=3;,inttx=newint20;,intty=newint20;,intrtx=new int20;,intrty=new int20;,JButtoncp=newJButton5;,JColorChooser ch;,Color nco;,Graphics2D g2;,public voidinit(),Container c=getContentPane();,c.setLayout(newFlowLayout(FlowLayout.LEFT);,c.setBackground(Color.white); c.setSize(500,400);,/建立,工,工具列,JToolBartb1=newJToolBar();,tb1.setFloatable(false);,String str1=,三,三角形,正方,形,形,五,五邊形,六邊,形,形,選,選擇顏色;,/建立,元,元件,for(inti=0;i5;i+), cpi=newJButton(str1i);,cpi.addActionListener(this);,tb1.add(cpi);,c.add(tb1,BorderLayout.NORTH);,ch=new JColorChooser();,addMouseMotionListener(this);,addMouseListener(this);,status=0;,nco=newColor(0,255,255);,public voidactionPerformed(ActionEvente), for(int i=0;i4;i+), if(e.getSource()=cpi), tno=i+3;,if(e.getSource()=cp4),nco=ch.showDialog(this,選擇,顏,顏色,nco);,/移動,滑,滑鼠指標,public voidmouseMoved(MouseEvent e), /取得座,標,標位置,px=e.getX();py=e.getY();status=0;,/拖移,滑,滑鼠指標,public voidmouseDragged(MouseEvente), Graphicsg=getGraphics();,g2=(Graphics2D)g;,g2.setStroke(newBasicStroke(3.0f);,g2.setColor(nco);,g2.setXORMode(Color.black);,/判斷,是,是否為新,的,的圖形,if(status=1), Polygon pg1=new Polygon(rtx,rty,tno);,g2.drawPolygon(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);,intw=360/tno;,/計算,正,正多邊形,的,的頂點座,標,標位置,for(inti=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=txi;,rtyi=tyi;,Polygonpg2=newPolygon(tx,ty,tno);,g2.drawPolygon(pg2);,/放掉,滑,滑鼠鍵,public voidmouseReleased(MouseEvente), Graphicsg=getGraphics();,g2=(Graphics2D)g;,g2.setColor(nco);,g2.setStroke(newBasicStroke(3.0f);,Polygonpg1=newPolygon(rtx,rty,tno);,g2.drawPolygon(pg1);,public voidmousePressed(MouseEvente),public voidmouseEntered(MouseEvente),public voidmouseExited(MouseEvent e),public voidmouseClicked(MouseEvente),統計圖表,:,:,統計圖表,這,這個程式,,,,可以經,由,由HTML傳遞圖,表,表內容的,數,數值參數,。,。,Applet程式,會,會根據數,值,值內容,,繪,繪製統計,圖,圖表。,統,計,計,圖,圖,表,表,程,程,式,式,碼,碼,如,如,下,下,:,:,importjavax.swing.*;,importjava.awt.*;,importjava.awt.event.*;,importjava.applet.*;,publicclassc8_03_02extendsJApplet,privateintcount;,privateintmax_value;,privateintv=newint10;,privateColorco=Color.red,Color.blue,Color.pink,Color.yellow,Color.green;,publicvoidinit(),Stringstr1;,Containerc=getContentPane();,c.setLayout(null);c.setSize(320,240);,count=Integer.parseInt(getParameter(count);,max_value=0;,for(inti=0;imax_value)max_value=vi;,publicvoidpaint(Graphicsg),intpx,py,width,dh;,width=300/count;,dh=220/max_value;,for(inti=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(String.valueOf(vi),10+i*width+width*1/3,230);,public String getAppletInfo(), returnTitle: 統,計,計圖表 n+Author: Wu,2002n;,public String getParameterInfo(), Stringinfo= count,1 -10,資料總數,v1, 0-100,第一個數,值,值,v2, 0-100,第二個數,值,值,v3, 0-100,第三個數,值,值,vn, 0-100,第n個數,值,值 ;,return info;,HTML文,件,件與參數內,容,容如下:,動畫,製,製作,:,:,在瀏,覽,覽器,中,中執,行,行畫,面,面如,下,下:,動畫,製,製作,的,的程,式,式碼,如,如下,:,:,importjavax.swing.*;,importjava.awt.*;,importjava.awt.event.*;,.*;,publicclassc8_02_02extendsJAppletimplementsActionListener,Timertimer;,Imageimg=newImage6;,intptr;,Graphics2Dg2;,publicvoidinit(),Stringstr1=a0.gif,a1.gif,a2.gif,a3.gif,a4.gif,a5.gif;,Containerc=getContentPane();,c.setLayout(null);c.setBackground(Color.white);,c.setSize(320,240);/設,定,定視,窗,窗大,小,小,for(inti=0;i6;i+),try,imgi=getImage(newURL(getCodeBase(),str1i);,catch(MalformedURLExceptione),System.out.println(,下,下載,圖,圖形,檔,檔發,生,生錯,誤,誤URL:+e+n);,return;,ptr=0;timer=newTimer(200,this);timer.start();,publicvoidactionPerformed(ActionEvente),if(e.getSource()=timer),Graphicsg=getGraphics();,g.drawImage(imgptr%6,10,50,this);ptr+;,電子,時,時鐘,:,:,設計,出,出顯,示,示時,間,間的Applet,程,程式,。,。,電子,時,時鐘,的,的程,式,式碼,如,如下,:,:,importjava.awt.*;importjavax.swing.*;,importjava.awt.event.*;.*;,importjava.util.*;importjava.text.*;,publicclassc8_03_01extendsJAppletimplementsRunnable,JLabellb1=newJLabel8;,ImageIconicon=newImageIcon11;,privatevolatileThreadtimer;,publicvoidinit(),Stringstr1=0.jpg,1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,8.jpg,9.jpg,x.jpg;,Containerc=getContentPane();,c.setLayout(null);,c.setSize(140,40);/,設,設定,視,視窗,大,大小,for(inti=0;i11;i+),try,iconi=newImageIcon(newURL(getCodeBase(),str1i);,catch(MalformedURLExceptione),System.out.println(下,載,載圖,形,形檔,發,發生,錯,錯誤URL:+e+n);,return;,for(inti=0;i javacImageScale.java, appletviewer ImageScale.html,GraphicsDoubleBuffering,如果在程,式,式中有使,用,用到很多,的,的Image或是,利,利用Graphics畫圖,的,的話,那,畫,畫面可能,就,就會一閃,一,一閃的,,看,看起來就,很,很不舒服,。,。而這個,問,問題就可,以,以利用GraphicsDouble Buffering,來,來解決。,當,當畫面上,顯,顯示出一,張,張Image時,,可,可以先將,下,下一個要,重,重繪畫面,所,所要用到,的,的Image先貼,到,到buffer裡,面,面,等到,要,要用的時,候,候再將這,一,一個buffer,貼,貼到畫面,上,上來,如,此,此就可以,很,很順的顯,示,示出這一,張,張Image,而,不,不會一閃,一,一閃的。,import java.applet.Applet;,import java.awt.*;,import java.awt.event.*;,public classTestDoubleBufferingextendsApplet,private int imageNum;,privateintdx,dy;,privateImage OffscreenImage;,privateGraphicsOffscreenGraphics;,public voidinit(), imageNum= 0;,dx =dy= 10;,public voidupdate(Graphicsg), paint(g);,public voidpaint(Graphics g), if(OffscreenImage=null),OffscreenImage =createImage(200,200);,OffscreenGraphics =OffscreenImage.getGraphics();,OffscreenGraphics.setColor(Color.white);/,將OffscreenGraphics的顏,色,色設定成,為,為白色,OffscreenGraphics.fillRect(0,0,200,200);,/,將顏色設,成,成黑色,,在,在畫出一,個,個實心圓,。,。,OffscreenGraphics.setColor(Color.black);,OffscreenGraphics.fillOval(dx,dy,dx+30,dy+30);g.drawImage(OffscreenImage,0,0,this);,/,OffscreenImage貼在,目,目前所使,用,用的這一,個,個Applet上,面,面。,dx += 10;dy+= 10;,tryThread.sleep(1000);,/,sleep()這,一,一個method,如,如果有發,生,生錯誤的,話,話會丟出,一,一個InterruptedException,所,以,以必須使,用,用try,這,這一個格,式,式來寫。,catch(InterruptedException e) ,repaint();,if(OffscreenImage =null)OffscreenImage =createImage(200,200);,首先檢查,看,看OffscreenImage之,前,前是否已,經,經被產生,出,出來了,,如,如果沒有,的,的話則OffscreenImage就會等,於,於null,這時,就,就必須要,利,利用createImage()來,產,產生一個,新,新的OffscreenImage,,,,因為這,一,一個Image並,不,不是直接,儲,儲存檔案,的,的內容,,所,所以必須,使,使用createImage()來,產,產生,而,裡,裡面的參,數,數分別為,寬,寬和長。,OffscreenGraphics =OffscreenImage.getGraphics();,利用Image裡,面,面的getGraphics()這,一,一個method,來,來得到一,個,個graphics context。,而OffscreenGraphics和OffscreenImage兩者的,用,用途何在,?,?OffscreenImage是,真,真正所要,顯,顯示的圖,所,所存放的,地,地方,而OffscreenGraphics是使用,來,來將這些,圖,圖畫在OffscreenImage上面的,。,。而每一,個,個Image都有,一,一個相對,應,應的Graphics,所,以,以其所對,應,應的那一,個,個Graphics就可以,在,在那個Image,上,上面作畫,。,。,OffscreenGraphics.setColor(Color.white);OffscreenGraphi
展开阅读全文