本题是一个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的一个,按住鼠标左键或右键在面板中拖动,就能两出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class java3extends Applet implements ActionListener
{int x=-1,y=-1,rubberNote=0,clearNote=0;
Color C=new Color(255,0,O);
int con=3;
Button b_red,b_blue,b_green,b_clear,b_quit;
public void init()
{
addMouseMotionListener(this);
b_red=new Button("画红色图形");
b_blue=new Button("画蓝色图形");
b_green=new Button("画绿色图形");
b_quit=new Button("橡皮");
b_clear=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit);
add(b_clear);
b_red.addActionListener(this);
b_green.addActionListener(this);
b_blue.addActionListener(this);
b_quit.addActionListener(this);
b_dear.addActionListener(this);
}
public void paint()
(if(x!=-l&&y!=-l&rubberNote= =
0R&;clearNote= =0)
{g.setColor(c);
g.filloval(X,Y,con,con);
}
else if(rubberNote= =1&;&;clearNote= =O)
{g.clearRect(x,Y,10,10);
}
else if(clearNote= =1&;&;rubberNote= =O)
{g.clearRect(0,0,getSize().width,getSize().
height);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();repaint();
}
public void mouseMoved(MouseEvent e){)
public void update(Graphics g)
{paint(g);
}
public void actionPerformed(Event e)
{if(e.getSource()= =b-red)
{rubberNote=0;clearNote=0;c=new Color
(255,0,0);
}
else if(e.getSource()= =b_green)
{rubberNore=0;clearNote=0;C=new Color(0,
255,0);
}
else if(e.getSource()= =b_blue)
{rubberNote=0;clearNote=0;C=new Color(0,
0,255);
}
if(e.getSource()= =b—quit)
{rubberNote=1;clearNote=0;
}
if(e.getSource()= =b—clear)
{clearNote=1;rubberNote=0;repaint();
}
}
}
订单号:
遇到问题请联系在线客服
订单号:
遇到问题请联系在线客服