SlideShare ist ein Scribd-Unternehmen logo
1 von 83
Java Applets
Introducing Applets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Applets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What an applet is ,[object Object],[object Object],[object Object],[object Object],[object Object]
The genealogy of  JApplet java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet
The simplest possible applet import javax.swing.JApplet; public class TrivialApplet extends JApplet { } <applet   code=&quot;TrivialApplet.class&quot;   width=&quot;150&quot;   height=&quot;100&quot;> </applet> TrivialApplet.java TrivialApplet.html
The simplest reasonable applet import java.awt.*; import javax.swing.JApplet; public class HelloWorld extends JApplet {   public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 30, 30); } }
What are the disadvantages of applets? ,[object Object],[object Object],[object Object]
What are the disadvantages of applets? (Cont’d) ,[object Object],[object Object],[object Object],[object Object]
What are the advantages of applets?  ,[object Object],[object Object],[object Object],[object Object]
What are the advantages of applets? (Cont’d) ,[object Object],[object Object],[object Object]
Java Applets Can not ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Running a Java Applet You write Java code using an editor javac MyApp.java appletviewer MyApp.html Text Editor You save the file with a  .java  extension You run the Java compiler ' javac ' You can view the applet with the command ' appletviewer ' This creates a file of  bytecode  with a  .class  extension Web Browser You write a web page in html using an editor You can view the web page from a web browser You save the file with a  .html  extension Java code: MyApp.java Bytecode: MyApp.class Window Web page: MyApp.html Text Editor
Creating an Applet    Open &quot; Notepad &quot; (Start    Programs    Other    Notepad)    Type this in:     Save As  &quot;Greetings.java&quot; (Put the &quot; &quot; round the name otherwise it adds .txt to the end!)    Open a  DOS  Window (Start    MS-DOS Prompt)    Type  javac Greetings.java G:gt; javac Greetings.java G:gt; If it gives an error check you typed it in  exactly  right. import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 50, 50); }  }    If you type  dir Greetings.*  you should see  Greetings.java  and  Greetings.class
Creating the Web Page ,[object Object],<applet code=&quot;name.class&quot; width=www height=hhh></applet> <html> <head> <title>Greetings Applet</title> </head> <body> <applet code=&quot;Greetings.class&quot; width=300 height=200 ></applet> </body> </html> Using Notepad type in the following and save it as  &quot;Greetings.html&quot; : Size of the applet in pixels
Running the Program G:gt; appletviewer Greetings.html In the DOS window type  appletviewer Greetings.html You should see something like this:
Running in a Web Browser In  Netscape  go to the  File  menu then  Open Page ... Press  Choose File... Find your file  Greetings  with the Netscape symbol alongside it (Greetings.html) - click on it and press  Open  (or double click on it) Back in the Open Page dialog press  Open You should see something like: Title Your greeting Message
What does it mean? import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 50, 50); }  } These 2 lines tell the computer to include ( import ) two standard libraries  awt  (Abstract Window Toolkit) and  applet . This line tells the computer to display some text ( a string) on the screen. This line announces that the program ( class ) can be run by anyone ( public ), is called  Greetings  and is an  Applet . This line declares what follows in the { } as a method called paint.  This is where it is displayed in pixels across and down from the top left hand corner This is what is displayed
General Form of an Applet  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
General Form of an Applet  (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Applet Initialization and Termination ,[object Object],[object Object],[object Object],Called just before the applet is terminated. Your applet should override this method if it needs to perform any  cleanup  prior to its destruction.   destroy() Called to suspend execution of the applet. Once stopped, an applet is restarted when the execution environment calls  start() .   stop() Called by the execution environment when an applet should start or resume execution. It is automatically called after  init()   when an applet first begins.   start() Applets do not usually have  main  method; instead they have the  init()  method  that, like  main() , is invoked by the execution environment.  It is the first method called for any applet . It is called  only  once   during the run-time of an applet. init() Comment Method
Applet methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
public void init ( ) ,[object Object],[object Object],[object Object],[object Object]
start( ) ,  stop( )  and  destroy( ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Methods are called in this order ,[object Object],[object Object],[object Object],[object Object],init() start() stop() destroy() do some work
public void  paint(Graphics g) ,[object Object],[object Object],[object Object],[object Object]
repaint( ) ,[object Object],[object Object],[object Object],[object Object],[object Object]
update( ) ,[object Object],[object Object],[object Object]
The paint() method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Graphics Object   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],(0,0) x y
Displaying Strings Using the Graphics Object ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Placing an Applet in a Web Page ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Placing an Applet in a Web Page  (cont’d)   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Colors ,[object Object],[object Object],[object Object],255, 255, 0 1.0F,  1.0F,  0.0F Color.yellow 255, 255, 255 1.0F,  1.0F,  1.0F Color.white 255, 0, 0 1.0F,  0.0F,  0.0F Color.red 255, 175, 175 1.0F,  0.7F,  0.7F Color.pink 255, 200, 0 1.0F,  0.8F,  0.0F Color.orange 255, 0, 255 1.0F,  0.0F,  1.0F Color.magenta RGB Value (integer) RGB Value (float) Color 0, 255, 0 0.0F,  1.0F,  0.0F Color.green 192, 192, 192 0.75F,  0.75F,  0.75F Color.lightGray 64, 64, 64 0.25F,  0.25F,  0.25F Color.darkGray 128, 128, 128 0.5F,  0.5F,  0.5F Color.gray 0, 255, 255 0.0F,  1.0F,  1.0F Color.cyan 0, 0, 255 0.0F,  0.0F,  1.0F Color.blue 0, 0, 0 0.0F,  0.0F,  0.0F Color.black RGB Value (integer) RGB Value (float) Color
Colors  (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Colors  (cont’d) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Drawing Some Shapes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Executing a Java Applet ,[object Object],[object Object],[object Object],[object Object]
Comparing Applets with Applications Has a  restricted access  to the machine resources (cannot open files or run other programs)  {Security reasons} Has an  unrestricted access  to the machine resources Almost always works with  GUI Can work with  command-line  (like what are always doing), or using a  graphical user-interface  (GUI) {More on this in ICS-201} Has to extend  java.applet.Applet  class Doesn’t have to extend any class Starts by the  init()  method Starts by the  main()  method Has to run  inside  another program, called execution environment (like  a web browser  or  an applet viewer ) Runs  independently An Applet An Application
A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java applet Program Output 1  // Fig. 3.6: WelcomeApplet.java 2  // A first applet in Java. 3  4  // Java core packages 5  import  java.awt.Graphics;  // import class Graphics 6  7  // Java extension packages 8  import  javax.swing.JApplet;  // import class JApplet 9  10  public class  WelcomeApplet  extends  JApplet {  11  12  // draw text on applet’s background 13  public void  paint( Graphics g ) 14  { 15  // call inherited version of method paint 16  super .paint( g ); 17  18  // draw a String at x-coordinate 25 and y-coordinate 25 19  g.drawString(  &quot;Welcome to Java Programming!&quot; ,  25 ,  25  ); 20  21  }  // end method paint 22  23  }  // end class WelcomeApplet import  allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends  allows us to inherit the capabilities of class  JApplet . Method  paint  is guaranteed to be called in all applets.  Its first line must be defined as above.
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],5  import  java.awt.Graphics;  // import class Graphics 8  import  javax.swing.JApplet;  // import class JApplet ,[object Object],[object Object]
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],10  public class  WelcomeApplet  extends  JApplet {
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],10  public class  WelcomeApplet  extends  JApplet {
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],10  public class  WelcomeApplet  extends  JApplet {
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],13  public void  paint( Graphics g )
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],13  public void  paint( Graphics g )
3.3 A Simple Java Applet: Drawing a String ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],16  super .paint( g ); 19  g.drawString(  &quot;Welcome to Java Programming!&quot; ,  25 ,  25  );
3.3.1  Compiling and Executing WelcomeApplet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.3.1  Compiling and Executing WelcomeApplet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],1  <html> 2  <applet code =  &quot;WelcomeLines.class&quot;   width =  &quot;300&quot;  height =  &quot;40&quot; > 3  </applet> 4  </html>
3.3.1  Compiling and Executing WelcomeApplet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],1  <html> 2  <applet code =  &quot;WelcomeLines.class&quot;   width =  &quot;300&quot;  height =  &quot;40&quot; > 3  </applet> 4  </html>
3.4 Two More Simple Applets: Drawing Strings and Lines ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1  // Fig. 3.8: WelcomeApplet2.java 2  // Displaying multiple strings in an applet. 3  4  // Java core packages 5  import  java.awt.Graphics;  // import class Graphics 6  7  // Java extension packages 8  import  javax.swing.JApplet;  // import class JApplet 9  10  public class  WelcomeApplet2  extends  JApplet {  11  12  // draw text on applet’s background 13  public void  paint( Graphics g ) 14  { 15  // call inherited version of method paint 16  super .paint( g ); 17  18  // draw two Strings at different locations 19  g.drawString(  &quot;Welcome to&quot; ,  25 ,  25  ); 20  g.drawString(  &quot;Java Programming!&quot; ,  25 ,  40  ); 21  22  }  // end method paint 23  24  }  // end class WelcomeApplet2 1.  import 2. Class  WelcomeApplet2  ( extends JApplet ) 3.  paint 3.1  drawString 3.2  drawString on same x coordinate, but 15 pixels down The two  drawString  statements simulate a newline.  In fact, the concept of lines of text does not exist when drawing strings.
HTML file Program Output 1  <html> 2  <applet code =  &quot;WelcomeApplet2.class&quot;  width =  &quot;300&quot;  height =  &quot;60&quot; > 3  </applet> 4  </html>
WelcomeLines.java  2. Class  WelcomeLines  ( extends JApplet ) 3.  paint 3.1  drawLine 3.2  drawLine 3.3  drawString   Program Output 1  // Fig. 3.10: WelcomeLines.java 2  // Displaying text and lines 3  4  // Java core packages 5  import  java.awt.Graphics;  // import class Graphics 6  7  // Java extension packages 8  import  javax.swing.JApplet;  // import class JApplet 9  10  public class  WelcomeLines  extends  JApplet {  11  12  // draw lines and a string on applet’s background 13  public void  paint( Graphics g ) 14  { 15  // call inherited version of method paint 16  super .paint( g ); 17  18  // draw horizontal line from (15, 10) to (210, 10) 19  g.drawLine(  15 ,  10 ,  210 ,  10  );  20  21  // draw horizontal line from (15, 30) to (210, 30) 22  g.drawLine(  15 ,  30 ,  210 ,  30  );  23  24  // draw String between lines at location (25, 25) 25  g.drawString(  &quot;Welcome to Java Programming!&quot; ,  25 ,  25  ); 26  27  }  // end method paint 28  29  }  // end class WelcomeLines Draw horizontal lines with  drawLine  (endpoints have same y coordinate).
HTML file 1  <html> 2  <applet code =  &quot;WelcomeLines.class&quot;   width =  &quot;300&quot;  height =  &quot;40&quot; > 3  </applet> 4  </html>
3.4 Two More Simple Applets: Drawing Strings and Lines ,[object Object],[object Object],[object Object],[object Object]
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
AdditionApplet.java 1.  import 2. Class  AdditionApplet  ( extends JApplet ) 3. Instance variable  4.  init 4.1 Declare variables 4.2  showInputDialog 4.3  parseDouble 1  // Fig. 3.12: AdditionApplet.java 2  // Adding two floating-point numbers. 3  4  // Java core packages 5  import  java.awt.Graphics;  // import class Graphics 6  7  // Java extension packages 8  import  javax.swing.*;  // import package javax.swing 9  10  public class  AdditionApplet  extends  JApplet { 11  double  sum;  // sum of values entered by user 12  13  // initialize applet by obtaining values from user 14  public void init () 15  { 16  String firstNumber;  // first string entered by user 17  String secondNumber;  // second string entered by user 18  double  number1;  // first number to add 19  double  number2;  // second number to add 20  21  // obtain first number from user 22  firstNumber = JOptionPane.showInputDialog( 23  &quot;Enter first floating-point value&quot;  ); 24  25  // obtain second number from user 26  secondNumber = JOptionPane.showInputDialog( 27  &quot;Enter second floating-point value&quot;  ); 28  29  // convert numbers from type String to type double 30  number1 = Double.parseDouble( firstNumber );  31  number2 = Double.parseDouble( secondNumber ); 32  2 // Adding two floating-point numbers 3 import java.awt.Graphics;  // import class Graphics 5 6 public class AdditionApplet extends JApplet { 7   double sum;  // sum of the values entered by the user 8 9   public void init() 10   { 11   String firstNumber,  // first string entered by user 12   secondNumber;  // second string entered by user 13   double number1,  // first number to add 14   number2;  // second number to add 15 16   // read in first number from user 17   firstNumber = 18   JOptionPane.showInputDialog( 19   &quot;Enter first floating-point value&quot; ); 20 21   // read in second number from user 22   secondNumber = 23   JOptionPane.showInputDialog( 24   &quot;Enter second floating-point value&quot; ); 25 26 27   // convert numbers from type String to type double *  allows any class in the the package to be used. Instance variable  sum  may be used anywhere in the class, even in other methods. Data type  double  can store floating point numbers.
5. Draw applet contents 5.1 Draw a rectangle 5.2 Draw the results HTML file 33  // add numbers 34  sum = number1 + number2; 35  } 36  37  // draw results in a rectangle on applet’s background 38  public void  paint( Graphics g ) 39  { 40  // call inherited version of method paint 41  super .paint( g ); 42  43  // draw rectangle starting from (15, 10) that is 270  44  // pixels wide and 20 pixels tall 45  g.drawRect(  15 ,  10 ,  270 ,  20  ); 46  47  // draw results as a String at (25, 25) 48  g.drawString(  &quot;The sum is &quot;  + sum,  25 ,  25  ); 49  50  }  // end method paint 51  52  }  // end class AdditionApplet 1  <html> 2  <applet code =  &quot;WelcomeLines.class&quot;   width =  &quot;300&quot;  height =  &quot;40&quot; > 3  </applet> 4  </html> 31   // add the numbers 32   sum = number1 + number2; 33   } 34 35   public void paint( Graphics g ) 36   { 37   // draw the results with g.drawString 38   g.drawRect( 15, 10, 270, 20 ); 39   g.drawString( &quot;The sum is &quot; + sum, 25, 25 ); 40   } 41 } 1 <html> 2 <applet code=&quot;AdditionApplet.class&quot; width=300 height=50> 3 </applet> 4 </html> drawRect  takes the upper left coordinate, width, and height of the rectangle to draw.
Program Output
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],5  import  java.awt.Graphics; 8  import  javax.swing.*;
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],10  public class  AdditionApplet  extends  JApplet { 11  double  sum;  // sum of values entered by user
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],11  double  sum;  // sum of values entered by user 14  public void init ()   15  {
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],16  String firstNumber;  // first string entered by user 17  String secondNumber;  // second string entered by user 18  double  number1;  // first number to add 19  double  number2;  // second number to add
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],16  String firstNumber;  // first string entered by user 17  String secondNumber;  // second string entered by user 18  double  number1;  // first number to add 19  double  number2;  // second number to add
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],22  firstNumber = JOptionPane.showInputDialog( 23  &quot;Enter first floating-point value&quot;  );
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],34  sum = number1 + number2; 30  number1 = Double.parseDouble( firstNumber );  31  number2 = Double.parseDouble( secondNumber );
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],33   }   45  g.drawRect(  15 ,  10 ,  270 ,  20  );
3.5  Another Java Applet: Adding Floating-Point Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],  48  g.drawString(  &quot;The sum is &quot;  + sum,  25 ,  25  );
3.6  Viewing Applets in a Web Browser ,[object Object],[object Object],[object Object],[object Object],[object Object]
Fonts ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],A screen font suitable for user input in text fields DialogInput A screen font suitable for labels in dialogs Dialog A font in which all characters have the same width. e.g. Courier Monospaced A font without small segments, e.g. Helvetica SansSerif A font with small segments at the end, e.g. Times New Roman Serif
Fonts  (cont’d)   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Fonts  (cont’d)   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Drawing in an Applet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java’s coordinate system ,[object Object],[object Object],[object Object],[object Object],[object Object],(0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1) (50, 0) (0, 0) (0, 20) (50, 20)
Drawing rectangles ,[object Object],[object Object],[object Object]
Drawing strings ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The complete applet import javax.swing.JApplet; import java.awt.*; // CIT 591 example public class Drawing extends JApplet { public void paint(Graphics g) { g.setColor(Color.BLUE);   g.fillRect(20, 20, 50, 30);   g.setColor(Color.RED);   g.fillRect(50, 30, 50, 30);   g.setColor(Color.BLACK);   g.drawString(&quot;Example JApplet&quot;, 20, 80);   } }
More  java.awt.Graphics  methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Still more  Graphics  methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The HTML page ,[object Object],[object Object],[object Object],[object Object],[object Object]
Other useful JApplet methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVMJava Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Java IO
Java IOJava IO
Java IO
 
Operators in java
Operators in javaOperators in java
Operators in java
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 

Andere mochten auch (20)

Java applets
Java appletsJava applets
Java applets
 
Java applets
Java appletsJava applets
Java applets
 
Java applets
Java appletsJava applets
Java applets
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Applet java
Applet javaApplet java
Applet java
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Java Applets
Java AppletsJava Applets
Java Applets
 
Java applet
Java appletJava applet
Java applet
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
 
applet using java
applet using javaapplet using java
applet using java
 
Java Applet
Java AppletJava Applet
Java Applet
 
ITFT- Applet in java
ITFT- Applet in javaITFT- Applet in java
ITFT- Applet in java
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 
Java Applet
Java AppletJava Applet
Java Applet
 
L18 applets
L18 appletsL18 applets
L18 applets
 
Java Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet ProgramsJava Programming- Introduction to Java Applet Programs
Java Programming- Introduction to Java Applet Programs
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Applet Vs Servlet
Applet Vs ServletApplet Vs Servlet
Applet Vs Servlet
 
Java EE - Servlets API
Java EE - Servlets APIJava EE - Servlets API
Java EE - Servlets API
 
Interface
InterfaceInterface
Interface
 

Ähnlich wie Java: Java Applets

Ähnlich wie Java: Java Applets (20)

Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
 
Java applet basics
Java applet basicsJava applet basics
Java applet basics
 
Applet progming
Applet progmingApplet progming
Applet progming
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Applets
AppletsApplets
Applets
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Applet
AppletApplet
Applet
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Java applet
Java appletJava applet
Java applet
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Applets
AppletsApplets
Applets
 
Smart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdfSmart material - Unit 3 (2).pdf
Smart material - Unit 3 (2).pdf
 
Smart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdfSmart material - Unit 3 (1).pdf
Smart material - Unit 3 (1).pdf
 
Applets in Java
Applets in JavaApplets in Java
Applets in Java
 
Class notes(week 10) on applet programming
Class notes(week 10) on applet programmingClass notes(week 10) on applet programming
Class notes(week 10) on applet programming
 
Applets
AppletsApplets
Applets
 
Applet
AppletApplet
Applet
 
3. applets
3. applets3. applets
3. applets
 

Mehr von Tareq Hasan

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPressTareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryTareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHPTareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array PointerTareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 

Mehr von Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 

Kürzlich hochgeladen

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Kürzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Java: Java Applets

  • 2.
  • 3.
  • 4.
  • 5. The genealogy of JApplet java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet
  • 6. The simplest possible applet import javax.swing.JApplet; public class TrivialApplet extends JApplet { } <applet code=&quot;TrivialApplet.class&quot; width=&quot;150&quot; height=&quot;100&quot;> </applet> TrivialApplet.java TrivialApplet.html
  • 7. The simplest reasonable applet import java.awt.*; import javax.swing.JApplet; public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 30, 30); } }
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. Running a Java Applet You write Java code using an editor javac MyApp.java appletviewer MyApp.html Text Editor You save the file with a .java extension You run the Java compiler ' javac ' You can view the applet with the command ' appletviewer ' This creates a file of bytecode with a .class extension Web Browser You write a web page in html using an editor You can view the web page from a web browser You save the file with a .html extension Java code: MyApp.java Bytecode: MyApp.class Window Web page: MyApp.html Text Editor
  • 14. Creating an Applet  Open &quot; Notepad &quot; (Start  Programs  Other  Notepad)  Type this in:  Save As &quot;Greetings.java&quot; (Put the &quot; &quot; round the name otherwise it adds .txt to the end!)  Open a DOS Window (Start  MS-DOS Prompt)  Type javac Greetings.java G:gt; javac Greetings.java G:gt; If it gives an error check you typed it in exactly right. import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 50, 50); } }  If you type dir Greetings.* you should see Greetings.java and Greetings.class
  • 15.
  • 16. Running the Program G:gt; appletviewer Greetings.html In the DOS window type appletviewer Greetings.html You should see something like this:
  • 17. Running in a Web Browser In Netscape go to the File menu then Open Page ... Press Choose File... Find your file Greetings with the Netscape symbol alongside it (Greetings.html) - click on it and press Open (or double click on it) Back in the Open Page dialog press Open You should see something like: Title Your greeting Message
  • 18. What does it mean? import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString(&quot;Hello World!&quot;, 50, 50); } } These 2 lines tell the computer to include ( import ) two standard libraries awt (Abstract Window Toolkit) and applet . This line tells the computer to display some text ( a string) on the screen. This line announces that the program ( class ) can be run by anyone ( public ), is called Greetings and is an Applet . This line declares what follows in the { } as a method called paint. This is where it is displayed in pixels across and down from the top left hand corner This is what is displayed
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39. Comparing Applets with Applications Has a restricted access to the machine resources (cannot open files or run other programs) {Security reasons} Has an unrestricted access to the machine resources Almost always works with GUI Can work with command-line (like what are always doing), or using a graphical user-interface (GUI) {More on this in ICS-201} Has to extend java.applet.Applet class Doesn’t have to extend any class Starts by the init() method Starts by the main() method Has to run inside another program, called execution environment (like a web browser or an applet viewer ) Runs independently An Applet An Application
  • 40.
  • 41. Java applet Program Output 1 // Fig. 3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeApplet extends JApplet { 11 12 // draw text on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super .paint( g ); 17 18 // draw a String at x-coordinate 25 and y-coordinate 25 19 g.drawString( &quot;Welcome to Java Programming!&quot; , 25 , 25 ); 20 21 } // end method paint 22 23 } // end class WelcomeApplet import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet . Method paint is guaranteed to be called in all applets. Its first line must be defined as above.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53. 1 // Fig. 3.8: WelcomeApplet2.java 2 // Displaying multiple strings in an applet. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeApplet2 extends JApplet { 11 12 // draw text on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super .paint( g ); 17 18 // draw two Strings at different locations 19 g.drawString( &quot;Welcome to&quot; , 25 , 25 ); 20 g.drawString( &quot;Java Programming!&quot; , 25 , 40 ); 21 22 } // end method paint 23 24 } // end class WelcomeApplet2 1. import 2. Class WelcomeApplet2 ( extends JApplet ) 3. paint 3.1 drawString 3.2 drawString on same x coordinate, but 15 pixels down The two drawString statements simulate a newline. In fact, the concept of lines of text does not exist when drawing strings.
  • 54. HTML file Program Output 1 <html> 2 <applet code = &quot;WelcomeApplet2.class&quot; width = &quot;300&quot; height = &quot;60&quot; > 3 </applet> 4 </html>
  • 55. WelcomeLines.java 2. Class WelcomeLines ( extends JApplet ) 3. paint 3.1 drawLine 3.2 drawLine 3.3 drawString Program Output 1 // Fig. 3.10: WelcomeLines.java 2 // Displaying text and lines 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeLines extends JApplet { 11 12 // draw lines and a string on applet’s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super .paint( g ); 17 18 // draw horizontal line from (15, 10) to (210, 10) 19 g.drawLine( 15 , 10 , 210 , 10 ); 20 21 // draw horizontal line from (15, 30) to (210, 30) 22 g.drawLine( 15 , 30 , 210 , 30 ); 23 24 // draw String between lines at location (25, 25) 25 g.drawString( &quot;Welcome to Java Programming!&quot; , 25 , 25 ); 26 27 } // end method paint 28 29 } // end class WelcomeLines Draw horizontal lines with drawLine (endpoints have same y coordinate).
  • 56. HTML file 1 <html> 2 <applet code = &quot;WelcomeLines.class&quot; width = &quot;300&quot; height = &quot;40&quot; > 3 </applet> 4 </html>
  • 57.
  • 58.
  • 59. AdditionApplet.java 1. import 2. Class AdditionApplet ( extends JApplet ) 3. Instance variable 4. init 4.1 Declare variables 4.2 showInputDialog 4.3 parseDouble 1 // Fig. 3.12: AdditionApplet.java 2 // Adding two floating-point numbers. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.*; // import package javax.swing 9 10 public class AdditionApplet extends JApplet { 11 double sum; // sum of values entered by user 12 13 // initialize applet by obtaining values from user 14 public void init () 15 { 16 String firstNumber; // first string entered by user 17 String secondNumber; // second string entered by user 18 double number1; // first number to add 19 double number2; // second number to add 20 21 // obtain first number from user 22 firstNumber = JOptionPane.showInputDialog( 23 &quot;Enter first floating-point value&quot; ); 24 25 // obtain second number from user 26 secondNumber = JOptionPane.showInputDialog( 27 &quot;Enter second floating-point value&quot; ); 28 29 // convert numbers from type String to type double 30 number1 = Double.parseDouble( firstNumber ); 31 number2 = Double.parseDouble( secondNumber ); 32 2 // Adding two floating-point numbers 3 import java.awt.Graphics; // import class Graphics 5 6 public class AdditionApplet extends JApplet { 7 double sum; // sum of the values entered by the user 8 9 public void init() 10 { 11 String firstNumber, // first string entered by user 12 secondNumber; // second string entered by user 13 double number1, // first number to add 14 number2; // second number to add 15 16 // read in first number from user 17 firstNumber = 18 JOptionPane.showInputDialog( 19 &quot;Enter first floating-point value&quot; ); 20 21 // read in second number from user 22 secondNumber = 23 JOptionPane.showInputDialog( 24 &quot;Enter second floating-point value&quot; ); 25 26 27 // convert numbers from type String to type double * allows any class in the the package to be used. Instance variable sum may be used anywhere in the class, even in other methods. Data type double can store floating point numbers.
  • 60. 5. Draw applet contents 5.1 Draw a rectangle 5.2 Draw the results HTML file 33 // add numbers 34 sum = number1 + number2; 35 } 36 37 // draw results in a rectangle on applet’s background 38 public void paint( Graphics g ) 39 { 40 // call inherited version of method paint 41 super .paint( g ); 42 43 // draw rectangle starting from (15, 10) that is 270 44 // pixels wide and 20 pixels tall 45 g.drawRect( 15 , 10 , 270 , 20 ); 46 47 // draw results as a String at (25, 25) 48 g.drawString( &quot;The sum is &quot; + sum, 25 , 25 ); 49 50 } // end method paint 51 52 } // end class AdditionApplet 1 <html> 2 <applet code = &quot;WelcomeLines.class&quot; width = &quot;300&quot; height = &quot;40&quot; > 3 </applet> 4 </html> 31 // add the numbers 32 sum = number1 + number2; 33 } 34 35 public void paint( Graphics g ) 36 { 37 // draw the results with g.drawString 38 g.drawRect( 15, 10, 270, 20 ); 39 g.drawString( &quot;The sum is &quot; + sum, 25, 25 ); 40 } 41 } 1 <html> 2 <applet code=&quot;AdditionApplet.class&quot; width=300 height=50> 3 </applet> 4 </html> drawRect takes the upper left coordinate, width, and height of the rectangle to draw.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79. The complete applet import javax.swing.JApplet; import java.awt.*; // CIT 591 example public class Drawing extends JApplet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); g.setColor(Color.BLACK); g.drawString(&quot;Example JApplet&quot;, 20, 80); } }
  • 80.
  • 81.
  • 82.
  • 83.