本题中,主窗口中有两个下拉菜单,一个控制绘制图形的颜色,另一个控制绘制的图形,在画板中单击鼠标,则以单击的位置为左上角、以选定的颜色绘制选定的图形。
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(0);
);
Panel p=new Panel(new GridLayout(1,0));
for(int i=0;i<figureNames.length;i++)
chFigure.addItem(figureNames[i]);
for(int i=0;i<colorNames.length;i++)
chColor.addItem(colorNames[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.getSelectedIndex())
case 0;
g.fillOval(curX,curY,30,30);
break;
case 1:
g.fillOval(curX,curY,30,50);
break;
case 2:
g.fillReet(curX,curY,30,30);
break;
case 3:
g.fillRect(curX,curY,30,50);
break;
class MouseEventListener implements MouseAdapter
public void mousePressed(MouseEvent evt)
curX=evt.getX();
curY=evt.getY();
repaint();
static public void main(String[]args)
new java3();