Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in...
Transcript of Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in...
![Page 1: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/1.jpg)
Java Applets
CSCI 136: Fundamentals of Computer Science II • Keith Vertanen • Copyright © 2011
<html> <body> <h2>Don't panic about frogs!</h2> <applet code="PanicAppletParam.class" width="500" height="500"> <param name="image" value="frog.jpg"> <param name="sound" value="frog.wav"> <param name="delay" value="500"> Java plugin not installed </applet> </body> </html>
![Page 2: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/2.jpg)
Overview
2
• Complete slideshow GUI
• Applets
– A Java GUI inside your browser! • Quite similar to building a desktop Java GUI
– Important methods
– Drawing images
– Playing audio
– Getting input parameters
– Double buffering
![Page 3: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/3.jpg)
Last time: programming activity
• Create an image slideshow GUI
– Loads images specified on command line
– Outputs height/width, ignore bogus files
– Cycles between images every second
3
% java ShowImages cat.jpg frog.jpg dog.jpg bogus.jpg cat.jpg [240 x 160] frog.jpg [240 x 118] dog.jpg [240 x 228]
![Page 4: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/4.jpg)
Applets
• Applets
– Java programs embedded in a web page • <applet> tag in the HTML
• Appears in a box like other page elements (e.g. images)
– But you can interact with it, pushing buttons, etc.
• Requires a Java plug-in
• Not support by some browsers (iPhone)
– Code very similar to a Java desktop JFrame app • Class that extends JApplet
– Operates in a security "sandbox" • Not allowed to access:
– Local file system, clipboard, arbitrary web sites, etc.
4
![Page 5: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/5.jpg)
Example applet: plug-in approval
5
![Page 6: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/6.jpg)
Example applet: running
6
![Page 7: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/7.jpg)
Example applet: HTML code
7
<html> <body> <h2>BouncingBall Internet Edition</h2> <applet code="BouncingBallApplet.class" width="300" height="300"> Java plug-in not installed </applet> </body> </html>
Byte code generated by: % javac BoucningBallApplet.java
![Page 8: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/8.jpg)
JApplet methods
8
Method Description
void init() Called once by the applet container. This should initialize widgets, load images, sounds, threads, etc.
void start() Called after init completes. Also called whenever user browses to another site and later returns to page. May start animations or threads.
void paint(Graphics g) Called after init and start. Also whenever window resized, moves, or repaint called.
void stop() Called when user leaves the applet's web page for another page. May stop animations or threads.
void destroy() Called when browser closed, cleans up resources.
• Override some JApplet methods
– Otherwise you get default implementation from parent
![Page 9: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/9.jpg)
Applet example
• Counter applet
– Increase count by 1 every second • Create a Thread to manage this
– Reset button sets back to zero • Hook up an ActionListener to do reset
– Bar graph showing current count • Use paint method to draw directly, no need for a custom JPanel
9
<html> <body> <h2>Let's count to infinity!</h2> <applet code="CounterApplet.class" width="300" height="50">Java plugin not installed </applet> </body> </html>
CounterApplet.java
![Page 10: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/10.jpg)
For comparison…
• Counter application
– Same goal, but as an application extending JFrame
– Code differences: • Moved widget setup to constructor
• Layout setup a little more complicated
– Must create a custom JPanel for the graph
• Create a main method that created object
10
CounterApplet.java
Counter.java
![Page 11: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/11.jpg)
11 PanicApplet.java
![Page 12: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/12.jpg)
Multimedia applets
• Using images in your applet
– Put in same directory as applet's *.class file:
– Or on a web server somewhere:
– Drawing in the paint(Graphics g) method:
12
img = getImage(getDocumentBase(), "dont_panic.png");
img = getImage(new URL( "http://katie.mtech.edu/classes/csci136/dont_panic.png"));
if (img != null) g.drawImage(img, x, y, this);
![Page 13: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/13.jpg)
Multimedia applets
• Playing sounds in your applet
– Put in same directory as applet's *.class file:
– Or on a web server somewhere:
13
audio = getAudioClip(getDocumentBase(), "boink.wav");
img = getAudioClip( "http://katie.mtech.edu/classes/csci136/boink.wav"));
Method Description
void play() Starts playing this audio clip.
void stop() Stops playing this audio clip.
void loop() Starts playing this audio clip in a loop.
![Page 14: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/14.jpg)
Getting input
• Applets may take input
– Specified as parameters in the HTML
– Name/value string pairs • Parse into other data type as necessary
• Example:
– Make flashing image/sound applet configurable
– Different images, sounds, and time delay
14
<html> <body> <h2>Don't panic about frogs!</h2> <applet code="PanicAppletParam.class" width="500" height="500"> <param name="image" value="frog.jpg"> <param name="sound" value="frog.wav"> <param name="delay" value="500"> Java plugin not installed </applet> </body> </html>
![Page 15: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/15.jpg)
15 PanicAppletParam.java
![Page 16: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/16.jpg)
Double buffering
• Problem: PanicApplet flickers a lot
• Solution: Use double buffering
– Technique that prevents flicker
– Draw things to a background buffer first
– Then quick copy to screen
16
![Page 17: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/17.jpg)
17
... private Image img; private int x = 0; private int y = 0; public void init() { img = getImage(getDocumentBase(), "frog.jpg"); addMouseMotionListener(this); } public void paint(Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); if (img != null) g.drawImage(img, x, y, this); } ...
... private Image img; private int x = 0; private int y = 0; private Graphics buffer; private Image offscreen; public void init() { img = getImage(getDocumentBase(), "frog.jpg"); addMouseMotionListener(this); offscreen = createImage(this.getWidth(), this.getHeight()); buffer = offscreen.getGraphics(); } public void paint(Graphics g) { buffer.clearRect(0, 0, getWidth(), getHeight()); if (img != null) buffer.drawImage(img, x, y, this); g.drawImage(offscreen, 0, 0, this); } ...
DragFrog.java
DragFrogDouble.java
![Page 18: Java Applets - Montana Technological University · Applets •Applets –Java programs embedded in a web page • tag in the HTML •Appears in a box like other page](https://reader034.fdocuments.us/reader034/viewer/2022050303/5f6bbed8273191212d349425/html5/thumbnails/18.jpg)
Summary
• Java Applets
– Like a Java GUI application, but inside a web browser
– Must extend JApplet instead of JFrame • Implement some of the methods:
– init, start, stop, destroy, paint
– Multimedia applets • Showing images, playing audio
– Getting input using <param> tags
– Double buffering to prevent flicker
18