Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package lab7;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.util.ArrayList;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JDesktopPane;
  8. import javax.swing.JFrame;
  9. import javax.swing.JInternalFrame;
  10.  
  11. public class Game {
  12.     public static void main(String[] args) {
  13.  
  14.         ArrayList<Player> players = new ArrayList<>();
  15.         players.add(new Player("1"));
  16.         players.add(new Player("2"));
  17.         players.add(new Player("3"));
  18.         players.add(new Player("4"));
  19.         players.add(new Player("5"));
  20.         players.add(new Player("67"));
  21.         players.add(new Player());
  22.         players.add(new Player());
  23.         players.add(new Player());
  24.  
  25.         Racing race = new Racing(players);
  26.  
  27.         Scene scene1 = new Scene(race);
  28.         scene1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         /*
  30.          * JDesktopPane desk = new JDesktopPane(); JInternalFrame frame1 = new
  31.          * JInternalFrame("Internal Frame", true, true, true, true);
  32.          * JInternalFrame frame2 = new JInternalFrame("Internal Frame", true,
  33.          * true, true, true); JFrame frame = new JFrame(); frame.add(desk);
  34.          * frame1.add(new JButton(), BorderLayout.CENTER);
  35.          * frame1.setVisible(true); frame1.setSize(300, 300);
  36.          * frame2.setVisible(true); frame2.setSize(300, 300); desk.add(frame1);
  37.          * desk.add(frame2); frame.setVisible(true);
  38.          * frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.          * frame.setSize(300, 300); frame1.setVisible(true);
  40.          */
  41.  
  42.     }
  43. }
  44. package lab7;
  45.  
  46. import java.awt.Color;
  47. import java.awt.event.MouseAdapter;
  48. import java.awt.event.MouseEvent;
  49.  
  50. import javax.swing.JButton;
  51.  
  52. public class Player extends Thread {
  53.  
  54.     private static int count = 0;
  55.     private String name;
  56.     private JButton button;
  57.     private Color color;
  58.     private int x;
  59.     private Racing race;
  60.  
  61.     public Player() {
  62.         count++;
  63.         name = "Player" + count;
  64.         init();
  65.     }
  66.  
  67.     public Player(String name) {
  68.         this.name = name;
  69.         init();
  70.     }
  71.  
  72.     private void init() {
  73.         x = 10;
  74.         button = new JButton(name);
  75.         button.addMouseListener(new MouseAdapter() {
  76.             @Override
  77.             public void mouseReleased(MouseEvent e) {
  78.                 if (e.getButton() == MouseEvent.BUTTON1) {
  79.                     race.play();
  80.                 }
  81.                 if (e.getButton() == MouseEvent.BUTTON2) {
  82.                     race.restart();
  83.                 }
  84.             }
  85.         });
  86.         color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
  87.     }
  88.  
  89.     public void setRacing(Racing race) {
  90.         this.race = race;
  91.     }
  92.  
  93.     public void restart() {
  94.         x = 10;
  95.         button.setLocation(x, (int) button.getLocation().getY());
  96.     }
  97.  
  98.     public void run() {
  99.         for (;;) {
  100.             while (!race.getPlay()) {
  101.                 try {
  102.                     synchronized (this) {
  103.                         this.wait();
  104.                     }
  105.                 } catch (InterruptedException e) {
  106.                     e.printStackTrace();
  107.                 }
  108.             }
  109.             double a = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
  110.             double b = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
  111.             double c = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
  112.             int speed = (int) (Math
  113.                     .sqrt(Math.sqrt(Math.sqrt(a * a + b * b + c * c + 2 * c * b + 2 * a * c + 2 * a * b))));
  114.             // int speed = (int) (Math.random() * 9) + 1;
  115.             x += speed;
  116.             button.setLocation(x, (int) button.getLocation().getY());
  117.  
  118.             if (x + button.getWidth() >= race.getFinish()) {
  119.                 race.stop(this);
  120.             }
  121.  
  122.             try {
  123.                 sleep((int) (a + b + c) / 15);
  124.             } catch (InterruptedException e) {
  125.                 e.printStackTrace();
  126.             }
  127.         }
  128.     }
  129.  
  130.     public int getX() {
  131.         return x;
  132.     }
  133.  
  134.     public JButton getButton() {
  135.         return button;
  136.     }
  137.  
  138.     public Color getColor() {
  139.         return color;
  140.     }
  141.  
  142.     public void setColor(int a, int b, int c) {
  143.         color = new Color(a, b, c);
  144.     }
  145.  
  146.     public String getNick() {
  147.         return name;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement