This site requires JavaScript, please enable it in your browser!
Greenfoot back
suushi
suushi wrote ...

2011/12/11

project help

suushi suushi

2011/12/11

#
Hello, I'm working on a project and I'm stuck. I need to create a canvas and in the canvas I need to create random colored and random sized squares by pressing a button. I also need to do the same with circles and lines but cant figure out where to start. Any help will be greatly appreciated thanks.
giordanno92 giordanno92

2011/12/12

#
Greenfoot has some methods from GreenfootImage like drawRect() and setColor(), you just have to use them, define some variables for max size and use the Random class or Math.random() and multiply its value for the max. You could only use the World class or the World class and another Actor class. check this tutorial where Michael Kölling shows how to insert an object in the world in random places using the mouse clicking. to make variable sizes squares, make this:
int max = 50 // can be any number
int rand;
// img is an already declared GreenfootImage
// clears previous images
img.clear();
rand = Math.random() * max + 1;
img.drawRect(0, 0, rand, rand);
This logic is the same for circles (img.drawOval()) and for lines (img.drawLine()) I'm not sure (cause I never used), but I think you can use ints to represent colors, so you can use: img.setColor(Math.random() * 24 + 1); Idk, maybe it is: img.setColor(new Color(Math.random() * 24 + 1); You can check... Hope this helps.
suushi suushi

2011/12/12

#
thank you! how can i make it so that it keeps making them continuously?
suushi suushi

2011/12/12

#
and toggle it on/off, sry. thx
giordanno92 giordanno92

2011/12/14

#
You mean making by itself? Cause if so, then you should just put on the act method. It will act like an infinite loop, I've made something like that with an image in Eclipse using thread. It's the same with the act method. Put a Greenfoot.isKeyDown("k") method ("k" can be any key), or use the mouse clicking with an if and a boolean like:
boolean isLooping = true;

public void act() {
    if (Greenfoot.mouseClicked(null))
        isLooping = !isLooping; // this will change isLooping to true or false everytime the mouse is clicked
    if (isLooping) {
        // insert code to put the random images here
        randomImages();
    }
}

public void randomImages() {
    // code for random images
}
I advise you to put the code of random images on a new method (like i did on the sample), it looks cleaner and better to read. Besides you can create one for each shape you want, like randomSquares(), randomCircles, randomLines(). Hope this helps, if not, then bring your doubt :-)
suushi suushi

2011/12/14

#
if ( Greenfoot.isKeyDown("2")){ if (Greenfoot.isKeyDown("2")) isLooping = !isLooping; if (isLooping) { java.awt.Color a; int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); int x1 = Greenfoot.getRandomNumber(this.getWidth()); int y1 = Greenfoot.getRandomNumber(this.getHeight()); a = new java.awt.Color(r,g,b); background.setColorAt(x, y, a); background.setColor(a); //background.clear(); background.drawLine(x, y, x1, y1); }} this is what i have so far but it doesnt seem to be working
You need to login to post a reply.