1 / 18

Lecture 09 Applets

Lecture 09 Applets. Introduction to Applets. Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler getting a .class file as before Create an HTML file (say HelloWorld.HTML in same directory as .class file) and include in this

Download Presentation

Lecture 09 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. Lecture 09 Applets

  2. Introduction to Applets • Applets should NOT have main method but rather init, stop, paint etc • They should be run through javac compiler getting a .class file as before • Create an HTML file (say HelloWorld.HTML in same directory as .class file) and include in this • <applet code="Example.class" width=500 height=200 > </applet> in simplest case with no parameters where applet will run in window of given width and height (in pixels)

  3. More on Applets • Given the following HTML <APPLET CODE="StockGraph" CODEBASE="http://www.javasoft.com/applets/"WIDTH=200 HEIGHT=200> </APPLET> • Runs the "StockGraph.class" executable as an applet

  4. Java applets • Java application executes from a command window • an applet is a Java program that runs in the appletviewer or in a Web browser • The appletviewer (or browser) executes an applet when a HTML document containing the applet is opened in the appletviewer (or browser).

  5. Applets in Code Warrior • Start a new project in CW • choose java stationary and applet (in a window jdk1.3) • Project with three source files: TrivialApplet.java, TrivialApplet.html and TrivialAppletDebug.html • Open the java file. • import java.awt.*; • import java.applet.Applet; • We import objects from two packages which supports graphical user interface: AWT (abstract windowing tool). There is a newest version of class Applet called JApplet, from the new package java.swing (SWING is built on awt and exdends its capabilities).

  6. Applets in Code Warrior • We import objects from two packages which supports graphical user interface: • AWT (abstract windowing tool). • the new package java.swing (SWING is built on awt and exdends its capabilities) • public class TrivialApplet extends Applet The extends keyword followed by a class name indicates the class (in this case Applet) from which our new class inherits existing pieces. In this inheritance relationship, Applet is called the superclass or base class and TrivialApplet is called the subclass or derived class. We will discuss inheritance in detail later. Using inheritance a new class TrivialApplet has the data and methods of the Applet class as well as the new features, like a method paint(), which overrides Applet's method paint(). • three methods init, start and paint that are guaranteed to be called automatically for you when any applet begins execution. • called exactly in that order

  7. init, start methods • init • public void init() • Called by the browser or applet viewer to inform this applet that it has been loaded into the system. It is always called before the first time that the start method is called. A subclass of Applet should override this method if it has initialization to perform. • start • public void start() • Called by the browser or applet viewer to inform this applet that it should start its execution. It is called after the init method and each time the applet is revisited in a Web page. A subclass of Applet should override this method if it has any operation that it wants to perform each time the Web page containing it is visited. For example, an applet with animation might want to use the start method to resume animation, and the stop method to suspend the animation.

  8. init, start and paint methods • The appletviewer or browser expects each of these methods to be defined so it can provide a consistent start-up sequence for an applet. • Some sample code import java.awt.*; import java.applet.Applet; public class test extends Applet { // Initialize the applet public void init() { } // Paint graphics on the screen public void paint(Graphics g) { g.drawString( "Hello World!!!", 30, 30 ); } }

  9. drawString method • The first argument to drawString is the string to draw • The last two arguments in the list, 30 and 30, are the coordinates at which the bottom-left corner of the string should be drawn in the applet's area on the screen • Coordinates are measured from the upper-left corner of the applet in pixels: first 30 (x-coordinate) is a number of pixel to the right, and the second parameter (y-coordinate) is number of pixel down.

  10. Executing a Java Applet • provide an HTML text file that indicates which applet the appletviewer (or browser) should load and execute • <applet codebase="Java Classes" code="TrivialApplet.class" width=200 height=200></applet>

  11. Java Resources • There is a large number of Java applet resources available to you. The best place to start is • http://java.sun.com/applets/index.html

  12. Event Listeners • If a class want to respond to a user event, it must implement the interface (we will discuss interfaces in a few lectures) that deals with the event • These interfaces are called event listeners. • Each listener handles a specific kind of event • A sample of event listeners: • ActionListener - action event that generated by a user taking an action such as a click on a button. • AdjustmentListener - an event that generated when the componenet such as a scrollbar is moved. • ItemListener - item event that generated when an item such as a check box has been changed.

  13. Event Listeners • more sample event listeners: • KeyListener - keyboard event that occurs when a user types on a keyboard. • MouseListener - mouse event that generated by a mouse click. • MouseMotionListener - mouse event that generated by a mouse movement. • WindowListener - window event that generated by window move or adjustment.

  14. an example of a proper class declaration: public class Foo extends Applet implements ActionListener, MouseListener { ... } • By making a class event listener you have to set up a specific type of event • You have to add a matching listener. • Once a component (a button, for example) is created you can add one of following methods associated with it:

  15. Listener Methods • addActionListener() - Button, CheckBox, ComboBox, TextField, RadioButton. • addAdjustmentListener() - ScrollBar. • addItemListener() - Button, CheckBox, ComboBox, RadioButton. • addKeyListener() - all components. • addMouseListener() - all components. • addMouseMotionListener() - all components.

  16. Names • names of the component may be different depending on which package you use • All above components are in AWT (import java.applet.Applet; ) • In the new package SWING (import javax.swing.JApplet; ), all above components have the capital letter J in front: JButton, JCheckBox and so on.

  17. An Example • create a button and associate an action event listener • JButton b = new JButton("give him a name"); • b.addActionListener(this); • The ActionListener interface contains only one method actionPerformed() • public void actionPerformed(ActionEvent e) { //what to do when a user //clicks the button? }

  18. Example • public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonOne) ... else ... } • If you have more than one componenet has an event listener, you have to figure out which one does what. • The class ActionEvent (a parameter in actionPerformed()) has a method getSource() which can help to determine which component generated the event.

More Related