简体   繁体   English

随机圈小程序Java

[英]Random circles applet java

I am new to programming and trying to write a graphics program in java that displays ovals of different sizes and colors, however, I am not able to get the program to display the ovals in applet window. 我是编程和尝试使用Java编写图形程序的新手,它可以显示不同大小和颜色的椭圆,但是,我无法让该程序在applet窗口中显示椭圆。 Does anyone have any suggestions/input on where I went wrong here? 有人对我在这里哪里出错有任何建议/意见吗? Please see an example of my paint method below: 请在下面查看我的绘画方法的示例:

   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);
  }

I modified a recent school project (we were supposed to make a hot air balloon) so excuse the naming of some of the stuff: 我修改了一个最近的学校项目(我们应该制造一个热气球),因此请为其中的一些名称命名:

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

Its rather small at the moment so you may want to change around some of the variables... however it does what you asked. 目前它很小,因此您可能需要更改一些变量...但是它确实满足您的要求。

In terms of where you went wrong... it appears you have a loop that put values into two arrays... however I don't see a second array that goes through and draws all the ovals... In my code, I generate all the coordinates, colours, and print it out all at once. 关于出错的地方...似乎有一个将值放入两个数组的循环...但是我看不到第二个数组经过并绘制所有椭圆形...在我的代码中,我生成所有坐标,颜色,然后一次全部打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM