您好,欢迎访问三七文档
多媒體多媒體包括繪圖(graphics)、影像(image)聲音(sound)、,以及視聽(video)等。Graphics類別•Graphics為抽象類別,是所有繪圖圖形或字型的基礎類別,它允許您在元件上製作圖形或字型Graphics方法•當applet程式啟動時執行完init()、start()之後,會自動執行paint(),後來若要更改畫面必須產生事件,例如改變applet視窗大小等。若要強迫執行paint(),您可執行repaint(),它會先執行update()清除畫面之後再呼叫paint()。paint()•要在容器上繪圖,在您的程式裡頭必須實作一個paint(),並且提供一個Graphics類別的物件供繪圖之用,repaint(),update()•publicvoidrepaint()本元件重新繪圖。•publicvoidrepaint(intx,inty,intwidth,intheight)本元件重新繪圖。(x,y)左上角座標,width寬,height高。•publicvoidrepaint(longtm)本元件重新繪圖,在tm毫秒之內。•publicvoidrepaint(longtm,intx,inty,intwidth,intheight)本元件重新繪圖。(x,y)左上角座標,width寬,height高,在tm毫秒之內。•publicvoidupdate(Graphicsg)以g物件更新本元件。repain事件處理模式範例這個程式是有一個Button,Button上面寫著請按我,下面有一行字”Buttonnotyetclicked”,當我們按下去之後,下面的字串就會改變成”Buttonclicked”://TestButton.javaimportjava.applet.Applet;importjava.awt.*;importjava.awt.event.*;publicclassTestButtomextendsAppletimplementsActionListener{privateButtonbtn;privateStringstr;publicvoidinit(){str=newString(Buttonnotyetclicked.);btn=newButton(按下我);btn.addActionListener(this);add(btn);}publicvoidactionPerformed(ActionEventae){str=Buttonclicked.;repaint();}publicvoidpaint(Graphicsg){g.drawString(str,50,50);}}可以使用下列三個建構子,自己建立一個顏色物件•publicColor(floatr,floatg,floatb)建立一個新的r紅g綠b藍所構成的Color物件。r、g、b介於0.0(含)與1.0(含)之間。•publicColor(intrgb)建立一個新的rgb所構成的Color物件。rgb第0-7位元表藍色,第8-15位元表綠色,第16-23位元表紅色。•publicColor(intr,intg,intb)建立一個新的r紅g綠b藍所構成的Color物件。r、g、b介於0(含)與255(含)之間。設定顏色•publicfinalstaticColorblack=newColor(0,0,0);•publicfinalstaticColorblue=newColor(0,0,255);•publicfinalstaticColorcyan=newColor(0,255,255);•publicfinalstaticColordarkGray=newColor(64,64,64);•publicfinalstaticColorgray=newColor(128,128,128);•publicfinalstaticColorgreen=newColor(0,255,0);•publicfinalstaticColorlightGray=newColor(192,192,192);•publicfinalstaticColormagenta=newColor(255,0,255);•publicfinalstaticColororange=newColor(255,200,0);•publicfinalstaticColorpink=newColor(255,175,175);•publicfinalstaticColorred=newColor(255,0,0);•publicfinalstaticColorwhite=newColor(255,255,255);•publicfinalstaticColoryellow=newColor(255,255,0);在Color類別中有下列方法可取得顏色的成分•publicintgetBlue()傳回藍色成分,介於0-255間。•publicintgetGreen()傳回綠色成分,介於0-255間。•publicintgetRed()傳回紅色成分,介於0-255間。•publicintgetRGB()傳回紅綠藍色成分,第0-7位元表藍色,第8-15位元表綠色,第16-23位元表紅色。下列兩個抽象方法getColor()及setColor()屬於Graphics類別,它的衍生類別必須實作這兩個方法。•publicabstractvoidsetColor(Colorc)設定顏色為c。•publicabstractColorgetColor()傳回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(String[]args){MyWindow3frm=newMyWindow3();frm.setVisible(true);}}【執行結果】MyColor.javaimportjava.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.*;publicclassMyColor2extendsJFrameimplementsActionListener{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();}}【執行結果】繪線及方圓•publicabstractvoiddrawLine(intx1,inty1,intx2,inty2)以起點座標(x1,y1)至終點(x2,y2)畫直線。•publicvoiddrawRect(intx,inty,intwidth,intheight)畫一個長方形。左上角座標為(x,y),寬為width,高為height。•publicvoidfillRect(intx,inty,intwidth,intheight)畫並填滿一個長方形。左上角座標為(x,y),寬為width,高為height。•publicabstractvoiddrawOval(intx,inty,intwidth,intheight)畫一個橢圓。中心座標為(x,y),長徑(寬)為width,短徑(高)為height。若長短徑值相同即為圓。•publicabstractvoidfillOval(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);//矩形(實
本文标题:多媒体包括绘图
链接地址:https://www.777doc.com/doc-1473849 .html