CSC 224 Lecture Notes


Week 6: Applets



Questions on the homework or the quiz? [1/18]
Java’s Original Appeal [2/18]
Which Applets to Use? [3/18]
Applets and their Methods [4/18]
A simple applet [5/18]
A slightly more interesting applet [6/18]
Anatomy of the HTML File [7/18]
DigitalClock2.java extends DigitalClock.java [8/18]
Security restrictions on applets [9/18]
How restrictions are enforced [10/18]
What about all that Swing GUI we learned? [11/18]
Animation [12/18]
An abstract animation applet framework class [13/18]
Using AnimationApplet.java [14/18]
Double Buffering [15/18]
BouncingBall.java [16/18]
JAR Files [17/18]
Exam Next Week [18/18]

Questions on the homework or the quiz? [1/18]



-THE DL FINAL WILL BE ON FRIDAY, MARCH 18, AT 7 PM IN ROOM 222 -IF THAT TIME IS NOT GOOD WITH YOU YOU CAN TAKE IT WITH A PROCTOR AT YOUR CONVENIENCE -How did the homework go?
-Any questions on the quiz?

Let's take a look at my solution for the homework, and then we'll do the quiz

Java’s Original Appeal [2/18]


Java's originally drew "buzz-word" status due to Applets... So what's an Applet?

Which Applets to Use? [3/18]



Applets and their Methods [4/18]


Applets define their onscreen appearance by overriding the paint method:
class OurApplet extends Applet {
    . . .
    public void paint(Graphics g) { . . . }
    . . .
}

The paint method is one of two display methods an applet can override:
paint
The basic display method. Many applets implement the paint method to draw the applet's representation within a browser page.
update
A method you can use along with paint to improve drawing performance.

Applets inherit their paint and update methods from the Applet class, which inherits them from the Abstract Window Toolkit (AWT) Component class.


A simple applet [5/18]


1: import java.applet.*; 2: import java.awt.*; 3: 4: public class Lifecycle extends Applet{ 5: public void init() { starts = stops = 0; } 6: public void start() { starts++; } 7: public void stop() { stops++; } 8: public void destroy() { System.out.println("I'm melting!"); } 9: public void paint(Graphics g) 10: { 11: g.drawString("Starts: "+starts, 10, 10); 12: g.drawString("Stops: "+stops, 100, 10); 13: } 14: private int starts, stops; 15: } Lifecycle.java

The HTML tags: <html><body> <applet height=100 width=500 code="Lifecycle.class"></applet> </body></html> We can also check things with the appletviewer tool, let's demonstrate...

A slightly more interesting applet [6/18]


1: import java.awt.*; 2: import java.util.Calendar; 3: 4: public class DigitalClock 5: extends java.applet.Applet implements Runnable { 6: 7: protected Thread clockThread = null; 8: protected Font font = new Font("Monospaced", Font.BOLD, 48); 9: protected Color color = Color.green; 10: 11: public void start() { 12: if (clockThread == null) { 13: clockThread = new Thread(this); 14: clockThread.start(); 15: } 16: } 17: 18: public void stop() { 19: clockThread = null; 20: } 21: 22: public void run() { 23: while (Thread.currentThread() == clockThread) { 24: repaint(); 25: try { 26: Thread.currentThread().sleep(1000); 27: } catch (InterruptedException e) {} 28: } 29: } 30: 31: public void paint(Graphics g) { 32: Calendar calendar = Calendar.getInstance(); 33: int hour = calendar.get(Calendar.HOUR_OF_DAY); 34: int minute = calendar.get(Calendar.MINUTE); 35: int second = calendar.get(Calendar.SECOND); 36: g.setFont(font); 37: g.setColor(color); 38: g.drawString(hour + ":" + minute / 10 + minute % 10 + 39: ":" + second / 10 + second % 10, 10, 60); 40: } 41: } DigitalClock.java
The HTML tag: <APPLET code=DigitalClock.class width=300 height=100></APPLET>

Anatomy of the HTML File [7/18]


<html> <head><title>MyApplet</title></head> <body> <applet CODE="MyApplet.class" WIDTH=200 HEIGHT=100 ALIGN=MIDDLE CODEBASE="MyAppletDir/"> <param name="myVar1" value="myVal1"> <param name="myVar2" value="myVal2"> </applet> </body> </html>

DigitalClock2.java extends DigitalClock.java [8/18]


1: import java.awt.Color; 2: 3: 4: public class DigitalClock2 extends DigitalClock { 5: 6: public void init() { 7: String param = getParameter("color"); 8: 9: if ("red".equals(param)) { 10: color = Color.red; 11: } else if ("blue".equals(param)) { 12: color = Color.blue; 13: } else if ("yelow".equals(param)) { 14: color = Color.yellow; 15: } else if ("orange".equals(param)) { 16: color = Color.orange; 17: } else { 18: color = Color.green; 19: } 20: } 21: } DigitalClock2.java
HTML tag: <APPLET CODE=DigitalClock2.class WIDTH=250 HEIGHT=80> <PARAM NAME=color VALUE=red> </APPLET>

Security restrictions on applets [9/18]



How restrictions are enforced [10/18]



What about all that Swing GUI we learned? [11/18]


1: import java.awt.*; 2: import java.awt.event.*; 3: import java.applet.Applet; 4: 5: public class TestAWT extends Applet { 6: public TestAWT(){ 7: setLayout(new BorderLayout()); 8: ta = new TextArea(); 9: add(ta, BorderLayout.CENTER); 10: Panel p = new Panel(); 11: p.setLayout(new FlowLayout()); 12: Button b = new Button("Press"); 13: Choice c = new Choice(); 14: c.addItem("Monday"); 15: c.addItem("Wednesday"); 16: c.addItem("DistanceLearning"); 17: p.add(b); 18: p.add(c); 19: add(p, BorderLayout.SOUTH); 20: b.addActionListener(new MyButtonListener()); 21: c.addItemListener(new MyChoiceListener()); 22: } 23: private class MyButtonListener implements ActionListener{ 24: public void actionPerformed(ActionEvent event){ 25: ta.append("Button Pressed\n"); 26: } 27: } 28: private class MyChoiceListener implements ItemListener{ 29: public void itemStateChanged(ItemEvent event){ 30: ta.append(event.getItem().toString() + "\n"); 31: } 32: } 33: private TextArea ta; 34: } TestAWT.java

Animation [12/18]


Applets allow for some fun animation, let's look at the basic framework... javaboutique.com is a place to post neat applets and often to share the source...

An abstract animation applet framework class [13/18]


1: /***************************************** 2: * An abstract class that handles the specifics 3: * of animation. All the user has to do is 4: * implement subclass specific behavior. 5: *****************************************/ 6: 7: public abstract class AnimationApplet extends java.applet.Applet implements Runnable{ 8: // the thread that does the redrawing 9: private Thread animationThread = null; 10: // how often the thread redraws itself 11: private int delay; 12: 13: public void start(){ 14: if (animationThread == null){ 15: animationThread = new Thread(this); 16: animationThread.start(); 17: } 18: } 19: 20: public void stop(){ 21: animationThread = null; 22: } 23: 24: public void run(){ 25: while (Thread.currentThread() == animationThread){ 26: repaint(); 27: try{ 28: Thread.currentThread().sleep(delay); 29: } 30: catch(InterruptedException e){/* don't do anything*/} 31: } 32: } 33: 34: // Java bean pattern to enforce encapsulation 35: public void setDelay(int delay){ 36: this.delay = delay; 37: } 38: // Java bean pattern to enforce encapsulation 39: public int getDelay(){ 40: return delay; 41: } 42: } AnimationApplet.java

Using AnimationApplet.java [14/18]


1: import java.awt.*; 2: 3: public class ScrollingBanner extends AnimationApplet{ 4: 5: protected String text; 6: protected Font font = new java.awt.Font("Sans-serif", Font.BOLD, 24); 7: protected int x, y; 8: protected int offset = 1; 9: protected Dimension d; 10: 11: public void init() { 12: // get parameters "delay" and "text" 13: String att = getParameter("delay"); 14: 15: // set the delay of the animation 16: if (att != null){ 17: setDelay(Integer.parseInt(att)); 18: } 19: else{ 20: setDelay(100); // default value 21: } 22: 23: // set the text to show 24: att = getParameter("text"); 25: if (att != null) { 26: text = att; 27: } 28: else { 29: text = "Scrolling banner."; 30: } 31: 32: // set initial position of the text 33: d = getSize(); 34: x = d.width; 35: y = font.getSize(); 36: } 37: 38: 39: public void paint(Graphics g) { 40: // get the font metrics to determine the length of the text 41: g.setFont(font); 42: FontMetrics fm = g.getFontMetrics(); 43: int length = fm.stringWidth(text); 44: 45: // adjust the position of text from the previous frame 46: x -= offset; 47: 48: // if the text is completely off to the left end 49: // move the position back to the right end 50: 51: if (x < -length) 52: x = d.width; 53: 54: // set the pen color and draw the background 55: g.setColor(Color.black); 56: g.fillRect(0,0,d.width,d.height); 57: 58: // set the pen color, then draw the text 59: g.setColor(Color.green); 60: g.drawString(text, x, y); 61: } 62: } ScrollingBanner.java

Double Buffering [15/18]


1: import java.awt.*; 2: 3: public class ScrollingBanner2 extends ScrollingBanner { 4: 5: protected Image image; // The off-screen image 6: protected Graphics offscreen; // The off-screen graphics 7: 8: public void update(Graphics g) { 9: // create the offscreen image if it is the first time 10: 11: if (image == null) { 12: image = createImage(d.width, d.height); 13: offscreen = image.getGraphics(); 14: } 15: 16: // draw the current frame into the off-screen image 17: // using the paint method of the superclass 18: super.paint(offscreen); 19: 20: // copy the off-screen image to the screen 21: g.drawImage(image, 0, 0, this); 22: } 23: 24: public void paint(Graphics g) { 25: update(g); 26: } 27: } ScrollingBanner2.java

BouncingBall.java [16/18]


1: import java.awt.*; 2: 3: 4: public class BouncingBall extends AnimationApplet{ 5: 6: protected Color color = Color.magenta; 7: // radius of the ball 8: protected int radius = 20; 9: protected int x, y; 10: // how much the location changes at every redraw 11: protected int dx = -2, dy = -4; 12: // off screen image 13: protected Image image; 14: // off screen graphics 15: protected Graphics offscreen; 16: // dimensions of the applet region 17: protected Dimension d; 18: 19: public void init() { 20: String att = getParameter("delay"); 21: if (att != null) { 22: setDelay(Integer.parseInt(att)); 23: } 24: else{ 25: setDelay(100); // default 26: } 27: 28: d = getSize(); 29: 30: // set the starting point of the ball 31: x = d.width * 2 / 3; 32: y = d.height - radius; 33: } 34: 35: 36: public void update(Graphics g) { 37: // create the off-screen image buffer 38: // if it is invoked the first time 39: if (image == null) { 40: image = createImage(d.width, d.height); 41: offscreen = image.getGraphics(); 42: } 43: 44: // draw the background 45: offscreen.setColor(Color.cyan); 46: offscreen.fillRect(0,0,d.width,d.height); 47: 48: // adjust the position of the ball 49: // reverse the direction if it touches 50: // any of the four sides 51: if (x < radius || x > d.width - radius) { 52: dx = -dx; 53: } 54: if (y < radius || y > d.height - radius) { 55: dy = -dy; 56: } 57: x += dx; 58: y += dy; 59: 60: // draw the ball 61: offscreen.setColor(color); 62: offscreen.fillOval(x - radius, y - radius, 63: radius * 2, radius * 2); 64: 65: // copy the off-screen image to the screen 66: g.drawImage(image, 0, 0, this); 67: } 68: 69: public void paint(Graphics g) { 70: update(g); 71: } 72: } BouncingBall.java

JAR Files [17/18]


OperationCommand
To create a JAR file jar cf jar-file input-file(s)
To view the contents of a JAR file jar tf jar-file
To extract the contents of a JAR file jar xf jar-file
To extract specific files from a JAR file jar xf jar-file archived-file(s)
To run an application packaged as a JAR file
(version 1.1)
jre -cp app.jar MainClass
To run an application packaged as a JAR file
(version 1.2 -- requires Main-Class
manifest header)
java -jar app.jar
To invoke an applet packaged as a JAR file
<applet code=AppletClassName.class
        archive="JarFileName.jar"
        width=width height=height>
</applet>


Exam Next Week [18/18]


-Exam 6 Next Week
-Homework 6 Due Before Class