380 likes | 499 Views
This guide provides comprehensive objectives for differentiating between Java applications and applets, as well as creating an applet from Java source code. Learn to write code to display graphics, text, and colors within an applet and create an accompanying HTML host document. Gain insights into object-oriented programming, Java GUI development using JOptionPane, and the importance of import statements in accessing Java classes. Step-by-step code examples illustrate the use of the Graphics class, the creation of applets, and the structure of Java applications.
E N D
Objectives • Differentiate between an application and an applet • Create an applet from Java source code • Write code to display a graphic, text, color, and the date in an applet • Create an HTML host document
Class 3 Objectives • Get an elementary understand object-orientation • Write a JAVA program with a Graphical User Interface (GUI) using the JOptionPane class • Differentiate between a Java Applet and a Java Application • Write a Java Applet
Import Packages • Use the import statement to access classes in the SDK • The java.lang package is automatically imported • Place the import statement before the class header • Use an asterisk (*) after the package name and period delimiter to import all necessary classes in the package
Call a System Date Constructor • Use the Date class in the java.util package to access the system date • Store the Date in an object variable • Declare the object variable by calling the Date constructor • The constructor is a method denoted by the new keyword followed by the object type and parentheses • Declaration syntax: objectType variableName = new objectType();
java applet public class WelcomeApplet2 extends JApplet{ public void paint( Graphics g) { g.drawString( “Welcome to”, 25, 25 ) g.drawString(“Java Programming”, 25, 40); } } 1. Name two methods in the above code. paint drawString 2. What kind of data is being passed to the paint method? A Graphics object
java applet public class WelcomeApplet2 extends JApplet { public void paint( Graphics g) { g.drawString( “Welcome to”, 25, 25 ) g.drawString(“Java Programming”, 25, 40); } } 3. What is the name of the Graphics object g 4. What kind of data is being passed to the drawString method? A string and two integers
import javax.swing.JOptionPane; The import statement includes particular classes from a particular package (folder) in the java library of classes
import javax.swing.JOptionPane; public class Welcome4 All code in java must be inside a class definition; the above line begins the definition of a class called Welcome4. The rules for class names are the same as the rules for any user-defined Java identifier (variables, constants, class names, etc.)
import javax.swing.JOptionPane; public class Welcome4 { public static void main(String args[ ]) Every Java application (one run on a desktop, not over the web through an html file) must have a method called main; that is beginning/entry point of execution of your Java application.
import javax.swing.JOptionPane; public class Welcome4 { public static void main(String args[ ]) { JOptionPane.showMessageDialog (null,”Welcome\nto\nJava\n” + “Programming!”); System.exit(0); } }
import javax.swing.JApplet; The import statement includes particular classes from a particular package (folder) in the java library of classes
import javax.swing.JApplet; The import statement includes particular classes from a particular package (folder) in the java library of classes import java.awt.Graphics;
import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet2 extends JApplet Applets (programs that run over the web through an html file) don’t have a method main; instead we want our class to be an applet type so we use the word extends to make our class a JApplet Keyword extends gives us inheritance in Java
import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet2 extends JApplet { public void paint(Graphics g) { g.drawString(”Welcome to ” ,25,25); g.drawString(“Java Programming!”, 25,40); } }
Every applet must have an associated html file that contains the name of the class created with the .java file; the following code would be entered into a text editor and saved as WelcomeApplet2.html <html> <applet code=“WelcomeApplet2.class” width = 300 height =30> </applet> </html>
Every applet must have an associated html file that contains the name of the class created with the .java file; the following code would be entered into a text editor and saved as WelcomeApplet2.html <html> <applet code=“WelcomeApplet2.class” width = 300 height =30> </applet> </html>
import javax.swing.JOptionPane; public class Addition { public static void main(String argvs[ ]) {String firstNumber, secondNumber; int number1, number2; int sum;
firstNumber = JOptionPane.showInputDialog( “Enter first integer”); secondNumber= JOptionPane.showInputDialog( “Enter second integer”); number1= Integer.parseInt(firstNumber); number2=Integer.parseInt(secondNumber); sum = number1 + number2;
JoptionPane.showMessageDialog( null, “The sum is “ + sum,”Results”, JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
import java.awt.Graphics; import javax.swing.*; // make available all classes // (not all packages BUT all classes in the // swing package (folder)
import java.awt.Graphics; import javax.swing.*; public class AdditionJApplet extends JApplet { double sum; public void init() {String firstNumber, secondNumber; double number1, number2;
firstNumber = JOptionPane.showInputDialog( “Enter first floating-point value”); secondNumber= JOptionPane.showInputDialog( “Enter second floating-point value”); number1= Double.parseDouble(firstNumber); number2= Double.parseDouble(secondNumber); sum = number1 + number2; }
public void paint(Graphics g) { g.drawRect(15, 10, 270, 20); g.drawString(“The sum is “ + sum,25,25); } }
html file associate with AdditionApplet.java file <html> <applet code = “AdditionApplet.class” width = 300 height = 50> </applet> </html>
Moving to the Web • Characteristics of an applet • Applets run within a browser/viewer and are usually delivered to the client machine via the Web • Applets cannot use system resources or files on the client machine • Convert the application into an applet • Import two packages • Change the class name and extend the Applet class • Include a paint method to draw text and display color and a graphic
Import Applet Packages • Applet package (java.applet.*) • Allows applets to inherit attributes and methods • AWT package (java.awt.*) • Provides access to color, draw methods, and GUI elements
Change the Class Name and Extend the Applet Class • Change the class name and file name to create a new applet file • Edit the comment header in the applet file • Add “extends Applet” in the class header to inherit from the superclass, Applet • Provides the init() method to load the applet in the browser window
The paint() Method • Accepts a Graphics object as a parameter • The Graphics object is commonly referred to by the variable name g • The variable g is created and initialized in the init() method • The variable g is a reference variable, or a specific instance of an object • The return type is void
The drawString() Method • Displays text in the applet window • Accepts three arguments • The String data • If the data is not a String object, convert it to a String object using the toString() method • The horizontal and vertical coordinates of the String • The coordinates are measured in pixels • Called by the Graphics object, g
Draw an Image • Declare an Image object • Use the getImage() method to load the image • The getImage() method calls the getDocumentBase() method to pull the image from the current folder • Use the drawImage() method to set the coordinates of the image
Set the Background Color • Use the setBackground() method to change the background color of the applet window • The setBackground() method does not need to be called from a reference variable
Creating an HTML Host Document • A host program, such as a Web page executes the applet
Creating an HTML Host Document • The Web page contains HTML tags to define a section or format • A tag consists of a start tag, denoted by <> and an end tag, denoted by </> • The tag, <APPLET>…</APPLET>, informs the browser of the applet • The applet tag encloses the name of the bytecode applet file and the width and height of the applet window
Running an Applet • An applet is run by opening the HTML host document • In TextPad, use the Run Java Applet command • At the command prompt, type appletviewer followed by the name of the host document • Use Applet Viewer to test the applet • Ignores irrelevant HTML code • Uses less memory than a browser • Does not have to be Java-enabled