简答题
10、本题中,主窗口中有两个下拉菜单,一个控制绘制图形的颜色,另一个控制绘制的图形,在画板中单击鼠标,则以单击的位置为左上角、以选定的颜色绘制选定的图形。
import java.awt.*;
import java.awt.event.*;
class java3 extends Frame {
String[]figureNames={"圆形","椭圆形","正
方形","长方形");
String[]colorNames={"红色","绿色","蓝
色","黄色"};
Color[]colorValues={Color.red,Color.green,
Color.blue,Color.yellow);
Choice chFigure=new Choice();
Choice chColor=new Choice();
int curX,curY;
java3(){
super("java3");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(O);
}
});
Panel P=new Panel(new GridLayout(1,O));
for(int i=0;i ehFigure.addhem(figureNames[i]);
}
for(int i=0;i chColor.addhem(eolorNames[i]);
}
P.add(chColor);
P.add(chFigure);
add(p,BorderLayout.NORTH);
addMouseListener(this);
setSize(300,300);
show();
}
public void update(Graphics g){
g.getColor(colorValues[chColor,getSelectedIndex
()]);
switch(chFigure.getSelectedlndex()){
case 0;
g.fillOval(curX,curY,30,30);
break;
case l:
g.fillOval(curX,curY,30,50);
break;
case 2:
g.fillRect(curX,curY,30,30);
break;
case 3:
g.fillRect(curX,curY,30,50);
break;
}
}
class MouseEventListener implements MouseAda-
pter{
public void mousePressed(MouseEvent evt){
curX=evt.getX();
curY=evt.getY();
repaint();
}
}
static public void main(String[]args){
new java3();
}
)
一对一 咨询课程
点我开始