1 / 8

Java Applets

Java Applets. A First Program. Applet Example. /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into zombies */ import javax.swing.*; // swing package import java.awt.*; // graphics package public class HelloWorld extends JApplet {

ghines
Download Presentation

Java Applets

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. Java Applets A First Program

  2. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into zombies */ import javax.swing.*; // swing package import java.awt.*; // graphics package public class HelloWorld extends JApplet { public void paint( Graphics g ) { g.drawString( "Hello World!", 0, 12 ); } } To create applets, we inherit from the JApplet class so we don’t rewrite the code to make an applet work! “drawString” is a method from the Graphics object named g

  3. Applet Example • Change some things and see if it works, or if you get errors • Change the coordinates from “0,12” to “0,0” – where does it go? • Add a second line of text – how do you align it under “Hello World”? • Try to center the text /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into zombies */ import javax.swing.*; // swing package import java.awt.*; // graphics package public class HelloWorld extends JApplet { public void paint( Graphics g ) { g.drawString( "Hello World!", 0, 12 ); } }

  4. Header Comments • A bloc of comments (crosses several lines) needs an opening and closing comment marker. /* opens a comment; */ closes • An in-line comment (one line only) requires a double slash // at the beginning of the line • Comments will be ignored by the compiler

  5. import import packagename.*; // imports all classes in package OR import packagename.classname; //imports a specific class in a package Examples: import java.awt.*; import javax.swing.*; import javax.swing.JApplet; • Java provides classes with graphics abilities • To use these classes we need to IMPORT the packages (java.awt package) • This way, we can reuse code already written instead of writing it ourselves! • Must be at HEAD of file The * designates to include ALL classes within the package

  6. Class Declaration: extends JApplet • Applets need to extend (inherit) the JApplet class: public class xyz extends JApplet This way, we can reuse code already written instead of writing it ourselves! • This class inherits many capabilities, including what to do to make applets work

  7. Paint Method • public void paint(Graphics g) { g.drawString(“Hello World”, 30, 20); } • We’ve imported the Graphics class, which includes the “drawString()” method, which lets us print text. • To use the Graphics class, we have to declare a variable/object “g” to which we can then apply the drawString() method. • The drawString() method has three parameters: the text, the x-coordinate, and the y-coordinate.

  8. X-Y coordinates • X-coordinate: horizontal measure in pixels, beginning at upper left corner of screen • Y-coordinate: vertical measure in pixels, beginning at upper left corner of screen 0,0 30,20

More Related