/*********Circle类********/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Circle extends Applet{
int x,y;
int r;
int R,G,B;
Graphics g;
Random rd=new Random();
Circle(int r)
{
this();
this.r=r;
init();
}
public Circle(){
super();
}
public void init(){
this.resize(500,500);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
public void paintComponents(Graphics g){
x=rd.nextInt(this.getWidth()-50);
y=rd.nextInt(this.getHeight()-50);
R=rd.nextInt(255);
B=rd.nextInt(255);
G=rd.nextInt(255);
g.setColor(Color.white);
//g.fillRect(0,0,this.getWidth(),this.getHeight());
Color color=new Color(R,G,B);
g.setColor(color); //设置圆的颜色
//g.drawOval(y,x,r,r); //画空心圆
g.fillArc(x,y,r,r,0,360); //画实心圆
}
public void setR(int r) {
this.r = r;
}
}
/*******Cantainer类*********/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Cantainer extends JFrame implements ActionListener {
//定义各个变量,创建对象//
Container c = this.getContentPane();
BorderLayout bL = new BorderLayout();
JLabel label;
JTextField jtf1, jtf2;
JButton btn;
public static int num1, num2;
String s1, s2;
Circle3 applet;
public Cantainer() {
//创建各组件的对象//
jtf1 = new JTextField(2);
jtf2 = new JTextField(2);
btn = new JButton("确定");
label = new JLabel("此程序为自动画圆程序,请在左边方框中输入半径(若不输则在100像素范围内随机取半径),在右边方框中输入你要画的个数,然后点击确定。");
//设置方位式布局//
c.setLayout(bL);
//添加各组件并设置位置//
c.add(label, BorderLayout.NORTH);
c.add(jtf1, BorderLayout.WEST);
c.add(jtf2, BorderLayout.EAST);
c.add(btn, BorderLayout.SOUTH);
btn.addActionListener(this);
applet = new Circle3();
applet.init();
c.add(applet, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
s1 = jtf1.getText();
s2 = jtf2.getText();
num2 = Integer.parseInt(s2);
applet.paint(applet.getGraphics()); //调用Circle类的画矩形
if (s1.equals("")) {
Thread t = new Thread() {
public void run() {
int i = 0;
Random rd = new Random();
while (i < num2) {
i++;
num1 = rd.nextInt(80)+20; //半径在20-100像素取随机数
applet.setR(num1);
applet.paintComponents(applet.getGraphics());
try {
sleep(200);
} catch (Exception e) {
}
}
}
};
t.start();
} else {
Thread t = new Thread() {
public void run() {
int i = 0;
while (i < num2) {
i++;
applet.setR(num1);
applet.paintComponents(applet.getGraphics());
try {
sleep(200);
} catch (Exception e) {
}
}
}
};
t.start();
}
}
}
/*****Main函数*********/
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
Cantainer c = new Cantainer();
c.setSize(1000, 500);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true);
}
}
5 个解决方案
#1
没有性能问题吗
#2
没有问题啊,输入半径和个数后就可以画圆了
#3
看一下聚焦吧!! 一时半会说不清!!
#4
没有问题啊,在方框中输入半径和个数后,点击确定就可以画圆了,现在想要实现的是按回车也可以画圆
#5
这个问题不错
#1
没有性能问题吗
#2
没有问题啊,输入半径和个数后就可以画圆了
#3
看一下聚焦吧!! 一时半会说不清!!
#4
没有问题啊,在方框中输入半径和个数后,点击确定就可以画圆了,现在想要实现的是按回车也可以画圆
#5
这个问题不错