我是编程并尝试在java中编写显示不同大小和颜色的椭圆的图形程序的新手,但是,我无法让程序在applet窗口中显示椭圆。有没有人对我在哪里出错有任何建议/意见?请参阅下面我的绘画方法示例:
public void paint(Graphics g)
{
for(int i=0; i<n; i++)
{
x[i] = (int)(600* Math.random() +1);
y[i] = (int)(600* Math.random() +1);
}
int c= (int)(255*Math.random()); //random foreground color
int a= (int)(255*Math.random());
int t= (int)(255*Math.random());
Color f = new Color(c,a,t);//variables have been declared in init
g.setColor(f);
g.fillOval(rand(0, 600), rand(0, 600), r = rand(5, 100), r);
sleep(100);
cnt += 1;
if(cnt >= 500) clearScreen();
else update(g);
}
答案 0 :(得分:0)
我修改了一个最近的学校项目(我们本来应该制作一个热气球),请原谅一些东西的命名:
import java.awt.*;
import javax.swing.*;
public class Balloon extends JComponent {
public static void main(String args[]){
JFrame frame = new JFrame("balloons");
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Balloon balloon = new Balloon();
frame.setContentPane(balloon);
}
public void paint(Graphics g){
super.paint(g);
for(int i = 0; i<20; i++){
int c= (int)(255*random()); //random foreground color
int a= (int)(255*random());
int t= (int)(255*random());
g.setColor(new Color(c,a,t));
g.fillOval((int)(200*random()),(int)(200*random()),(int)(30*random()),(int)(30*random()));
}
}
public double random(){
return Math.random();
}
}
目前它相当小,所以你可能想要改变一些变量...但它会按照你的要求做。
就你出错的地方而言...看起来你有一个循环将值放入两个数组......但是我没有看到第二个数组通过并绘制所有的椭圆......在我的代码中,我生成所有坐标,颜色,并一次打印出来。