package chapter2.e2;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AAIfillOval extends Applet implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
CheckboxGroup cg;
Checkbox ch1, ch2, ch3;
Choice choice;
int i = 1;
int k = 90;
public void init() {
cg = new CheckboxGroup();
ch1 = new Checkbox("red", true, cg);
ch2 = new Checkbox("yellow", false, cg);
ch3 = new Checkbox("blue", false, cg);
add(ch1);
ch1.addItemListener(this);
add(ch2);
ch2.addItemListener(this);
add(ch3);
ch3.addItemListener(this);
choice = new Choice();
choice.addItem("90");
choice.addItem("70");
choice.addItem("50");
choice.addItem("30");
choice.addItem("10");
add(choice);
choice.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
if (ch1.getState()) {
i = 1;
} else if (ch2.getState()) {
i = 2;
} else {
i = 3;
}
if(e.getSource() == choice){
k = Integer.parseInt(choice.getSelectedItem());
}
}
public void paint(Graphics g){
if(i==1){
g.setColor(Color.red);
g.fillOval(10, 10, k, k);
}
if(i==2){
g.setColor(Color.yellow);
g.fillOval(10, 10, k, k);
}
if(i==3){
g.setColor(Color.blue);
g.fillOval(10, 10, k, k);
}
try {
Thread.sleep(300);
repaint();
} catch (Exception e) {
// TODO: handle exception
}
}
}
评论0
最新资源