0% found this document useful (0 votes)
49 views7 pages

CS 126 Lecture S2: Introduction To Java Applets: Outline

This document summarizes a lecture on Java applets. It introduces applets and their life cycle, demonstrates a simple "Hello World" applet, and discusses a slightly more complex applet that responds to mouse clicks. It also outlines some truths about Java, including that it is secure but has bugs, productive but versions cause issues, and was hyped but is still important for its large community and libraries.

Uploaded by

wphile2270
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

CS 126 Lecture S2: Introduction To Java Applets: Outline

This document summarizes a lecture on Java applets. It introduces applets and their life cycle, demonstrates a simple "Hello World" applet, and discusses a slightly more complex applet that responds to mouse clicks. It also outlines some truths about Java, including that it is secure but has bugs, productive but versions cause issues, and was hyped but is still important for its large community and libraries.

Uploaded by

wphile2270
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS 126 Lecture S2:

Introduction to Java Applets

Outline
• Introductions
• Your first applet and more tools of trade
• Life cycle of an applet
• Simple drawing and events
• Conclusions

CS126 21-1 Randy Wang


Applets: Beyond Animated Clowns
• What can you do when you can slurp code over the net?
• Extensibility
- Bill Joy: “No more protocols; just code!”
- No need for hard wired network protocols
- No need for hard wired information content protocols
• A brave new world
- New way of structuring applications (local or distributed)
- New way of structuring operating systems (local or
distributed)
• Today is only an introduction to the bare basics
- Encourage interested people to explore on their own
- It’s fun and there’s nothing hard
CS126 21-2 Randy Wang

Learning About Applets


• Again, take advantage of on-line resources
- Go through tutorials
- Always look for existing code to steal
- Read online documentations to learn about library
functionalities
• A warning
- The GUI stuff is most vulnerable to version confusions
- “AWT”, “JFC”, “Swing”, ......?!
- The GUI stuff is also most buggy and least compatible
• (Don’t get scared: you need to know very little to survive
this class, so the advice is mostly for people who want
more.)

CS126 21-3 Randy Wang


Outline
• Introductions
• Your first applet and more tools of trade
• Life cycle of an applet, “funny” part
- You have to write a whole bunch of methods you don’t call
- You call a whole bunch of methods that you didn’t write
• Simple drawing and events
• Conclusions

CS126 21-4 Randy Wang

Your First Java Applet


import java.applet.Applet;
import java.awt.Graphics;
Hello.java
public class Hello extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 125, 95);
}
}
<HTML><BODY>
<APPLET CODE=Hello.class WIDTH=300 HEIGHT=200></APPLET>
hello.html </BODY></HTML>
• To try it
- Compile: javac Hello.java
- Test: appletviewer hello.html
- Or: put all these files in a publicly accessible directory (such as ~/
public_html and view using netscape)
• What happens
- .html and .class files are slurped over the net
- The browser has a virtual machine (interpreter) in it
- It checks for security violations and runs it if ok.
CS126 21-5 Randy Wang
Life Cycle of an Applet
import java.applet.Applet; public void destroy() {
import java.awt.Graphics; addItem("preparing for unloading...");
}
public class Simple extends Applet {
StringBuffer buffer; void addItem(String newWord) {
System.out.println(newWord);
public void init() { buffer.append(newWord);
buffer = new StringBuffer(); repaint();
addItem("initializing... "); }
}
public void paint(Graphics g) {
public void start() { g.drawString(buffer.toString(), 5, 15);
addItem("starting... "); }
} }

public void stop() {


addItem("stopping... ");
}

• init(): browser calls it when applet first loaded


• start(): start execution (eg. after becoming visible)
• stop(): stop execution (eg. after switching to different page)
• destroy(): clean up after final exit
• paint(): browser tells it it’s time to redraw
CS126 21-6 Randy Wang

A Slightly Larger Example


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
A helper class for the dot
class Spot {
public int size;
public int x, y;

public Spot(int size) {


this.size = size;
this.x = -1;
this.y = -1;
}
}

public class ClickMe extends Applet


implements MouseListener { Later
private Spot spot = null;
private static final int RADIUS = 7;

A constant that can’t be changed


CS126 21-7 Randy Wang
Example (cont.) -- Drawing
public void paint(Graphics g) {
// draw a black border and a white background
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width - 1,
getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1,
getSize().height - 1);

// draw the spot


g.setColor(Color.red);
if (spot != null) {
g.fillOval(spot.x - RADIUS,
spot.y - RADIUS,
RADIUS * 2, RADIUS * 2);
}
}

CS126 21-8 Randy Wang

Example (cont.) -- Event Handling


public class ClickMe extends Applet
implements MouseListener {
... MouseListner is an interface.
ClickMe promises to implement
“this” is the reference to this everything specified by the interface.
instance of the class. (Kindof like multiple inheritance in C++)
public void init() {
addMouseListener(this); As long as ClickMe promises to
implement the interface, it can now
} accept mouse events.
public void mousePressed(MouseEvent event) {
if (spot == null) { The browser calls the applet
spot = new Spot(RADIUS);through this method when
} the mouse is pressed.
spot.x = event.getX(); Figure out where the mouse is and
spot.y = event.getY(); trigger a paint() through repaint().
repaint(); Don’t need these, but a promise is
} a promise.
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
CS126 21-9 Randy Wang
Outline
• Introductions
• Your first applet and more tools of trade
• Life cycle of an applet
• Simple drawing and events
• Conclusions

CS126 21-10 Randy Wang

The “Truth”
• “KISS”
- Large number of complicated features of C++ gone
- The language is incredibly small
- Flip side: huge number of libraries and you can’t be a serious
Java programmer without knowing a lot about them
• “Modern”
- Garbage collection, strongly typed, exceptions, support for
multi-threading and networking
- Flip side: ideas have been around in the research community
for ages: Modula-3, Smalltalk, Lisp, C++, Object C
• “Secure”
- A nice three-tier protection system: verifier, class loader, and
security manager.
- Can reason about it formally
- Flip side: bugs
CS126 21-11 Randy Wang
The “Truth” (cont.)
• “Productive”
- Much less debugging headaches: no pointer probs, exceptions
- Stealing has never been easier: the net, portability, reusability
- Excellent documentation
- Large and growing body of libraries to help: utilities, media,
GUI, networking, threads, databases, cryptogaphy...
- Flip side: versions, large libraries
• “Slow”
- Interpreted, too many tiny objects and methods
- Flip side: just-in-time compiling can make things almost as
fast as native code
• “Hype”
- Important for momentum which translates into community
expertise and support, applications, tools, and libraries
- Flip side: hasty dicision-making to feed the frenzy
• Only game in town?
- Unprecedented roles for scripting languages on the net
CS126 21-12 Randy Wang

You might also like