java中如何用画布绘制矩形图形?
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Ovalcanvas extends Canvas
{
int N=10;
int x[]=new int[N];
int y[]=new int[N];
Ovalcanvas()
{
setSize(300,200);
setBackground(Color.cyan);
}
public void setOval(int[] x,int[] y,int N)
{
this.N=N;
for(int i=0;i<N;i++)
{
this.x[i]=x[i];
this.y[i]=y[i];
}
}
public void paint(Graphics g)
{
g.drawPolygon(x,y,N);
}
}
public class Example6_7 extends JFrame implements ActionListener
{
Ovalcanvas canvas;
TextField in_N;
Button btn;
Example6_7()
{
super("画布上绘制多边形");
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
in_N=new TextField(6);
setLayout(new FlowLayout());
add(new Label("请输入变数:"));
add(in_N);
btn=new Button("确定");
btn.addActionListener(this);
add(btn);
canvas=new Ovalcanvas();
add(canvas);
validate();
}
public void actionPerformed(ActionEvent e)
{
int N=Integer.parseInt(in_N.getText());
int x[]=new int[N];
int y[]=new int[N];
for(int i=0;i<N;i++)
{
x[i]=(int)(Math.random()*200);
y[i]=(int)(Math.random()*200);
}
canvas.setOval(x,y,N);
canvas.repaint();
}
public static void main(String[] args)
{
new Example6_7();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询