home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java by Example
/
jbecd.bin
/
JBE-CD
/
NTUsers
/
JBECODE.ZIP
/
JavaByExample
/
chap21
/
CanvasApplet2.java
< prev
next >
Wrap
Text File
|
1996-03-08
|
1KB
|
70 lines
import java.awt.*;
import java.applet.*;
public class CanvasApplet2 extends Applet
{
MyCanvas canvas;
public void init()
{
GridLayout layout = new GridLayout(2, 1, 10, 10);
setLayout(layout);
canvas = new MyCanvas();
Panel panel = new Panel();
Button button1 = new Button("Black");
Button button2 = new Button("Green");
Button button3 = new Button("Red");
add(canvas);
add(panel);
panel.add(button1);
panel.add(button2);
panel.add(button3);
resize(200, 250);
}
public boolean action(Event evt, Object arg)
{
if (evt.target instanceof Button)
{
if (arg == "Black")
canvas.swapColor(Color.black);
else if (arg == "Green")
canvas.swapColor(Color.green);
else
canvas.swapColor(Color.red);
}
return true;
}
}
class MyCanvas extends Canvas
{
Color color;
public MyCanvas()
{
color = Color.black;
}
public void paint(Graphics g)
{
Rectangle r = bounds();
g.setColor(color);
g.fillRect(0, 0, r.width, r.height);
g.setColor(Color.white);
g.drawString("CANVAS", 72, 60);
}
public void swapColor(Color color)
{
this.color = color;
repaint();
}
}