Applets 26-Jul-16 Applets An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page to tell the browser about the applet You don’t need to supply a main method; the browser does that When you write an applet, you are writing only part of a program You supply certain methods that the browser calls For security reasons, applets run in a sandbox: they have no access to the client’s file system 2 Applet Support Most modern browsers support applets if they have the appropriate plugin The best support isn't a browser, but the standalone program appletviewer You need a version of the plugin at least as new as the version of Java you are trying to run The plugin is usually installed automatically when you install a new version of Java appletviewer uses only the applets in a Web page, and ignores everything else If you want to reach the widest audience, you should use Java 1.1, and AWT instead of Swing This is because Microsoft no longer supports Java 3 What an applet is You write an applet by extending the class JApplet JApplet is just a class like any other; you can even use it in applications if you want When you write an applet, you are only writing part of a program; the browser supplies the main method Once you understand how applets work, you can write a program that function either as an applet or as an application—just write a main method that calls the right methods at the right time Such programs have the ugly name “appletcations” 4 The genealogy of JApplet java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet 5 The simplest possible applet TrivialApplet.java import javax.swing.JApplet; public class TrivialApplet extends JApplet { } TrivialApplet.html <applet code="TrivialApplet.class" width="150" height="100"> </applet> 6 The simplest reasonable applet import java.awt.*; import javax.swing.JApplet; public class HelloWorld extends JApplet { public void paint(Graphics g) { g.drawString("Hello World!", 30, 30); } } 7 Applet methods public void init () public void start () public void stop () public void destroy () public void paint (Graphics) Also: public void repaint() public void update (Graphics) public void showStatus(String) public String getParameter(String) 8 Why an applet works You write an applet by extending the class JApplet Applet defines methods init( ), start( ), stop( ), paint(Graphics), destroy( ) These methods do nothing--they are stubs You make the applet do something by overriding these methods When you create an applet in BlueJ, it automatically creates sample versions of these methods for you 9 public void init ( ) init() is the first method to execute init() is an ideal place to initialize variables init() is the best place to define the GUI Components (buttons, text fields, checkboxes, etc.), lay them out, and add listeners to them Almost every applet you ever write will have an init( ) method 10 start( ), stop( ) and destroy( ) start() and stop( ) are used when the Applet is doing timeconsuming calculations that you don’t want to continue when the page is not in front public void start() is called: public void stop( ) is called: Right after init( ) Each time the page is loaded and restarted When the browser leaves the page Just before destroy( ) public void destroy( ) is called after stop( ) Use destroy() to explicitly release system resources (like threads) System resources are usually released automatically 11 Methods are called in this order init() start() do some work stop() destroy() init and destroy are only called once each start and stop are called whenever the browser enters and leaves the page do some work is code called by your listeners paint is called when the applet needs to be repainted 12 public void paint(Graphics g) Needed if you do any drawing or painting other than just using standard GUI Components Any painting you want to do should be done here, or in a method you call from here Painting that you do in other methods may or may not happen Never call paint(Graphics), call repaint( ) 13 repaint( ) Call repaint( ) when you have changed something and want your changes to show up on the screen You do not need to call repaint() when something in Java’s own components (Buttons, TextFields, etc.) You do need to call repaint() after drawing commands (drawRect(...), fillRect(...), drawString(...), etc.) repaint( ) is a request--it might not happen When you call repaint( ), Java schedules a call to update(Graphics g) 14 update( ) When you call repaint( ), Java schedules a call to update(Graphics g) Here's what update does: public void update(Graphics g) { // Fills applet with background color, then paint(g); } 15 Sample Graphics methods A Graphics is something you can paint on g.drawString(“Hello”, 20, 20); Hello g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red); 16 Painting at the right time is hard When you modify common components (JButtons, JLabels, JTextFields, etc.), Java keeps the screen display up to date When you paint on a Graphics object, you have to make your changes appear on the screen To help ensure your changes appear on screen, follow these rules: Rule #1: Never call paint(Graphics g), call repaint( ) Rule #2: Do all your painting in paint, or in a method that is called from paint Rule #3: If you paint on any Graphics other than the Applet’s, call its update method from the Applet’s paint method Rule #4. Do your painting in a separate Thread These rules aren't perfect, but they should help If you follow these rules and the screen still doesn’t change, I probably won’t be able to find the problem, either :-( 17 Other useful JApplet methods System.out.println(String s) Works from appletviewer, not from browsers Automatically opens an output window. showStatus(String s) displays the String in the applet’s status line. Each call overwrites the previous call. You have to allow time to read the line! 18 Applets are not magic! Anything you can do in an applet, you can do in an application. You can do some things in an application that you can’t do in an applet. If you want to access files from an applet, it must be a “trusted” applet. Trusted applets are beyond the scope of this course. 19 Structure of an HTML page HTML HEAD BODY TITLE (content) Most HTML tags are containers. A container is <tag> to </tag> 20 HTML <html> <head> <title> Hi World Applet </title> </head> <body> <applet code="HiWorld.class" width=300 height=200> <param name="arraysize" value="10"> </applet> </body> </html> 21 The <applet> tag The <applet ...> ... </applet> tag is what tells your browser to put an applet here <applet> has three required attributes: code="HiWorld.class" tells the browser where to find the code Alternatively, if your code is in a .jar file, you can say archive="HiWorld.jar“ width="300" tells the browser how many pixels wide to make the applet window A typical monitor resolution is 72 pixels/inch height="200" tells the browser how many pixels high to make the applet window In a browser, you cannot resize the applet; but in appletviewer, you can resize the applet 22 <param name="arraysize" value="10"> You can put parameters in the HTML page, to be read by the applet Parameters go between <applet> and </applet> All parameters are read by the applet as Strings To read a parameter, use the applet method public String getParameter(String name) Example: String s = getParameter("arraysize"); try { size = Integer.parseInt (s) } catch (NumberFormatException e) {…} 23 The End 24