Pertemuan6 JavaApplets [Compatibility Mode]

  ! " # $ # %

  & '

  && && && ( #

  )

  • # $
    • ,-. # / '

  $ 0 $ 1

  01

  2

  3

  • 0 $

  4

  • 0 $

  0 $

  • – byte code is a generic machine code 0 $ 1
  • – must have a byte code interpreter for each hardware platform

  , 0 $

  , 0 $

  / # 01 ' /

  3 , ' .

  ! 5 $

  3 6 '

  $ !

  2

  • 7" $ . %8 6.. + .

    , 0 $ 9) :

  ' #

  • 0 $

  # # ' ' " " . ' # / , 0 $ , $ #

  01

  • /

  #

  01 '

  ; 0 $ ; <= Compile-time Environment

  Compile-time Environment Class Loader Java Class Libraries Bytecode Verifier Java Source (.java) Just in Just in Java Java Java

  Time Interpreter Bytecodes Java Compiler move locally Virtual or through machine Java network Compiler

  Runtime System Java Operating System Bytecode

  ; 0 $ ; <= 0 $

  !

  6 0 $ 1 01 # # ' 01 # 08.

  2

  > ' 0 $ 3 ( $ 3 , '

  .

  " # ) / # ;

  $ $ 2

  & '& 3 0 $ &&-

  8

  7

  3 0 $

  ' = ' =

  > 0 $

  3 ' ? & # ) , / 3 >

  3

  • 3 / 3 '
  • 3 + "

  0 $ 3 0 $ '

  6 0 $ 0 $

  • 0 $ 1

  3 0 $ $ '

  0 $ # ' 01 - 01 -

  . 0 $ # 0 $ ' " $ 0 $ , 8 ' #

  ,

  8 5 $

  • 3 0 $ $

  $ '

  $ @8 # ? $

  

0 $ ,

  • 3 8 ! 0 $

  = ' # public static void main

  # ' ? # public void paint # public void init# <

  3 , / 0 $ # 3 , / 0 $ #

  > 0 $ ' / #

  01 #

  2 0 $ / / 0 $ #

  / / ! / 0 $ $

  2 > %

  0 $ , import java.awt.*; import java.applet.*; /**

  • This class displays "Hello world!" on the applet window. */

  public class HelloWorld extends Applet { public void paint(Graphics g) {g.drawString("Hello world!", 10, 10); // writes starting 10 pixels over & down }

  }

  0 $ $ " $

  !

  #include

  , ? /. @98 java.applet: // /* */

  • java.awt:

  ! /** */

  0 $ , import java.awt.*; import java.applet.*; /**

  • This class displays "Hello world!" on the applet window. */ public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 10, 10); // writes starting 10 pixels over & down } }

  0 $

  • $
    • . ' 0 $ *

  6 2 $

  HelloWorld.java

  !

  0 $ , import java.awt.*; import java.applet.*; /**

  • This class displays "Hello world!" on the applet window. */ public class HelloWorld extends Applet { public void paint(Graphics g)

  { g.drawString("Hello world!", 10, 10); // writes starting 10 pixels over & down } }

  / , $ 2

  ! init(): memanggil saat page di-load untuk membuat/inisialisasi $ paint(Graphics g): / /

  7 , 6.

  ? # , 7.

  &-7 # 678@6. ?8-.6 / / 5 " ,

  7.

  2 2# 0 $ &;07 .

  <html> <!– COMP519 hello1.html 18.09.2005 --> <head> <title>Hello World Page</title> </head> <body> <p>

  <applet code="HelloWorld.class" height=100 width=100> You must use a Java-enabled browser to view this applet. </applet>

  </p> </body> </html>

  6. + , <html>

  '

  <!– COMP519 hello2.html 18.09.2005 --> <head>

  6.

  <title>Hello World Page</title> </head> <body>

  ;

  <p> <div align="center"> <table border=1> <tr><td> <tr><td> <applet code="HelloWorld.class" height=200 width=200> You must use a Java-enabled browser to view this applet.

  </applet> </td></tr> </table> </div> </p> </body> </html>

  6.

  <html> <!– COMP519 hello3.html 18.09.2005 -->

  • <head> <title>Hello World Page</title>

  , 7.

  </head>

  ' 6.

  <body> <p> <div align="center">

  3

  <table border=1> <tr><td> <tr><td> <applet code="HelloWorld1.class" height=35 width=300>

  ,), <param name=" name " value="Chris">

  , 7.

  <param name=" age " value=20> You must use a Java-enabled browser to view this applet.

  </applet> </td></tr>

  3

  </table> </div>

  $

  </p> </body> </html>

  , import java.awt.*; import java.applet.*; /**

  • This class displays a message based on parameters. */ public class HelloWorld1 extends Applet { public void paint(Graphics g) { String userName = getParameter(" name "); int userAge = Integer.parseInt( getParameter(" age ") ) ; String message1 = "Hello " + userName + "."; String message2 = "On your next birthday, you will be " + String message2 = "On your next birthday, you will be " + (userAge+1) + " years old."; g.drawString(message1, 10, 10);

  g.drawString(message2, 10, 30); } } 6. getParameter

  /

  parseInt parseFloat

  #

  0 $

  3 , % 3 . B .

  3 2 $ #

  2 B /

  2 0 $ $

  0 $ - $ 3 '

  ; >

  %

  $ .

  1

  3 ; # # # # # # #

  2 3 . ' 3 8 /

  $ $

  2 3 $ $ !

  1 B 1 # " C DB C E2D# C E2D# $ & C B

  8

  3 # 3 0 $ ' $ $

  5 $ 3 / '

  3

  7

  5

  • "C E2DB %% & C EB %% $ C F % AB %% = C F2G B %%

    $ C F2H % A2HB %%

  3 E2D $

  2

  3 E2D E2D $ $ EF EF

  3

  2

  2

  , 3 , 0 $ $

  C E# C D# C F C C 2 2

  I CI

  I C I

  I C I 3 ? $ # +

  • 3 ! C C B

  ; &

  • / % + - •
    • double myVal = a + b % d – c * d / b;
      • / %

  $

  3 8 ! double myVal = (a + (b % d)) – ((c * d) / b);

  • ; + ;

  3 ,!

  C I JB

  3 / !

  K EC I JB DC I; JB L

  3 ;

  /

  3 0 $ 3 ; 0 $ / !

  , ! # # / ! # / # / 7 ! # # 8 ( . 8 ( .

  3 ' $ " $

  • " EH# " EH

  2

  " M EH " C EHB

  3 6 !

  " M EH " C EHB

  3 , # $ !

  ) &

  CC

  7N =C

  4 N OC @ N MC N O @

  M

  8 <

  3 . < $ " $ 3 . < $ " $

  2

  if (x != oldx) { System.out.print(“x was changed”); } else {

  System.out.print(“x is unchanged”); }

  4 < if ( myVal > 100 ) { if ( remainderOn == true) { myVal = mVal % 100; } else { myVal = myVal / 100.0; } }

  } else { System.out.print(“myVal is in range”); }

  3 ; $ !

  if ( n == 1 ) { // execute code block #1 } else if ( j == 2 ) {

  // execute code block #2 } else { // if all previous tests have failed, execute code block

  #3 }

  < WRONG! if( i == j ) if ( j == k ) System.out.print(“i equals k”); else

System.out.print(“i is not equal to j”);

  CORRECT! if( i == j ) { if ( j == k )

  System.out.print(“i equals k”); } else System.out.print(“i is not equal to j”); // Correct!

  / switch ( n ) { case 1:

  // execute code block #1 break; case 2: // execute code block #2 break; break; default: // if all previous tests fail then //execute code block #4 break; }

  3 for ( i = 0; i < n; n++ ) {

  // this code body will execute n times // ifrom 0 to n-1 }

  3

  3

  4 4 ! ! for ( j = 0; j < 10; j++ ) { for ( i = 0; i < 20; i++ ){

  // this code body will execute 200 times } }

  / while(response == 1) { System.out.print( “ID =” + userID[n]); n++; response = readInt( “Enter “);

  }

K< L / K< L /

  do { System.out.print( “ID =” + userID[n] ); n++; response = readInt( “Enter ” );

  } while (response == 1);

  ; 3 ;

  / # # /

  2 for ( int i = 0; i &lt; maxID, i++ ) { if ( userID[i] == targetID ) { index = i; break;

  } } // program jumps here after break 3 6 / #

  2

  3 for ( int i = 0; i &lt; maxID; i++ ) { if ( userID[i] != -1 ) continue; System.out.print( “UserID ” + i + “ :” + userID);

  }

  ,)7&amp; -

  3 &amp;&amp; ' 3 ' 3 0 $ 3 ' 2 $

  3

  4 %

  . &amp;&amp;

  3

  7 &amp;

  3

  8 / $

  car Super class auto-

  3

  manual matic Subclasses

  8

  draw() draw()

  ! K

  K B ! ! B

  • ! ! B * ! ! B L L

  3 0 $

  3 #

  2

  3 "

  2 public float convertCelsius(float tempC) { return( ((tempC * 9.0f) / 5.0f) + 32.0 );

  }

  3 !

  4

  2 .

  2 . $

  2 ; '

  2 " # $ %

  $ KL 9 8 # # / 8 "&amp; &amp;; 7" KL

  % $

  3 % %

  &lt; 3 @ ! $

  $

  3

  8 (

  9

  9

  3 &lt;

  C / B B C

  3 %

  3 .

  C / B

  3 $ ' 3 &gt;

  # 222 &amp;$ &amp;$

  3 - $ %

  K CFHBL # K C B P P C BL 3 -

  # 0 $ $

  0 $ - $ &gt; 3 $ ' . 0 $ 3 $ ' . 0 $ 8 3 ' . 0 $ - 3 $ / ' .

  3 $ ' 0 $ 3 $ ' 0 $ 3 $ ' 3 $ '

  0 $ $ 2 0 $

  $ ! #

  5 G EQ EQ RD QA RD QA $ # # + ! ! # ! # + % $

  0 $ # $ $ + 0 $ # $ $ + # # $

  6 # 6 $ # { public static final double PI = 3.14159; // access as Math.PI class Math public static double sqrt(double num) { . . . } // access as in Math.sqrt(9.0) . . . }

  $ $ 2 ) .

  $ "

  $

  $ new

  $ &amp; '(( )* )*

  0 $ $ '$ . { for (int i = 0; i &lt; nums.length; i++) { public void Init(int[] nums) $ ' _____________________________ int nums[] = new int[10]; } nums[i] = 0; } _ Init(nums);

  0 $ java.lang

  '

  3 int length() char charAt(index) int indexOf(substring)

  String str = "foo" String substring(start, end)

  String str = new String("foo"); String toUpperCase() boolean equals(Object) ... java.lang java.lang

  ' '

  3 , 3 , int length

instance variable

  Type [](index) operator

  String toString() ... int[] nums = {1,2,3,4,5}; int[] nums = new int[10];

  • + 3 0 $ $ " $

  

java.util Date Random Timer

ArrayList LinkedList Stack TreeSet HashSet TreeMap HashMap

  , ; $

  3 5 $ + ;

  

#

! / /

  5 3 / # $ @

  @ !

  void drawString(String msg, int x, int y) void setColor(Color color) Color

  # !

  Color.red, Color.blue, Color.black, 2 2 2 import java.awt.*; import java.applet.*; import java.util.Random;

  6 , /** * This class displays lots of "Hello world!"s on the applet window.

  • / public class HelloWorld2 extends Applet { private static final int NUM_WORDS=100;

  private static final Color[] colors = 3 "

  {Color.black,Color.red,Color.blue,Color.green, Color.yellow}; private static Random randy; setColor private int RandomInRange(int low, int high) { return (Math.abs(randy.nextInt()) % (high-low+1)) + low;

  ) $ } }

  $ public void init() { randy = new Random();

  }

  • $

  public void paint(Graphics g) {

  5 for (int i = 0; i &lt; NUM_WORDS; i++) { int x = RandomInRange(

  1 , 14 0); int y = RandomInRange( 10 , 200 ); g.setColor(colors[RandomInRange(0,4)]); g.drawString("Hello world!", x, y); }

  } } You must use a Java-enabled browser to view this applet. &lt;applet code="HelloWorld2.class" height=200 width=200&gt;

  • , -

  ! &lt;PARAM&gt;

  6. -

  getParamete r

  , $

  Dimension dim = getSize(); // stores applet dimensions dim.height dim.width

  /

  import java.applet.*; import java.awt.*; /** import java.util.Random; * This class displays lots of "Hello world!"s on the applet window. , $ 6 { public class HelloWorld3 extends Applet */ private static final Color[] colors = {Color.black,Color.red,Color.blue,Color.green,Color.yellow}; private int numReps; private Dimension dim; private static Random randy;

  getParameter { private int RandomInRange(int low, int high) $ } return (Math.abs(randy.nextInt()) % (high-low+1)) + low; { public void init() public void init()

  # } numReps = Integer.parseInt( getParameter(" reps ") ); dim = getSize(); randy = new Random(); ? ?

  public void paint(Graphics g) { for (int i = 0; i &lt; numReps; i++) {

  getSize int x = RandomInRange(1, dim.width -60); int y = RandomInRange(10, dim.height ); # g.drawString("Hello world!", x, y); g.setColor(colors[RandomInRange(0,4)]); }

  " / } } &lt;applet code="HelloWorld3.class" height=300 width=400&gt; &lt;param name="reps" value=200&gt;

  , @

  3 "

  &amp; @ -

  void drawLine(int x1, int y1, int x2, int y2) void drawRect(int x, int y, int width, int height) void fillRect(int x, int y, int width, int height) void drawOval(int x, int y, int width, int height) void drawOval(int x, int y, int width, int height) void fillOval(int x, int y, int width, int height)

  3 7S, 7! / N # / π

  $ 2 # - π

  C A * % N

  @ , public class Monte1 extends Applet { private static Random randy; private int NUM_POINTS; private int SIZE;

  private int RandomInRange(int low, int high) { CODE OMITTED } private double distance(int x1, int y1, int x2, int y2) { CODE OMITTED } public void init() { randy = new Random(); NUM_POINTS = Integer.parseInt(getParameter("points")); Dimension dim = getSize();

  • paint

  SIZE = Math.min(dim.width, dim.height); }

  init

  / public void paint(Graphics g) { g.setColor(Color.red);

  g.fillOval(0, 0, SIZE, SIZE); for (int i = 0; i &lt; NUM_POINTS; i++) { int x = RandomInRange(0, SIZE); int y = RandomInRange(0, SIZE);

  if (distance(x, y, SIZE/2, SIZE/2) &lt; SIZE/2) { g.setColor(Color.white); } else {

  g.setColor(Color.black); } g.drawLine(x, y, x, y); } &lt;applet code="Monte1.class" height=300 width=300&gt; &lt;param name="points" value=20000&gt; You must use a Java-enabled browser... &lt;/applet&gt; paint

  /

  ; - 3 ! $

  $ # ' /

  • 3

  / '

  ' ! ,

  !

  { public class Monte2 extends Applet . . . . . . private Graphics offScreenGraphics; private Image offScreenImage;

  ; , init { public void init() NUM_POINTS = Integer.parseInt(getParameter("points")); randy = new Random();

  / SIZE = Math.min(dim.width, dim.height); Dimension dim = getSize(); offScreenImage = createImage(SIZE, SIZE); / # ' offScreenGraphics.setColor(Color.red); offScreenGraphics = offScreenImage.getGraphics(); @ offScreenGraphics.fillOval(0, 0, SIZE, SIZE); for (int i = 0; i &lt; NUM_POINTS; i++) { paint init if (distance(x, y, SIZE/2, SIZE/2) &lt; SIZE/2) { int y = RandomInRange(0, SIZE); int x = RandomInRange(0, SIZE);

  / / $ $ $ $ }

  &amp; } } offScreenGraphics.drawLine(x, y, x, y); } &lt;applet code="Monte2.class" height=300 width=300&gt; { public void paint(Graphics g) &lt;/applet&gt; You must use a Java-enabled browser... &lt;param name="points" value=20000&gt;

  • else { offScreenGraphics.setColor(Color.white); offScreenGraphics.setColor(Color.black);

  { public class Monte3 extends Applet . . . public void init() { Dimension dim = getSize(); NUM_POINTS = Integer.parseInt(getParameter("points")); randy = new Random();

  ; } SIZE = Math.min(dim.width, dim.height);

  / / # public void paint(Graphics g) { if (offScreenImage == null) { offScreenImage = createImage(SIZE, SIZE); offScreenGraphics = offScreenImage.getGraphics();

  paint paint g.fillOval(0, 0, SIZE, SIZE); g.setColor(Color.red); offScreenGraphics.fillOval(0, 0, SIZE, SIZE); offScreenGraphics.setColor(Color.red);

  / # $ / for (int i = 0; i &lt; NUM_POINTS; i++) { int x = randomInRange(0, SIZE); if (distance(x, y, SIZE/2, SIZE/2) &lt; SIZE/2) { int y = randomInRange(0, SIZE); int y = randomInRange(0, SIZE); ' offScreenGraphics.setColor(Color.white); } g.setColor(Color.white);

  N # } else { g.setColor(Color.black); offScreenGraphics.setColor(Color.black); / ' } else { } g.drawLine(x, y, x, y); offScreenGraphics.drawLine(x, y, x, y); } } } g.drawImage(offScreenImage, 0, 0, null); &lt;/applet&gt; &lt;applet code="Monte3.class" height=300 width=300&gt; &lt;param name="points" value=20000&gt;

  @98 7 , 3 0 $ " $ @98 @

  9

  8

  6. # " " # " # &lt;

  3 nameLabel = new Label("User's name"); add(nameLabel); nameField = new TextField(20); nameField.setValue("Dave"); add(nameField);

  3 0 $ FlowLayout BorderLayout

  Panel

  { public class Monte4 extends Applet . . .

  

. " ; "

public void paint(Graphics g) { . . . private Label insideLabel; private Label outsideLabel; private TextField insideField; insideField.setText("0"); outsideField.setText("0"); if (distance(x, y, SIZE/2, SIZE/2) &lt; SIZE/2) { . . . { public void init() private TextField outsideField; int value = g.setColor(Color.white); Integer.parseInt(insideField.getText())+1; NUM_POINTS = randy = new Random(); Dimension dim = getSize(); Integer.parseInt(getParameter("points")); } else { insideField.setText(""+value); g.setColor(Color.black); SIZE = Math.min(dim.width, dim.height); setLayout(new BorderLayout()); outsideField.setText(""+value); int value = Integer.parseInt(outsideField.getText())+1; insideLabel = new Label("Inside:"); Panel p = new Panel(); insideField = new TextField(5); p.add(insideLabel); } . . . } outsideField = new TextField(5); p.add(outsideLabel); outsideLabel = new Label("Outside:"); p.add(insideField); } } add(p, BorderLayout.SOUTH); p.add(outsideField);

  &lt;applet code="Monte4.class" height=335 width=300&gt; &lt;param name="points" value=20000&gt;

  7$

  6 3 $ 2 2# " # #

  ActionListener

  6 / '

  public class Monte5 extends Applet implements ActionListener

  $

  dotButton = new Button("Click to generate dots"); dotButton = new Button("Click to generate dots"); dotButton.addActionListener(); actionPerformed

  6 $

  public void actionPerformed(ActionEvent e) { if (e.getSource() == dotButton) { drawDots(); } }

  import java.applet.*; import java.awt.*; , { public void drawCircle()

  import java.awt.event.*; import java.util.Random; public class Monte5 extends Applet public void drawDots() } CODE FOR DRAWING CIRCLE { private Button dotButton; . . . implements ActionListener { Graphics g = getGraphics(); drawCircle(); public void init() { randy = new Random(); } } for (int i = 0; i &lt; NUM_POINTS; i++) { CODE FOR DRAWING DOTS SIZE = dim.width; Dimension dim = getSize(); NUM_POINTS = Integer.parseInt(getParameter("points")); public void paint(Graphics g) { g.drawImage(offScreenImage, 0, 0, null); setLayout(new BorderLayout()); dotButton = new Button("Click to generate dots"); { public void actionPerformed(ActionEvent e) } drawCircle(); add(dotButton, BorderLayout.SOUTH); drawDots(); dotButton.addActionListener(this); if (e.getSource() == dotButton) { } } } &lt;applet code="Monte5.class" height=325 width=300&gt; }

  &lt;param name="points" value=20000&gt;