Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- /**
- * This class is responsible for the spinning wheel that is going to be
- * displayed in the game of life
- */
- // spin wheel class
- public class SpinWheel extends JFrame {
- private float degr;
- Point pPoint;
- private int xPoint;
- private int yPoint;
- public static void main(String[] args) {
- SpinWheel spin = new SpinWheel();
- }
- public SpinWheel() {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
- | UnsupportedLookAndFeelException ex) {
- ex.printStackTrace();
- }
- JFrame frame = new JFrame("Wheel Spin");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new TestPane());
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- });
- }
- public class TestPane extends JPanel {
- private float degrees = 0;
- public TestPane() {
- Timer timer = new Timer(2, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- degrees += 0.5f;
- degr = degrees;
- repaint();
- }
- });
- timer.start();
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(200, 200);
- }
- // drawing the circle
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g.create();
- // to get the background image running in the spinning wheel
- // change the image and make sure that it is numbered so the players
- // can understand it
- BufferedImage img = null;
- try {
- img = ImageIO.read(new File("1-primary.jpg"));
- } catch (IOException e) {
- System.out.println("Image not Found");
- e.printStackTrace();
- }
- g2d.drawRenderedImage(img, null);
- int diameter = Math.min(getWidth(), getHeight());
- int x = (getWidth() - diameter) / 2;
- int y = (getHeight() - diameter) / 2;
- g2d.setColor(Color.GREEN);
- g2d.drawOval(x, y, diameter, diameter);
- // outer circle and line that rotate around the wheel
- g2d.setColor(Color.BLACK);
- float innerDiameter = 20;
- Point p = getPointOnCircle(degrees, (diameter / 2f) - (innerDiameter / 2));
- g2d.fillOval(x + p.x - (int) (innerDiameter / 2), y + p.y - (int) (innerDiameter / 2), (int) innerDiameter,
- (int) innerDiameter);
- g2d.drawOval(x + p.x - (int) (innerDiameter / 2), y + p.y - (int) (innerDiameter / 2), (int) innerDiameter,
- (int) innerDiameter);
- Point l = getPointOnCircle(degrees, (diameter / 2f) - (innerDiameter / 2));
- g2d.drawLine(l.x, y + l.y - (int) (innerDiameter / 2), 100, 100);
- /**
- * This is the only place i can place this line of code in order to get the number of spaces return on the console
- * I want it to be used later so when the wheel closes itself that it will return a number back to a different class
- */
- System.out.println(getNumSpaces());
- g2d.dispose();
- }
- // method to determine spaces using the xy coordinate from the spin
- // circle
- public double getNumSpaces() {
- double numSpaces = 0;
- // for red zone
- if (pPoint.getX()>= 89 && pPoint.getY() >=59) {
- numSpaces = 1;
- }
- // for blue zone
- if (pPoint.getX() <= 185 && pPoint.getY() <= 70) {
- numSpaces = 2;
- }
- if (pPoint.getX() <= 89 && pPoint.getY() >= 57) {
- numSpaces = 3;
- }
- return numSpaces;
- }
- public Point getPointOnCircle(float degress, float radius) {
- int x = Math.round(getWidth() / 2);
- int y = Math.round(getHeight() / 2);
- double rads = Math.toRadians(degress - 90); // 0 becomes the top
- // Calculate the outter point of the line
- int xPosy = Math.round((float) (x + Math.cos(rads) * radius));
- int yPosy = Math.round((float) (y + Math.sin(rads) * radius));
- return pPoint = new Point(xPosy, yPosy);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment