1 / 38

CSE 1340 Class 5

CSE 1340 Class 5. 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

patch
Télécharger la présentation

CSE 1340 Class 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1340Class 5

  2. 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

  3. 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

  4. 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

  5. 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();

  6. 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

  7. 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

  8. import javax.swing.JOptionPane; The import statement includes particular classes from a particular package (folder) in the java library of classes

  9. 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.)

  10. 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.

  11. import javax.swing.JOptionPane; public class Welcome4 { public static void main(String args[ ]) { JOptionPane.showMessageDialog (null,”Welcome\nto\nJava\n” + “Programming!”); System.exit(0); } }

  12. import javax.swing.JApplet; The import statement includes particular classes from a particular package (folder) in the java library of classes

  13. 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;

  14. 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

  15. 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); } }

  16. 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>

  17. 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>

  18. import javax.swing.JOptionPane; public class Addition { public static void main(String argvs[ ]) {String firstNumber, secondNumber; int number1, number2; int sum;

  19. firstNumber = JOptionPane.showInputDialog( “Enter first integer”); secondNumber= JOptionPane.showInputDialog( “Enter second integer”); number1= Integer.parseInt(firstNumber); number2=Integer.parseInt(secondNumber); sum = number1 + number2;

  20. JoptionPane.showMessageDialog( null, “The sum is “ + sum,”Results”, JOptionPane.PLAIN_MESSAGE); System.exit(0); } }

  21. Class Addition written as an Applet

  22. import java.awt.Graphics; import javax.swing.*; // make available all classes // (not all packages BUT all classes in the // swing package (folder)

  23. import java.awt.Graphics; import javax.swing.*; public class AdditionJApplet extends JApplet { double sum; public void init() {String firstNumber, secondNumber; double number1, number2;

  24. 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; }

  25. public void paint(Graphics g) { g.drawRect(15, 10, 270, 20); g.drawString(“The sum is “ + sum,25,25); } }

  26. html file associate with AdditionApplet.java file <html> <applet code = “AdditionApplet.class” width = 300 height = 50> </applet> </html>

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. Creating an HTML Host Document • A host program, such as a Web page executes the applet

  35. 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

  36. 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

More Related