50,528
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class huantu extends Applet implements ActionListener{
Graphics g;
Panel panel=new Panel();
Button button1=new Button("画直线");
Button button2=new Button("画矩形");
Button button3=new Button("画多边形");
Button button4=new Button("画圆");
Button button5=new Button("画扇形");
public void init(){
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
g = getGraphics();
if(e.getSource()==button1)
paint(g,1);
else if(e.getSource()==button2)
paint(g,2);
else if(e.getSource()==button3)
paint(g,3);
else if(e.getSource()==button4)
paint(g,4);
else if(e.getSource()==button5)
paint(g,5);
}
public void paint(Graphics g,int i){
if(i==1){
g.setColor(Color.white); //覆盖矩形
g.drawRect(50, 50, 50, 100);
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; // 覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); // 覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.red); //画直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
}
else if(i==2){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); // 覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); //覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.green); //画矩形
g.drawRect(50, 50, 50, 100);
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
}
else if(i==3){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.fillArc(60, 80, 80, 80, 30, 70); // 覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.blue); //画多边形
int x1[]={20,60,100,130,80};
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
}
else if(i==4){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.fillArc(60, 80, 80, 80, 30, 70); //覆盖扇形
g.fillArc(200, 80, 80, 80, 60, 140);
g.setColor(Color.pink); //画圆
g.drawOval(60, 60, 60, 60);
g.fillOval(180, 60, 60, 60);
}
else if(i==5){
g.setColor(Color.white); //覆盖直线
g.drawLine(30, 60, 80, 60);
g.drawLine(100, 60, 150, 100);
g.drawLine(160, 100, 270, 60);
g.drawRect(50, 50, 50, 100); //覆盖矩形
g.fillRect(130, 50, 50, 100);
g.fillRect(200, 50, 100, 50);
int x1[]={20,60,100,130,80}; //覆盖多边形
int y1[]={60,80,150,40,60};
int x2[]={220,260,300,330,280};
int y2[]={60,80,150,40,60};
g.drawPolygon(x1, y1, 5);
g.fillPolygon(x2, y2, 5);
g.drawOval(60, 60, 60, 60); //覆盖圆
g.fillOval(180, 60, 60, 60);
g.setColor(Color.orange); //画扇形
g.fillArc(60, 80, 80, 80, 30, 70);
g.fillArc(200, 80, 80, 80, 60, 140);
}
}
}