Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applet 2019/4/23.

Similar presentations


Presentation on theme: "Applet 2019/4/23."— Presentation transcript:

1 Applet 2019/4/23

2 Applet So far we have focused on Java programs. Another type of program is the applet. Applets are small applications that are accessed on an Internet Server, transported over the Internet. Next page is the simple applet. This applet begins with two import statements. 2019/4/23

3 It declares a new class called MyApplet
Format you can write the simplest Java applet in only a few lines of code, as follows: import java.applet.*; public class MyApplet extends Applet { …… } tells the Java compiler that this applet will be using some or all of the classes defined in the applet package It declares a new class called MyApplet 2019/4/23

4 A simple applet (1) Step SimpleApplet 1
Create a HTML that executes SimpleApplet 2 lecture12h.htm 3 2019/4/23

5 A simple applet (2) 4 Applet Use appletviewer to run it.
Appletviewer lecture12h.htm 4 Applet 2019/4/23

6 Using IE Explorer to browse lecture12h.htm
Applet 2019/4/23

7 Two Lines 2019/4/23

8 Laboratory 2 – input and output
2019/4/23

9 Comparison for three numbers
maximum Max 2 Max 1 Number 4 Number 5 Number 6 Number 1 Number 2 Number 3 2019/4/23

10 Laboratory /* This Applet reads nine numbers, number0, number1, and number2, number3, number4, number5, number6, number7, number8; and prints the largest value and the samllerest value. */ import java.awt.Graphics; // import class Graphics import javax.swing.*; // import package javax.swing public class lab5 extends JApplet { double max; double min; double max0; double min0; double max1; double min1; double max2; double min2; // initialize applet by obtaining values from user public void init() { String numberOne; String numberTwo; String numberThree; ……………….. double number0; double number1; double number2; …………………. numberOne = JOptionPane.showInputDialog( "Enter first floating-point value" ); ………………. number0 = Double.parseDouble( numberOne ); ………………… // Determine largest value for num 1 to 3 max0 = number0; if (number1 > max0) max0 = number1; if (number2 > max0) max0 = number2; // Determine largest value for num 4 to 6 max1 = number3; if (number4 > max1) max1 = number4; if (number5 > max1) max1 = number5; ……………………… // Determine largest value for num 1 to 9 i.e. max 0 to 2 max = max0; if (max1 > max) max = max1; if (max2 > max) max = max2; …………………… } // end method init // draw results in a rectangle on applet background public void paint( Graphics g ) // call superclass version of method paint super.paint( g ); // draw rectangle starting from (15, 10) that is 270 // pixels wide and 20 pixels tall g.drawRect( 15, 10, 270, 20 ); // draw results as a String at (25, 25) g.drawString( "Largest value is " + max + " ,Smallest is " + min, 25, 25 ); } // end method paint } // end class MaxMin9Applet 2019/4/23


Download ppt "Applet 2019/4/23."

Similar presentations


Ads by Google