1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| //The Canvas Applet that generates random colour circles
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
public class CanvasApplet extends Applet implements ActionListener
{
MyCanvas c;
public void init()
{
Button b = new Button("Randomize!");
b.addActionListener(this);
c = new MyCanvas();
this.setLayout(new BorderLayout());
this.add("North", b);
this.add("Center",this.c);
}
public void actionPerformed(ActionEvent e)
{
c.drawCircles();
}
}
class MyCanvas extends Canvas
{
Color tempColor;
private Image theImage;
public void init()
{
this.theImage = this.getImage( this.getDocumentBase(), "portable.JPG" );
}
public void drawCircles()
{
this.repaint();
}
public void paint(Graphics g)
{
g.drawImage( this.theImage, 10, 10, this);
for(int i=0; i<200; i++)
{
tempColor = new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
g.setColor(tempColor);
g.drawOval((int)(Math.random()*200), (int)(Math.random()*200), 20, 20);
}
}
} |
Partager