Roman4525

Untitled

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