HelloWeb4
Rakendi HelloWeb3 seletab lahti Andres Tsengov. Siin on toodud ainult
näide ise:
import java.applet.*;
import java.awt.*;
public class HelloWeb4 extends Applet implements Runnable {
int messageX = 125, messageY = 95;
String theMessage;
Button theButton;
Color[] someColors = {
Color.black, Color.red, Color.green, Color.blue, Color.yellow };
int colorIndex = 0;
Thread blinkThread;
boolean blinkState;
public void init() {
theMessage = getParameter("message");
theButton = new Button("Change Color");
add(theButton);
}
public void paint(Graphics gc) {
gc.setColor(blinkState ? Color.white : currentColor());
gc.drawString(theMessage, messageX, messageY);
}
public boolean action(Event e, Object arg) {
if (e.target == theButton) {
changeColor();
return true;
}
return false;
}
public boolean mouseDrag(Event evt, int x, int y) {
messageX = x;
messageY = y;
repaint();
return true;
}
synchronized private Color currentColor() {
return someColors[colorIndex];
}
synchronized private void changeColor() {
if (++colorIndex == someColors.length)
colorIndex = 0;
theButton.setForeground(currentColor());
repaint();
}
public void run() {
while ( true ) {
blinkState = !blinkState;
repaint();
try {
Thread.sleep(500);
}
catch(InterruptedException e) {
}
}
}
public void start() {
if ( blinkThread == null ) {
blinkThread = new Thread(this);
blinkThread.start();
}
}
public void stop() {
if ( blinkThread != null ) {
blinkThread.stop();
blinkThread = null;
}
}
}
Siin see HelloWeb4 on.