Advertisement
Guest User

Untitled

a guest
May 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Font;
  4. import java.awt.Frame;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9.  
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. public class Gameboard extends JPanel implements MouseListener {
  14.  
  15. public Color smallTarget = Color.RED;
  16. public Color mediumTarget = Color.WHITE;
  17. public Color largeTarget = Color.WHITE;
  18. public Color slowSpeed = Color.RED;
  19. public Color mediumSpeed = Color.WHITE;
  20. public Color fastSpeed = Color.WHITE;
  21. public boolean startButton = false;
  22. public static int targetSize = 0;
  23. public static int speed = 0;
  24.  
  25. public int width = getWidth();
  26. public int height = getHeight();
  27.  
  28. // public static Time time;
  29. private Target targetInstance;
  30. private TargetGroup targetGroup;
  31. private MouseEvent e;
  32. private Gameboard gameboard;
  33. Time time = new Time(gameboard);
  34.  
  35. static Gameboard panel = new Gameboard();
  36.  
  37. public void paint(Graphics g)
  38. {
  39. super.paint(g);
  40.  
  41. int width = getWidth();
  42. int height = getHeight();
  43.  
  44. Color background = Color.GRAY;
  45.  
  46. double ratioX = (double)width/300.0;
  47. double ratioY = (double)height/200.0;
  48.  
  49. Graphics2D g2 = (Graphics2D)g;
  50. g2.scale(ratioX, ratioY);
  51.  
  52.  
  53. setBackground(background);
  54.  
  55. // Target size text
  56. if (startButton == false)
  57. {
  58. g.setColor(Color.WHITE);
  59. g.setFont(new Font("Times New Roman", Font.PLAIN, 12));
  60. g.drawString("Size of Targets", 50, 40);
  61.  
  62. // Small size Target button
  63. g.setColor(smallTarget);
  64. g.fillRect(50, 50, 20, 20);
  65.  
  66. // Medium size Target button
  67. g.setColor(mediumTarget);
  68. g.fillRect(90, 50, 30, 30);
  69.  
  70. // Large size Target button
  71. g.setColor(largeTarget);
  72. g.fillRect(140, 50, 40, 40);
  73.  
  74. // Slow speed button
  75. g.setColor(slowSpeed);
  76. g.setFont(new Font ("", Font.BOLD, 8));
  77. g.drawString("Slow", 55, 142);
  78. g.drawRect(50, 130, 30, 20); // 50 + 30 = 80
  79.  
  80. // Speed text
  81. g.setColor(Color.WHITE);
  82. g.setFont(new Font("Times New Roman", Font.BOLD, 12));
  83. g.drawString("Speed", 50, 110);
  84.  
  85. // Slow speed button
  86. g.setColor(slowSpeed);
  87. g.setFont(new Font ("", Font.BOLD, 8));
  88. g.drawString("Slow", 55, 142);
  89. g.drawRect(50, 130, 30, 20); // 50 + 30 = 80
  90.  
  91. // Medium speed button
  92. g.setColor(mediumSpeed);
  93. g.setFont(new Font ("", Font.PLAIN, 8));
  94. g.drawString("Medium", 105, 142);
  95. g.drawRect(100, 130, 40, 20); // 100 + 40 = 140
  96.  
  97. // Fast speed button
  98. g.setColor(fastSpeed);
  99. g.setFont(new Font ("", Font.PLAIN, 8));
  100. g.drawString("Fast", 167, 142);
  101. g.drawRect(160, 130, 30, 20); // 160 + 30 = 190
  102.  
  103. // Start button
  104. g.setColor(Color.WHITE);
  105. g.setFont(new Font ("", Font.PLAIN, 8));
  106. g.drawString("Start!", 215, 100);
  107. g.drawRect(210, 90, 30, 15); // 210 + 30 = 240
  108. }
  109.  
  110.  
  111. if (startButton)
  112. {
  113. background = Color.GRAY;
  114. // g.clearRect(0, 0, width, height);
  115. setSize(targetSize);
  116. // setSpeed(speed);
  117. // targetGroup.draw(g);
  118. repaint();
  119. }
  120.  
  121. if (startButton)
  122. {
  123. try {
  124. time.start(g);
  125. } catch (InterruptedException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. }
  129. targetGroup.draw(g);
  130. }
  131. }
  132.  
  133.  
  134. public void setSize(int size)
  135. {
  136. targetGroup = new TargetGroup(size);
  137. }
  138.  
  139. // public void setSpeed(int speed)
  140. // {
  141. // time = new Time(speed);
  142. // }
  143.  
  144.  
  145. public static void main(String[] args)
  146. {
  147. JFrame w = new JFrame("Gameboard");
  148. w.setBounds(300, 300, 800, 600);
  149. w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  150. // panel.setBackground(Color.GRAY);
  151. w.add(panel);
  152. w.setResizable(true);
  153. w.setVisible(true);
  154. panel.addMouseListener(panel);
  155. }
  156.  
  157. @Override
  158. public void mouseClicked(MouseEvent e)
  159. {
  160.  
  161. }
  162.  
  163. @Override
  164. public void mouseEntered(MouseEvent e) {
  165. // TODO Auto-generated method stub
  166.  
  167. }
  168.  
  169. @Override
  170. public void mouseExited(MouseEvent e) {
  171. // TODO Auto-generated method stub
  172.  
  173. }
  174.  
  175. @Override
  176. public void mousePressed(MouseEvent e) {
  177.  
  178. int width = getWidth();
  179. int height = getHeight();
  180.  
  181. double ratioX = width/300.0;
  182. double ratioY = height/200.0;
  183.  
  184. int mouseX = e.getX();
  185. int mouseY = e.getY();
  186.  
  187. if (startButton)
  188. {
  189. targetGroup.checksClick(mouseX, mouseY);
  190. return; // prevents everything below from running
  191. }
  192.  
  193. // If user clicked small Target button
  194. if (startButton == false && mouseX >= (int)(50 * ratioX) && mouseX <= (int)(70 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(70 * ratioY)) // If cursor clicked on small Target button
  195. {
  196. smallTarget = Color.RED;
  197. mediumTarget = Color.WHITE;
  198. largeTarget = Color.WHITE;
  199. // If user clicked medium Target button
  200. } else if (startButton == false && mouseX >= (int)(90 * ratioX) && mouseX <= (int)(120 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(80 * ratioY)) // If cursor clicked on medium Target button
  201. {
  202. smallTarget = Color.WHITE;
  203. mediumTarget = Color.RED;
  204. largeTarget = Color.WHITE;
  205. // If user clicked large Target button
  206. } else if (startButton == false && mouseX >= (int)(140 * ratioX) && mouseX <= (int)(180 * ratioX) && mouseY >= (int)(50 * ratioY) && mouseY <= (int)(90 * ratioY)) // If cursor clicked on large Target setting
  207. {
  208. smallTarget = Color.WHITE;
  209. mediumTarget = Color.WHITE;
  210. largeTarget = Color.RED;
  211. // If user clicked slow speed button
  212. } else if (startButton == false && mouseX >= (int)(50 * ratioX) && mouseX <= (int)(80 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
  213. {
  214. slowSpeed = Color.RED;
  215. mediumSpeed = Color.WHITE;
  216. fastSpeed = Color.WHITE;
  217. // If user clicked medium speed button
  218. } else if (startButton == false && mouseX >= (int)(100 * ratioX) && mouseX <= (int)(140 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
  219. {
  220. slowSpeed = Color.WHITE;
  221. mediumSpeed = Color.RED;
  222. fastSpeed = Color.WHITE;
  223. // If user clicked fast speed button
  224. } else if (startButton == false && mouseX >= (int)(160 * ratioX) && mouseX <= (int)(190 * ratioX) && mouseY >= (int)(130 * ratioY) && mouseY <= (int)(150 * ratioY))
  225. {
  226. slowSpeed = Color.WHITE;
  227. mediumSpeed = Color.WHITE;
  228. fastSpeed = Color.RED;
  229. // If user clicked start button
  230. } else if (startButton == false && mouseX >= (int)(210 * ratioX) && mouseX <= (int)(240 * ratioX) && mouseY >= (int)(90 * ratioY) && mouseY <= (int)(105 * ratioY))
  231. {
  232. startButton = true;
  233.  
  234. // sets targetSize value
  235. if (smallTarget == Color.RED)
  236. {
  237. targetSize = 20;
  238. } else if (mediumTarget == Color.RED)
  239. {
  240. targetSize = 30;
  241. } else if (largeTarget == Color.RED)
  242. {
  243. targetSize = 40;
  244. }
  245.  
  246. // sets speed value
  247. if (slowSpeed == Color.RED)
  248. {
  249. speed = 1500;
  250. } else if (mediumSpeed == Color.RED)
  251. {
  252. speed = 1000;
  253. } else if (fastSpeed == Color.RED)
  254. {
  255. speed = 750;
  256. }
  257. }
  258. repaint();
  259. }
  260.  
  261. @Override
  262. public void mouseReleased(MouseEvent e) {
  263. // TODO Auto-generated method stub
  264.  
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement