码迷,mamicode.com
首页 > 其他好文 > 详细

随机绘制多边形图案

时间:2020-09-18 03:02:03      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:swing   textfield   draw   exit   visible   图片   dac   result   pac   

package result;
//随机绘制多边形图案
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class F8 extends JFrame {
JButton j1 = new JButton("确认");
JTextField j2 = new JTextField(5);

public F8() {
super("多边形绘制");
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(new JLabel("请输入边数"));
add(j2);
add(j1);
MyCanvas m = new MyCanvas();
add(m);
j1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = Integer.parseInt(j2.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);
}
m.setPolygon(x, y, n);
m.repaint();
}
});
validate();
}

public static void main(String[] args) {
new F8();
}
}

class MyCanvas extends Canvas {
int N = 10;
int x[] = new int[N];
int y[] = new int[N];
public MyCanvas() {
setSize(400, 300);
setBackground(Color.cyan);
}

public void setPolygon(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);
}

}

技术图片

 

 

随机绘制多边形图案

标签:swing   textfield   draw   exit   visible   图片   dac   result   pac   

原文地址:https://www.cnblogs.com/nanfengnan/p/13679123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!