Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.Random;
  3.  
  4. import acm.graphics.GLine;
  5. import acm.graphics.GOval;
  6. import acm.graphics.GRect;
  7. import acm.program.GraphicsProgram;
  8. import acm.util.RandomGenerator;
  9.  
  10. public class RandomShapes extends GraphicsProgram{
  11.  
  12. private int width = this.getWidth();
  13. private int height = this.getHeight();
  14. private RandomGenerator rand = new RandomGenerator();
  15.  
  16. public void run() {
  17. getRandomShape(1);
  18. getRandomShape(2);
  19. getRandomShape(3);
  20. }
  21.  
  22. public void getRandomShape(int i) {
  23. int x = rand.nextInt(1, this.width);
  24. int y = rand.nextInt(1, this.height);
  25. int w = rand.nextInt(1, this.width - x);
  26. int h = rand.nextInt(1, this.height - y);
  27. Color c = getRandomColor();
  28. switch(i) {
  29. case 1:
  30. GRect rect = new GRect(x, y, w, h);
  31. rect.setFilled(true);
  32. rect.setFillColor(c);
  33. add(rect);
  34. break;
  35. case 2:
  36. GOval oval = new GOval(x, y, w, h);
  37. oval.setFilled(true);
  38. oval.setFillColor(c);
  39. add(oval);
  40. break;
  41. case 3:
  42. GLine line = new GLine(x, y, x + w, y + h);
  43. line.setColor(c);
  44. add(line);
  45. break;
  46. }
  47. }
  48.  
  49. public Color getRandomColor() {
  50. Random rand = new Random();
  51. float r = rand.nextFloat();
  52. float g = rand.nextFloat();
  53. float b = rand.nextFloat();
  54. return new Color(r, g, b);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement