This program is about as simple as an applet can be and still
respond to user commands. It draws a circle in a random color;
each time the button is clicked, it redraws the circle in a new
random color. The button overlays the circle because no attention
has been paid to layout.
Note: This is a picture of the applet, not the applet itself.
The applet itself is not included because Java 1.1 is not supported
on Netscape browsers.
Here's the HTML file:
<html>
<head>
<title>Simple Applet</title>
</head>
<body bgcolor="#FFFFFF">
<applet code="MyApplet.class" width=200 height=200></applet>
</body>
</html>
|
Here's the Java file:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends Applet
{
Button newColor = new Button ("New Color");
public void init ()
{
add (newColor);
newColor.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event)
{
repaint ();
}
});
}
public void paint (Graphics g)
{
int red = (int) (Math.random () * 256);
int green = (int) (Math.random () * 256);
int blue = (int) (Math.random () * 256);
g.setColor (new Color (red, green, blue));
g.fillOval (0, 0, 200, 200);
}
}
|