Beispiel 20
import java.awt.Graphics;
public class Lebenszyklus extends java.applet.Applet {
StringBuffer buffer = new StringBuffer();
public void init () {resize(500, 20);addItem("Initializing...");};
public void start (){addItem("Starting ...");};
public void stop(){addItem("Stop...");};
public void destroy(){addItem("preparing for unloading");};
public void addItem(String meldung) {
System.out.println(meldung);
buffer.append(meldung);
repaint();
};
public void paint(Graphics g) {
g.drawRect(0,0,getSize().width -1, getSize().height -1);
// size deprecated in 1.2
g.drawString(buffer.toString(), 5, 15);
};
};
Beispiel 21 [Standalone Applet]
Erweitert (und vereinfacht) aus [Fla97a]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class StandaloneScribble extends Applet {
public static void main (String[] args) {
Frame f = new Frame();
Applet a = new StandaloneScribble();
f.add(a, "Center");
a.init();
f.setSize(400, 400);
f.show(); // show l"a"st einen Frame erscheine
f.setBackground(bgcolor);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);};
});
}; // end of main
public void init() { // auch als applet verwenbar
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
lastx = e.getX(); //
lasty = e.getY();
};
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics ();
int x = e.getX();
int y = e.getY();
g.setColor(StandaloneScribble.this.drawcolor);
// wiederum: this alleine geht nicht!
g.drawLine(lastx, lasty, x, y);
lastx = x; lasty = y;
};
});
Button b = new Button("Blau");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawcolor = Color.blue;
};
});
this.add(b);
};
protected int lastx, lasty;
protected static Color bgcolor = Color.white;
protected Color drawcolor = bgcolor;
};