Chap10

  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Chap10 as PDF for free.

More details

  • Words: 1,739
  • Pages: 20


 : ("'  "#*     %*#$0+;1 %&'-' %*#$0+;1+1,$'5.4:+2 Gdg  )* 1,e )9)   <APPLET>  0)'96 %*#$0+;1/+7)9)Applet+/+79) ) Graphics+*<& )7/,k3) )'$-6.,/+7,. 7. $(<-'%*#$0+;10)#,& ,4:+2!0+4:+27'. +16'-.-'%*#$ 0+;1*) 75$1(&15,K=696

: ("'  "#*  %*#$0+;1 F >UUNMJ /%*!"#$7) *X. !'16%*$;$C2 FZ_ `IPJ )* mg '=5 %*#$0+;1# &6$'4:+2%*7.4:+2/ - %*!"#$FBxJ - %*!"Gdg FBhM NJ %*) AjBA 1,$'5.-.%*#$0+;10)7&.-6/$*GNN ZNH %*# 115.#%*#$'8121.)4*5* *<&9) ) main() +#4*57*X.1*+0,.4&6 15#16.'96%&'K5%*!" Gdg  1,$'5.95%*) AjBR %*!" Gdg  )'96%* HelloWorldApplet.class 0)*$+K+!'16%*$;$C2)* mg'=5 ("'   6! %*#$0+;10)0(*02-6/$*n833=o=03J  import java.awt.*; import java.applet.*;  public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello World",20,20); } } 

    





("'   6!$%*!"nT4^) '96

HelloWorldApplet.class

    <TITLE>HelloWorld Example<7TITLE> 

<7HEAD>

 

  <APPLET CODE=”HelloWorldApplet.class” WIDTH=”300” HEIGHT=”300”>     

   %*!"Gdg #* 1,e)9) <APPLET>0)968%*42 %/6&)4&6*#/*40+2%*#$0+;1CD).!' 1,e #*/8@+,"@ FMM _ M)15.3/ 9)-.%*42%/6& FBONPP)+-&-.%*0+;1) 16.#7&.$;0#  -,10,a%*#$0+;1) 7&.1,$'5.)K5*#*&,. AB -'%*C2&%/6& FHelloWorldApplet.javaJ +%* $;0#FHelloWorld.htmlJ%&'96%*(&12&3 RB /*40+2%*HelloWorldApplet.java%&'96/*40+2&,. javac HelloWorldApplet.java

1,$'5.# 4&6%*42%/6&9) )HelloWorldApplet.class ^B 96%*$;$C2&395 ? M M |]UN  eMPOU ƒ& %*$;0#9) ) HelloWorld.htmlCD).#'%*42%/6&) 9) HelloWorldApplet.class %&',1%*,1(+#*K+,&,.7&. =)AjBA

 

T6,7/K,C02,@,/K,U,@,7





   6! K++,0<2)4&6#,%*)AjBR$;$C2  ,%*#$0+;1!'%*$;$C2 #*-,1 .&,.=)yBR%&'*-,1.&,. AB %*$;$C2#%+&Qc 1*15.)8 RB %*$;$C2#%+&4:+2 Gdg )8+/7,).1,e15.3) 84:+2Gdg  ^B %*$;$C2#%+&%*%*42%/6& )%* #$0+;1)81,e 9) )APPLET1*Qc )8 tB %*$;$C2#,%*#$0+;1  %*$;$C2),%*#$0+;1#16.* mg'=5+).# %*$;$C27*X%+&%*#$0+;1 *#/)./*0($12*5 -5')34&6 &,.,!"#$#D.4&6&/$*+&!,',%*#$ 0+;10)„.,4*56%*#$0+;1.15.3&,. - 5-'4:+2)3)'=5/).),%*'=5     





- '96%*)3)'=5/).),%*'=5 - 1(&15,/).*5-5') - '96 M  MhHF/7,).'*<&%*!")J  @)%*!"#$0+;10''*'96/7,).)%*$;$C2 4*58k16+5%*#75.-6K(&0+&9) )SecurityException+,*

  4!$.-.%*#$0+;1





: D?f ) qcVV{Nbp 

,%*#$0+;1#16.*76.4:+2 Gdg   CD).16.* 1,e )9) <APPLET>  CD).#*/8@+,"@15.30)815.+9)-.%*42%/6&-. %*#$0+;1+'+'&)3%&'*=&,. 

<APPLET code = filename.class width = widthPixels height = heigthPixels [archive = archivefile] [codeapplet = url] [vspace = vMargin] [hspace = hMargin] [align = alignment] [alt = text]> [ ]

 

 

T6,7/K,C02,@,/K,U,@,7

 

1,e)9)<APPLET>#*/8@+,"@)16.87*'5./ - code0)89)%*42%/6&-.%*#$0+;1 - width0)8/$*$6.-.%*0+;1)16.7&.$;0# - height0)8/$*7=.-.%*0+;1)16.7&.$;0# 

#',.*/8@+,"@)3).)4-0()*1(*&,. x

x

ALIGN0)&$.15.-.%*#$0+;195left, right, top, textop, middle, absmiddle, baseline, bottom +absbottom16 CODEBASE0)815.-.Qc );%*42%/6&-.%*

#$0+;1 x

ARCHIVE0)8/+74:+2>c)16.6%+&-D*4$6

x

ALT0)  8 -6  /$*) 16  .6  $;  0#7&.@ )4 *5 7 *X%+&

%*42%/6&4&6 x

VSPACE/'5..$1,.-.%*#$0+;1

x

HSPACE

/'5..$-.%*#$0+;1



: '1   /8@+,"@)9) PARAM 0+;14&6%&'*=&,.

#9675.K50*(124',.%*#$



 




VALUE = val1>

%&') - attr1/9)-.0*(12)#S'=5%*#$0+;1 - val1//5-.0*(12#) 75.6,%*#$0+;1 1,$'5.95 

    





 %*#$0+;17*X'&=/50*(12 #) 75.K5*#%* BhM N %&'96/8@+,"@ 1,e )9) APPLET 4 &6 %&'96*<&9) ) getParameter() )*= 



 



String getParameter(String paramName)

%&') paramName/9)0*(12

/*%&'#/5)'=5  *<&#75./59(&-6*=+ String   )9) VALUE /5)75.+,*# OPoP P M  159)0*(12#4*595 OPoP P M   1,$'5.95   getParameter(“SPEED”) #6 /5  +,  *  ‹Œ1*)  8 4 $6  1, $ '5  .-. -6  .16  @)4*500*(121*9))81,e )9)<APPLET> /5)4&6+,*#*/5 

null

:  #Applet Applet  /+7)&4$6  >?  )'=50/;# java.applet  %*#$0+;18%*#16.7&*#/+7%&'96/7,.)  extends Applet/+7)9) Applet#,&/+7! Container)7  &*#/+7) 9)Panel%&'*#,&$.K,.FlowLayout/+7)9)Applet#D.7*X)# 75#;/) 75$::ƒ-.0/;# >Zd 4&6 @1) 6.96 75$::ƒ-.0/;# javax.swing %*#$0+;1#16.7& *#/+79) ) JApplet15# +5$XD./+7)9) Applet75$0,a %*%&'96/+7)9)JApplet #*+,/+6', %*#$0+;1#115.#%*#$'812 1.)4*5**<&) 9) main() 15%*$;$C2#,%*#$0+;1%&'0(#@# *<&)'=5 %*CD).*<&-./+7)9) Applet  *<&/ init(), start(),stop(),destroy()+paint()%&'*+,.&,.7&.= )AjBR

 

T6,7/K,C02,@,/K,U,@,7



 A0init()  *<&#X='0'./,.&'$*)*%+&%*#$0+;1 *)* %+&FNHJ%*$;$C2 %&',$) 4%*#$0+;1# H *<&@1) 6.*/7,.) )16 .(*) 1695 &/5(*) 16%+&=!0 16 

 A0start()  *<&#X='+,.#/7,).*<&)9)init()+X='968/,.) K=696+,*',.$;0#*) %*#$0+;16'=5 8/,.*) 7&.:**)  %*#$0+;16'=5 %&',$) 4%*#$0+;1# H*<& @)16.*/7,).1) 6.8/,.)*+,-6*$;0#6(95 ()*16 7&.!0/+)4$*5 

 A0stop()  *<&#X='96*)K=696'6 '4',.$;0#6) *)*'+( 7&.:**) % *#$0+;16'=5 %&',$) 4%*#$0+;1#  H *<& @1) 6.*/7,).'8&.-.%**)#$; 0#695'8&!0/+)4$%*<&h1608,Ji

  A0destroy()  *<&#X='/,.&'$*)*ƒ&$;$C2 %&',)$4%*#$ 0+;1#4*5  H *<&'$6#16.+-6*=+5$'/$*#.'5.95 %*<&7) 6.-D 

 A0paint()  *<& paint() #*<&)*/7,).) )'$-6.,$&=::ƒ15.3 %&'*<&paint()*=&,.   

    





public void paint(Graphics g) { [statements] } 

%&')  x g/#;/-./+7Graphics)7*X96$&=::ƒ15.3 x statements//7,).$&=::ƒ15.3 /7,).*<& paint() #X='96.+,.#/7,).*<& start()67*X# ) -'=.-6/$*) ::ƒ4&6 #*<& paint() #X='C8/,.)$;$C2*+)'+.95 ,-&* /+))

    6!$ +,.-.*<&%*#$0+;1

:$  #Graphics 

76.!0::ƒ%*#$0+;1#4&6%&' H*<& paint()+6$-'/7,.) $&=.::ƒ15.3*<&1,$'5.95%*) AjB^ 7&.1,$'5.-./+7 MyApplet CD).#+761.#0(,& FAj\AjJ  4',.0(,& FAjj\AjjJ CD).%*# 4&6K++,0<2&,.7&.=)AjB^

 

T6,7/K,C02,@,/K,U,@,7

 ("'   6!%/+7MyApplet)96+761.   import java.awt.*; import java.applet.*;   public class MyApplet extends Applet { public void paint(Graphics g) {  g.drawLine(10,10,100,100); }  }  

   6!% K++,0<2)4&6#,%*)AjB^ 

:$  A0B' 0 1U /+7)9)

Graphics

#**<&$&=.::ƒ15.3+'*<&

&,. x

drawLine(int x1, int y1, int x2, int y2)

*<&) 96$&761.#0(,&(x1,y1)4',.0(,&(x2,y2) x

drawRect(int x, int y, int width, int height)

*<&) 96$&7) +') *KK6 %&'#*15.(*) 16)0(,& (x,y) CD).*/$*$6.+/$*7=.width+height1*+&, x

fillRect(int x, int y, int width, int height)

*<&) 96$&7) +') *KK695&'$,*<& drawRect()15 #*7 1(*1;*7)+)'*KK6     





x

drawRoundRect(int x, int y, int width, int height,  int arcWidth, int arcHieght)

x

fillRoundRect(int x, int y, int width, int height,  int arcWidth, int arcHieght)

*<&,.7.*<& *<&)96$&7) +)'*KK6*8** F Ho HHOM NJ%&'#*,p*/$*%/6. arcWidth+ arcHieght +1,.1*+&,  %&'*<& fillRoundRect() # $&7) +)'*KK6*8***) 7 1(*1;* x x

drawOval(int x, int y, int width, int height) fillOval(int x, int y, int width, int height)

*<&,.7.*<& *<&) 96$&=$.)** 8*C6''=5) 15.0(,&(x,y)%&'*/$*$6.+/$*7=. width + heigth 1*+&,75$*<&fillOval()#*7 1(*1;*$. x

drawArc(int x, int y, int width, int height, int  startAngle, int arcAngle)

x

fillArc(int x, int y, int width, int height, int  startAngle, int arcAngle)

*<&,.7.*<& *<&)96$&76%/6.FOJ)** 8*C6''=5) 15.0(,& (x,y) */$*$6. width*/$*7=. height(*) $&#*8* startAngle .p 4#$ arcAngle .p 75$*<& fillArc()#*7 1(*1;*76%/6. x x x

drawPolygon(int []x, int []y, int numPoints) fillPolygon(int []x, int []y, int numPoints) drawPolyline(int []x, int []y, int numPoints)

*<&,.7**<&*<&)96$&=+'+') * FUNW J %&' #*15.-.*8* numPoints15.CD).#*15.*8*'=50(,&)  1,$'2)9) x + y *<& fillPolygon()#*7 1(*1;*= +'+)'*75$*<&drawPolyline()#76+'+') *ƒ& x drawString(String s, int x, int y)  *<& 967&.-6/$*-.1,$9) ) s)15.0(,&(x,y)

 

T6,7/K,C02,@,/K,U,@,7

 %*) AjBt7&.1,$'5.-.%*}Q?)$&=.15.3%&'%* #6K++,0<2&,.7&.=)AjBt ("'   6!,1,$'5.96*<&/+7Graphics$&=.15.3  import java.awt.*;  import java.applet.*;  public class GUIApplet extends Applet {  public void paint(Graphics g) { g.drawRect(5,5,15,15);  g.fillOval(50,50,20,20); g.drawArc(75,25,40,40,0,135);  g.fillArc(80,80,30,30,90,270);  g.drawString("Hello",5,90); }  } 



   6!,K++,0<2)4&6#,%*)AjBt

:$  A0 B)B'0' />#'>'22v' #;/-./+79) ) Graphics */8@+,"@15.3-.=.::ƒ)$& 95 7-.06 Fb HJ +,"@-.76 +=-.:12 7*X)# &/8@+,"@15.3+54 &6%&''96*<&15.3-./+7&,.

    









  

x &7 *<& setColor() *<&) 96&7#96$&=. -.#;/-./+7Graphics%&'*=&,.   setColor(Color c) %&')#;/9) ) c#;/-./+7ColorCD).>?#('*7) 7+,4$6'=5+6$ 95 Color.red /7&.   Color.pink /7 9*0=161,$'5.95/7,). g.setColor(Color.red);  &=..::ƒ6X=$&&6$'7&. x &Font *<& setFont() *<&)96&:12)-'-6/$*+. #;/-./+7Graphics%&'*=&,.   setFont(Font f) %&')/+7Font#*O PM OM76.#;/&,.   Font(String s, int style, int size) 1,$'5.95/7,).   Fontfn1 = new Font(“Courier New”,Font.BOLD,24); g.setFont(fn1);

&6-6/$*#;/-./+7 Graphics )9) g *:12 Courier New)1,$-6*-&Rt  %*) AjBs 7&.1,$'5.%* }Q? )*- 6/$*-'&6$'7+:12 15.3CD).%*# 6K++,0<2&,.7&.=)AjBs

 A0repaint()  %* }Q?  #*%*<&)'$5 AWTThread )%*) *$+K+06*, CD).#6)/'1$#7$5 #16.*'96*<& paint() 0)$&=.::ƒ1*/7,).)867&.#;/! Container F95 AppletJ*54*5 CD).%* AWTThread#'96*<& paint()*5@ 15.3(95 x *)*%+&#;/-./+7!Container0)7&.K+/,. x *)%*+)'#4/+,*%*)7&.K+1*6#1(

 

T6,7/K,C02,@,/K,U,@,7

 x *)%*$;$C2 0+;1'=5

+,*7&.$;0#6)*%*#$

("'   6!- 1,$'5.%*_ZQ)*- 6/$*-'&6$'7+:1215.3 import java.awt.*; import java.applet.*; public class ColFontApplet extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(10,10,40,10); g.setColor(Color.pink); g.fillArc(95,50,30,30,0,210); g.setColor(Color.black); Font fn1 = new Font("Courier New",Font.BOLD,24); g.setFont(fn1); g.drawString("Font",5,95); } }



     6!- K++,0<2)4&6#,%*)AjBs  %* AWTThread #'96*<& repaint() *) 16.$&=. ::ƒ*5 CD).*<& repaint() #'96*<& update() 15D). &,.7&.=)AjB…

    





   6!.'96*<&15.3-.%*AWTThread %&',)$4*<&update() #*/7,).+!0::ƒ&(*+'96 *<&)9) paint() %&'*=&,. public void update(Graphics g){ g.clearRect(0, 0, width, height); paint(g); }

 7*X)# H*<& update()*5 0)4*56* +!0::ƒ &(*4&6%&'&=/7,).&,. public void update(Graphics g){ paint(g); } 

:*  S 0KPMhGoIJG9  %*#$0+;17*X)#7&.=!0)'=5:2*115.395}?b }4&6+7*X#) +54:+27'.4&6%&'*/+7)7/,k7./+7/ x /+7ImageCD)./+7)9676.#;/0)#,&,4:+2=!0 x /+7 7'.

 

AudioClip

T6,7/K,C02,@,/K,U,@,7

CD)./+7)9676.#;/0)#,&,4:+2



:* #Image  %*#$0+;17*X76.#;/-./+7 Image 4&6%&''96 *<& getImage())'=5/+7)9)AppletCD).*= &,. x x

Image getImage(URL u) Image getImage(URL u, string name)

%&') x 

URL/+7) 9676.#;/0)815.Qc -.=!0(95 URL u = new URL(“http://java.sun.com”)

x name/9)-.4:+2=!0*) :2*1_Q\}  @)15.-.4:+2=!0* Qc  &'$,15.-.%*$;0# 7*X'96*<&)9)getDocumentBase()0)8Qc &,.+5$(95 

Image cat = getImage(getDocumentBase(),“cat.gif”)

 75$@)15.-.4:+2= !0* Qc 1.,15.-.%*42%/6& -.%*#$0+;1 7*X'96*<&9) ) getCodeBase() 0)8 Qc &,.+5$(95 Image cat = getImage(getCodeBase(), “cat.gif”)   7&.=!0)'=5#;/-./+7 Image #4&6%&''96*<&)9) drawImage() )'=5/+7)9)Graphic CD).*=&,.  drawImage(Image im, int x, int y, ImageObserver ob)  %&') - im /9)#;/-./+7Image)16.7&. - x+y/15.0(,&)16.7&. - ob/#;/-./+7) implements(12:7 ImageObserver )968+)'+.7X-.=!0 CD)./+7 Applet 4&6 implements (12:7&,.+5$4$6 #D.7*X*   M %&'96 #;/)9)this4&6  %*)AjB…7&.1,$'5.-.%*#$0+;1)*7&.=!0) 9)duke.gifCD).;'=515.&'$,%*BhM N

    





("'   6!. 1,$'5.-.%*#$0+;1*) 7&.=!0  import java.awt.*;  import java.applet.*;  public class ImageDemo extends Applet{  Image im; public void init() {  im = getImage(getDocumentBase(), "duke.gif");  } public void paint(Graphics g){  g.drawImage(im, 20, 20, this); }  }  

:* #AudioClip  %*#$0+;17*X76.#;/-./+7 *<&getAudioClip()CD).*=&,. x x

AudioClip

4&6%&'96

AudioClip getAudioClip(URL u) AudioClip getAudioClip(URL u, String name)

%&') name/9)-.4:+27'.* ) : 2*1au

/+7AudioClip#**<&))'$-6.,+57'.&,. - play() /*<&) 96+57'. - loop() /*<&) 96+57'.$ - stop() /*<&) 96'8&+57'.  #/+7 Applet ',.**<& play() )7,78+54:+27'.%&' 4*5#16.76.#;/-./+7AudioClip %&'*=&,.

 

-

play(URL u)

-

play(URL u, string name)

T6,7/K,C02,@,/K,U,@,7

  %*) AjBu 7&.1,$'5.%*#$0+;1#) +54:+27'.'5. $+#'8&+5*)#$;0#6)*% *#$0+;1'=5 ("'   6!3 1,$'5.%*#$0+;1)*+54:+27'.  import java.awt.*;  import java.applet.*;  public class MusicDemo extends Applet{  AudioClip sound; public void init() {  sound = getAudioClip(getDocumentBase(), "spacemusic.au");  } public void paint(Graphics g){  sound.loop(); }  public void stop() {  sound.stop(); }  }   

:) ("'  "#* ('j B)  /+7)9) Applet /+7! Container +7*X)#,:Y. 18@2.::ƒ15.34&6(95 ActionListener, MouseListener, 16 MouseMotionListener, KeyListener, + FocusListener &,.,#D.7*X)#-'%*#$0+;10)%161,K=6964&6  %*) AjBy 7&.1,$'5.-.%*#$0+;1*) #;/-./+7) 75$::ƒ)3 %&'**8Š 7.8*Š 0)6)+5+'8&4:+20+.)%+& -6*%*

    





("'   6!4 1,$'5.-.%*#$0+;1*) 7 5$1(&15,K=6 96 import java.awt.*; import java.awt.event.*; import java.applet.*; public class MusicGUI extends Applet implements ActionListener{

           

 

AudioClip sound; Button bn1, bn2; public void init() { sound = getAudioClip(getDocumentBase(), "spacemusic.au"); bn1 = new Button("PLAY"); bn2 = new Button("STOP"); add(bn1); add(bn2); bn1.addActionListener(this); bn2.addActionListener(this); bn1.setEnabled(true); bn2.setEnabled(false); }

public void stop() { sound.stop(); } public void actionPerformed(ActionEvent ev) { String cmd = ev.getActionCommand(); if (cmd.equals("PLAY")) { sound.loop(); bn2.setEnabled(true); bn1.setEnabled(false); } else if (cmd.equals("STOP")) { sound.stop(); bn2.setEnabled(false); bn1.setEnabled(true); } } }



T6,7/K,C02,@,/K,U,@,7



/& x

%*#$0+;17*X,!'16$;$C24&6 %*4$6!'1,euPHH^
x

%*#$0+;18%*#16.7&*#/+79) ) Applet %&'96/7,).extends Applet +#()*16.)*<&init()

x

/+7 Applet **<&7) /,k/*<& init(), destroy() + paint()

x

%&'#16.89)

start(), stop(),

*<&-./+79) ) Graphics 7*X496$&=::ƒ15.34&6 95 drawLine(), drawPolyline(), drawRect() + fillRect() 16

x

*<&), ,0e2;871 -6*#;/-./+7 paint() +update()

x

/+7)9)Image /+7) ;#;/-.4:+2!0

x

/+7)9)AudioClip /+7) ;#;/-.4:+27'.

x

Applet

Graphics

/*<&

7*X#) ,:Y.18@2.::ƒ15.34&6 (95 ActionListener, MouseListener +KeyListener 16

  

   

    





                             

T6,7/K,C02,@,/K,U,@,7

Related Documents

Chap10
November 2019 13
Chap10
December 2019 7
Chap10 Notes
October 2019 10
Solutions Chap10
November 2019 7
Chap10 Geiger
November 2019 10
066-071-chap10
October 2019 7