Browse Source

Working, problem while moving mouse

master
Pierre-Antoine 'ZHAJOR' Tible 7 years ago
parent
commit
befdfc8c4a

+ 26
- 0
src/com/rthoni/uqac/pigeon/Food.java View File

@@ -0,0 +1,26 @@
1
+package com.rthoni.uqac.pigeon;
2
+
3
+/**
4
+ * Created by ZHAJOR on 30/10/2016.
5
+ */
6
+public class Food {
7
+    private int x;
8
+    private int y;
9
+
10
+    public Food(int x, int y) {
11
+        this.x = x;
12
+        this.y = y;
13
+    }
14
+
15
+    public Food() {
16
+
17
+    }
18
+
19
+    public int getX() {
20
+        return x;
21
+    }
22
+
23
+    public int getY() {
24
+        return y;
25
+    }
26
+}

+ 93
- 0
src/com/rthoni/uqac/pigeon/Game.java View File

@@ -0,0 +1,93 @@
1
+package com.rthoni.uqac.pigeon;
2
+
3
+import javax.swing.*;
4
+import javax.swing.Timer;
5
+import javax.swing.event.MouseInputListener;
6
+import java.awt.*;
7
+import java.awt.event.ActionEvent;
8
+import java.awt.event.ActionListener;
9
+import java.awt.event.MouseEvent;
10
+import java.awt.event.MouseListener;
11
+import java.util.*;
12
+import java.util.List;
13
+
14
+/**
15
+ * Created by ZHAJOR on 30/10/2016.
16
+ */
17
+public class Game extends JPanel implements MouseListener, ActionListener{
18
+
19
+    private int x, y;
20
+    private List<Pigeon> pigeons;
21
+    private List<Food> foods;
22
+
23
+    public Game(){
24
+        addMouseListener(this);
25
+        this.pigeons = new ArrayList<Pigeon>();
26
+        this.foods = new ArrayList<Food>();
27
+        for(int i = 0; i < 10; ++i){
28
+            Pigeon p = new Pigeon(""+i, this);
29
+            this.pigeons.add(p);
30
+            p.start();
31
+        }
32
+        this.createTimer();
33
+        this.setDoubleBuffered(false);
34
+    }
35
+
36
+    @Override
37
+    public void paint(Graphics g) {
38
+        Graphics2D g2d = (Graphics2D) g;
39
+        g2d.setColor(Color.RED);
40
+        // Draw piegons
41
+        for (Pigeon pigeon : this.pigeons) {
42
+            g2d.fillOval(pigeon.getX(), pigeon.getY(), 30, 30);
43
+        }
44
+        //g2d.setColor(Color.BLUE);
45
+        // Draw foods
46
+        for (Food food : this.foods) {
47
+            g2d.fillRect(food.getX(), food.getY(), 15, 15);
48
+        }
49
+    }
50
+
51
+    @Override
52
+    public void mouseClicked(MouseEvent e) {
53
+
54
+    }
55
+
56
+    @Override
57
+    public void mousePressed(MouseEvent e) {
58
+        this.foods.add(new Food(e.getX(), e.getY()));
59
+        System.out.print(e.getX() + " ");
60
+        System.out.print(e.getY() + "\n");
61
+    }
62
+
63
+    @Override
64
+    public void mouseReleased(MouseEvent e) {
65
+
66
+    }
67
+
68
+    @Override
69
+    public void mouseEntered(MouseEvent e) {
70
+
71
+    }
72
+
73
+    @Override
74
+    public void mouseExited(MouseEvent e) {
75
+
76
+    }
77
+
78
+    public List<Food> getFoods(){
79
+        return this.foods;
80
+    }
81
+
82
+    public void setFoods(List<Food> foods){
83
+        this.foods = foods;
84
+    }
85
+
86
+    private void createTimer(){
87
+        new Timer(50, this).start();
88
+    }
89
+    @Override
90
+    public void actionPerformed(ActionEvent e) {
91
+        repaint();
92
+    }
93
+}

+ 9
- 2
src/com/rthoni/uqac/pigeon/Main.java View File

@@ -1,6 +1,9 @@
1 1
 package com.rthoni.uqac.pigeon;
2 2
 
3 3
 import javax.swing.*;
4
+import java.awt.event.MouseAdapter;
5
+import java.awt.event.MouseEvent;
6
+import java.awt.event.MouseListener;
4 7
 
5 8
 /**
6 9
  * Created by robin on 10/29/16.
@@ -11,9 +14,13 @@ public class Main {
11 14
     {
12 15
         JFrame frame = new JFrame("Threads Over Pigeons");
13 16
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14
-
15
-        frame.pack();
17
+        frame.setSize(800, 800);
18
+        frame.setResizable(false);
19
+        //frame.pack();
16 20
         frame.setVisible(true);
21
+
22
+        Game g = new Game();
23
+        frame.add(g);
17 24
     }
18 25
 
19 26
     public static void main(String... argv)

+ 118
- 0
src/com/rthoni/uqac/pigeon/Pigeon.java View File

@@ -0,0 +1,118 @@
1
+package com.rthoni.uqac.pigeon;
2
+
3
+import java.util.List;
4
+import java.util.Random;
5
+
6
+/**
7
+ * Created by ZHAJOR on 30/10/2016.
8
+ */
9
+public class Pigeon implements Runnable{
10
+    private int x;
11
+    private int y;
12
+    private String name;
13
+    private Thread t;
14
+    private final Game game;
15
+    private static final int speed = 10;
16
+
17
+    public Pigeon(String name, Game g) {
18
+        Random r = new Random();
19
+        x = r.nextInt(700);
20
+        y = r.nextInt(700);
21
+        this.name = name;
22
+        this.game = g;
23
+    }
24
+
25
+    @Override
26
+    public void run() {
27
+        while (true) {
28
+            synchronized (game) {
29
+                List<Food> foods = game.getFoods();
30
+                Food best = new Food();
31
+                int bestFoodX = 0, bestFoodY = 0, distance = -1, bestIndex = -1, index = 0;
32
+                for (Food food : foods) {
33
+                    if (distance == -1) {
34
+                        distance = computeDistance(food.getX(), food.getY());
35
+                        bestFoodX = food.getX();
36
+                        bestFoodY = food.getY();
37
+                        bestIndex = index;
38
+                        best = food;
39
+                    } else if (distance > computeDistance(food.getX(), food.getY())) {
40
+                        distance = computeDistance(food.getX(), food.getY());
41
+                        bestFoodX = food.getX();
42
+                        bestFoodY = food.getY();
43
+                        bestIndex = index;
44
+                        best = food;
45
+                    }
46
+                    index++;
47
+                }
48
+                if (distance != -1 && distance <= Pigeon.speed) {
49
+                    this.x = best.getX();
50
+                    this.y = best.getY();
51
+                    foods.remove(best);
52
+                    game.setFoods(foods);
53
+                    System.out.print("Eat by " + this.name + "\n");
54
+                }
55
+                else if (distance != -1){
56
+                    this.computeNewPosition(best.getX(), best.getY());
57
+                }
58
+            }
59
+
60
+
61
+//            Random r = new Random();
62
+//            int moveX = r.nextInt(10);
63
+//            int moveY = r.nextInt(10);
64
+//            this.x += moveX;
65
+//            this.y += moveY;
66
+
67
+            try {
68
+                Thread.sleep(100);
69
+            } catch (InterruptedException e) {
70
+                e.printStackTrace();
71
+            }
72
+        }
73
+    }
74
+
75
+    private int computeDistance(int x, int y) {
76
+        int diffx = this.x > x ? this.x - x : x - this.x;
77
+        int diffy = this.y > y ? this.y - y : y - this.y;
78
+        return diffx + diffy;
79
+    }
80
+
81
+    private void computeNewPosition(int x, int y) {
82
+        int moves = Pigeon.speed;
83
+        while (moves > 0 ) {
84
+            if (this.x > x) {
85
+                this.x--;
86
+                moves--;
87
+            }
88
+            if (this.x < x) {
89
+                this.x++;
90
+                moves--;
91
+            }
92
+            if (this.y < y) {
93
+                this.y++;
94
+                moves--;
95
+            }
96
+            if (this.y > y) {
97
+                this.y--;
98
+                moves--;
99
+            }
100
+        }
101
+    }
102
+
103
+    public void start () {
104
+        System.out.println("Starting " +  name );
105
+        if (t == null) {
106
+            t = new Thread (this, name);
107
+            t.start ();
108
+        }
109
+    }
110
+
111
+    public int getX() {
112
+        return x;
113
+    }
114
+
115
+    public int getY() {
116
+        return y;
117
+    }
118
+}

Loading…
Cancel
Save