Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. package aquarium;
  2.  
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.awt.geom.*;
  8. import java.util.*;
  9.  
  10. public class aquariumFish
  11. {
  12. public static void main(String[] args)
  13. {
  14. Aquarium toTest = new Aquarium();
  15.  
  16. }
  17. }
  18.  
  19. class Aquarium extends Frame
  20. {
  21. Fish siddharth;
  22. private boolean isRunning;
  23.  
  24. Aquarium()
  25. {
  26. isRunning = false;
  27. siddharth = new redFish(100,100);
  28. Color blue = new Color(34, 68, 180);
  29. setBackground(blue);
  30.  
  31. setTitle("Sid's Aquarium");
  32. setSize(600,400);
  33. setResizable(false);
  34. setLocationRelativeTo(null);
  35. addWindowListener(new WindowAdapter()
  36. { public void windowClosing(WindowEvent evt){System.exit(0);}});
  37.  
  38. //start();
  39.  
  40. setVisible(true);
  41. }
  42.  
  43. public void paint(Graphics g)
  44. {
  45. siddharth.image(g);
  46.  
  47. }
  48.  
  49. private void start()
  50. {
  51. isRunning = true;
  52. run();
  53. }
  54.  
  55. private void run()
  56. {
  57. while(isRunning)
  58. {
  59. siddharth.move();
  60. repaint();
  61. delay(1);
  62. }
  63. }
  64.  
  65. private static void delay(int num)
  66. {
  67. try{Thread.sleep(num*100);}
  68. catch(InterruptedException e){}
  69. }
  70. }
  71.  
  72. class Fish
  73. {
  74. protected int xPos;
  75. protected int yPos;
  76. protected double xPrecision;
  77. protected double yPrecision;
  78. protected int width;
  79. protected int height;
  80. protected int direction;
  81. protected int speed;
  82. protected boolean up;
  83. protected boolean down;
  84.  
  85. public Fish()
  86. {
  87.  
  88. }
  89.  
  90. public int getXPos()
  91. {
  92. return xPos;
  93. }
  94.  
  95. public int getYPos()
  96. {
  97. return yPos;
  98. }
  99.  
  100. public void setXPos(int x)
  101. {
  102. xPos = x;
  103. }
  104.  
  105. public void setYPos(int y)
  106. {
  107. yPos = y;
  108. }
  109.  
  110. void move()
  111. {
  112.  
  113. }
  114.  
  115. public void image(Graphics g)
  116. {
  117.  
  118. }
  119. }
  120.  
  121. class redFish extends Fish
  122. {
  123. Graphics2D g2d;
  124.  
  125. private redFish()
  126. {
  127.  
  128. }
  129.  
  130. public redFish(int x, int y)
  131. {
  132. xPos = x;
  133. yPos = y;
  134. xPrecision = x;
  135. yPrecision = y;
  136. }
  137.  
  138. void move()
  139. {
  140. Random rand = new Random();
  141. int change = rand.nextInt(10);
  142.  
  143. if(change == 0 ) xPos++;
  144. if(change == 1 ) xPos++;
  145. if(change == 2 ) xPos++;
  146. if(change == 3 ) xPos++;
  147. if(change == 4 ) xPos++;
  148. if(change == 5 ) xPos--;
  149. if(change == 6 ) xPos--;
  150. if(change == 7 ) xPos--;
  151. if(change == 8 ) xPos--;
  152. if(change == 9 ) xPos--;
  153. }
  154.  
  155. public void image(Graphics g)
  156. {
  157. g2d = (Graphics2D)g;
  158.  
  159.  
  160. g2d.fillRect(xPos, yPos, 100, 100);
  161. }
  162. }
Add Comment
Please, Sign In to add comment