jeudi 22 septembre 2016

Random object won't generate for java.util.Random

this is my first post. I am relatively new to java and so I am working on a basic graphics program which creates a circle bouncing off the edge of the screen.

I am currently facing issues with the java.util.Random class, as occasionally the values it generates don't display the circle on the screen. The other issue is that my circle won't bounce off the walls. Here is my code: import java.awt.*; import java.util.Random;

/**
* MyArtwork
* Inspired by a lab from Jeff Ondich
* @author Dave Musicant
* @version 3.0
*
*/

class Graph
{
    public static void main(String[] args)
    {
        Random rand= new Random(); //sets up random
        int windowWidth = 2560;//sets width of window
        int windowHeight = 1600;//sets height of window
        int circplacex = rand.nextInt(2510);//sets xloc of initial circle
        int circplacey = rand.nextInt(1650);// sets yloc of initial circle
        int circspeedx = 1;// sets speed of circle
        int circspeedy = 1;
        int change1 = 1;//sets color change amount
        int change2 = 1;
        int change3 = 1;
        int x = 5;
        Canvas canvas = new Canvas("Hi there");//sets up canvas.
        canvas.setSize(windowWidth,windowHeight);
        canvas.setVisible(true);
        int color1 = rand.nextInt(256);
        int color2 = rand.nextInt(256);//creates random colors
        int color3 = rand.nextInt(256);
        while (x == 5){//infinite loop
            if(color1 == 255){
                change1 = -1;
            }
            if(color2 == 255){//changes color if to high
                change2 = -1;
            }
            if(color3 == 255){
                change3 = -1;
            }
            if(color1 == 0){
                change1 = 1;
            }
            if(color2 == 0){//changes color if too low
                change2 = 1;
            }
            if(color3 == 0){
                change3 = 1;
            }
            color1 = color1 + change1;
            color2 = color2 + change2;
            color3 = color3 + change3;
            canvas.fillBackground(Color.black);//clears screen
            Color fun = new Color(color1,color2,color3);
            canvas.setInkColor(fun);
            canvas.fillOval(circplacex,circplacey,50,50);//makes circle
            circplacex = circplacex + circspeedx;//moves circle by speed
            circplacey = circplacey + circspeedy;
            if(circplacex == 2510){//should make it bounce
                circspeedx = circspeedx*-1;
            }
            if(circplacey == 1550){
                circspeedy = circspeedy*-1;
            }
            if(circplacex == 0){
                circspeedx = circspeedx*-1;
            }
            if(circplacey == 0){
                circspeedy = circspeedy*-1;
            }
            canvas.wait(10);
        }
        }
            }




Aucun commentaire:

Enregistrer un commentaire