Advertisement
Guest User

Untitled

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