Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab7;
- import java.awt.BorderLayout;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JDesktopPane;
- import javax.swing.JFrame;
- import javax.swing.JInternalFrame;
- public class Game {
- public static void main(String[] args) {
- ArrayList<Player> players = new ArrayList<>();
- players.add(new Player("1"));
- players.add(new Player("2"));
- players.add(new Player("3"));
- players.add(new Player("4"));
- players.add(new Player("5"));
- players.add(new Player("67"));
- players.add(new Player());
- players.add(new Player());
- players.add(new Player());
- Racing race = new Racing(players);
- Scene scene1 = new Scene(race);
- scene1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- /*
- * JDesktopPane desk = new JDesktopPane(); JInternalFrame frame1 = new
- * JInternalFrame("Internal Frame", true, true, true, true);
- * JInternalFrame frame2 = new JInternalFrame("Internal Frame", true,
- * true, true, true); JFrame frame = new JFrame(); frame.add(desk);
- * frame1.add(new JButton(), BorderLayout.CENTER);
- * frame1.setVisible(true); frame1.setSize(300, 300);
- * frame2.setVisible(true); frame2.setSize(300, 300); desk.add(frame1);
- * desk.add(frame2); frame.setVisible(true);
- * frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- * frame.setSize(300, 300); frame1.setVisible(true);
- */
- }
- }
- package lab7;
- import java.awt.Color;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JButton;
- public class Player extends Thread {
- private static int count = 0;
- private String name;
- private JButton button;
- private Color color;
- private int x;
- private Racing race;
- public Player() {
- count++;
- name = "Player" + count;
- init();
- }
- public Player(String name) {
- this.name = name;
- init();
- }
- private void init() {
- x = 10;
- button = new JButton(name);
- button.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseReleased(MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON1) {
- race.play();
- }
- if (e.getButton() == MouseEvent.BUTTON2) {
- race.restart();
- }
- }
- });
- color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
- }
- public void setRacing(Racing race) {
- this.race = race;
- }
- public void restart() {
- x = 10;
- button.setLocation(x, (int) button.getLocation().getY());
- }
- public void run() {
- for (;;) {
- while (!race.getPlay()) {
- try {
- synchronized (this) {
- this.wait();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- double a = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
- double b = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
- double c = Math.random() * 2 * Math.random() * 3 * Math.random() * 5 * Math.random() * 7;
- int speed = (int) (Math
- .sqrt(Math.sqrt(Math.sqrt(a * a + b * b + c * c + 2 * c * b + 2 * a * c + 2 * a * b))));
- // int speed = (int) (Math.random() * 9) + 1;
- x += speed;
- button.setLocation(x, (int) button.getLocation().getY());
- if (x + button.getWidth() >= race.getFinish()) {
- race.stop(this);
- }
- try {
- sleep((int) (a + b + c) / 15);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public int getX() {
- return x;
- }
- public JButton getButton() {
- return button;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(int a, int b, int c) {
- color = new Color(a, b, c);
- }
- public String getNick() {
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement