Roman4525

Untitled

Apr 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.image.BufferedImage;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Random;
  16.  
  17. import javax.imageio.ImageIO;
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20. import javax.swing.Timer;
  21. import javax.swing.UIManager;
  22. import javax.swing.UnsupportedLookAndFeelException;
  23.  
  24. /**
  25. * This class is responsible for the spinning wheel that is going to be
  26. * displayed in the game of life
  27. */
  28.  
  29. // spin wheel class
  30. public class SpinWheel extends JFrame {
  31. private float degr;
  32. Point pPoint;
  33. private int xPoint;
  34. private int yPoint;
  35.  
  36. public static void main(String[] args) {
  37.  
  38. SpinWheel spin = new SpinWheel();
  39.  
  40. }
  41.  
  42. public SpinWheel() {
  43. EventQueue.invokeLater(new Runnable() {
  44. @Override
  45. public void run() {
  46. try {
  47. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  48. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
  49. | UnsupportedLookAndFeelException ex) {
  50. ex.printStackTrace();
  51. }
  52. JFrame frame = new JFrame("Wheel Spin");
  53. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. frame.add(new TestPane());
  55. frame.pack();
  56. frame.setLocationRelativeTo(null);
  57. frame.setVisible(true);
  58. }
  59. });
  60. }
  61.  
  62. public class TestPane extends JPanel {
  63.  
  64. private float degrees = 0;
  65.  
  66. public TestPane() {
  67. Timer timer = new Timer(2, new ActionListener() {
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent e) {
  71. degrees += 0.5f;
  72.  
  73. degr = degrees;
  74. repaint();
  75. }
  76. });
  77. timer.start();
  78.  
  79. }
  80.  
  81. @Override
  82. public Dimension getPreferredSize() {
  83. return new Dimension(200, 200);
  84. }
  85.  
  86. // drawing the circle
  87. @Override
  88. public void paintComponent(Graphics g) {
  89. super.paintComponent(g);
  90. Graphics2D g2d = (Graphics2D) g.create();
  91. // to get the background image running in the spinning wheel
  92. // change the image and make sure that it is numbered so the players
  93. // can understand it
  94. BufferedImage img = null;
  95. try {
  96. img = ImageIO.read(new File("1-primary.jpg"));
  97.  
  98. } catch (IOException e) {
  99. System.out.println("Image not Found");
  100. e.printStackTrace();
  101. }
  102.  
  103. g2d.drawRenderedImage(img, null);
  104.  
  105. int diameter = Math.min(getWidth(), getHeight());
  106. int x = (getWidth() - diameter) / 2;
  107. int y = (getHeight() - diameter) / 2;
  108.  
  109. g2d.setColor(Color.GREEN);
  110. g2d.drawOval(x, y, diameter, diameter);
  111.  
  112. // outer circle and line that rotate around the wheel
  113. g2d.setColor(Color.BLACK);
  114. float innerDiameter = 20;
  115.  
  116. Point p = getPointOnCircle(degrees, (diameter / 2f) - (innerDiameter / 2));
  117.  
  118. g2d.fillOval(x + p.x - (int) (innerDiameter / 2), y + p.y - (int) (innerDiameter / 2), (int) innerDiameter,
  119. (int) innerDiameter);
  120. g2d.drawOval(x + p.x - (int) (innerDiameter / 2), y + p.y - (int) (innerDiameter / 2), (int) innerDiameter,
  121. (int) innerDiameter);
  122.  
  123. Point l = getPointOnCircle(degrees, (diameter / 2f) - (innerDiameter / 2));
  124.  
  125. g2d.drawLine(l.x, y + l.y - (int) (innerDiameter / 2), 100, 100);
  126.  
  127. /**
  128. * This is the only place i can place this line of code in order to get the number of spaces return on the console
  129. * I want it to be used later so when the wheel closes itself that it will return a number back to a different class
  130. */
  131. System.out.println(getNumSpaces());
  132.  
  133.  
  134. g2d.dispose();
  135. }
  136.  
  137. // method to determine spaces using the xy coordinate from the spin
  138. // circle
  139. public double getNumSpaces() {
  140. double numSpaces = 0;
  141. // for red zone
  142. if (pPoint.getX()>= 89 && pPoint.getY() >=59) {
  143. numSpaces = 1;
  144. }
  145. // for blue zone
  146. if (pPoint.getX() <= 185 && pPoint.getY() <= 70) {
  147. numSpaces = 2;
  148. }
  149. if (pPoint.getX() <= 89 && pPoint.getY() >= 57) {
  150. numSpaces = 3;
  151. }
  152.  
  153. return numSpaces;
  154. }
  155.  
  156. public Point getPointOnCircle(float degress, float radius) {
  157.  
  158. int x = Math.round(getWidth() / 2);
  159. int y = Math.round(getHeight() / 2);
  160.  
  161. double rads = Math.toRadians(degress - 90); // 0 becomes the top
  162.  
  163. // Calculate the outter point of the line
  164. int xPosy = Math.round((float) (x + Math.cos(rads) * radius));
  165. int yPosy = Math.round((float) (y + Math.sin(rads) * radius));
  166.  
  167. return pPoint = new Point(xPosy, yPosy);
  168.  
  169. }
  170.  
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment