Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.Random;
- import acm.graphics.GLine;
- import acm.graphics.GOval;
- import acm.graphics.GRect;
- import acm.program.GraphicsProgram;
- import acm.util.RandomGenerator;
- public class RandomShapes extends GraphicsProgram{
- private int width = this.getWidth();
- private int height = this.getHeight();
- private RandomGenerator rand = new RandomGenerator();
- public void run() {
- getRandomShape(1);
- getRandomShape(2);
- getRandomShape(3);
- }
- public void getRandomShape(int i) {
- int x = rand.nextInt(1, this.width);
- int y = rand.nextInt(1, this.height);
- int w = rand.nextInt(1, this.width - x);
- int h = rand.nextInt(1, this.height - y);
- Color c = getRandomColor();
- switch(i) {
- case 1:
- GRect rect = new GRect(x, y, w, h);
- rect.setFilled(true);
- rect.setFillColor(c);
- add(rect);
- break;
- case 2:
- GOval oval = new GOval(x, y, w, h);
- oval.setFilled(true);
- oval.setFillColor(c);
- add(oval);
- break;
- case 3:
- GLine line = new GLine(x, y, x + w, y + h);
- line.setColor(c);
- add(line);
- break;
- }
- }
- public Color getRandomColor() {
- Random rand = new Random();
- float r = rand.nextFloat();
- float g = rand.nextFloat();
- float b = rand.nextFloat();
- return new Color(r, g, b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement