Hi all. I'm currently writing a pool game in java and I'm having trouble making the cue ball move by using the mousePressed(), mouseDragged and mouseReleased() methods of mouseEvents(). When the mouse is dragged a line should appear out of the ball in opposite directions one for the power and one for the target object. Once the mouse is released the ball should travel along the direction of the line towards the target object. I have been stuck on this problem for a while adding and removing code but with no success. Any help I can get will be much appreciated. I have added the code for my main class below. Thanks in advance for your help.
1. import java.applet.*;
2. import java.util.*;
3. import java.awt.*;
4. import java.awt.event.*;
5.
6. /**class which extends applet therefore eliminating the need for a main method to run the program.
7. * also implements Runnable which adds the run method to the class.
8. * @author Steve
9. *
10. */
11. public class PoolHustler extends Applet implements Runnable, MouseListener, MouseMotionListener{
12.
13. private static final long serialVersionUID = 1L;
14.
15. // Image which passes in an image to be copied.
16. private Image i;
17.
18. //Graphics which passes in graphics to be copied.
19. private Graphics doubleBuffer;
20.
21. Ball b[] = new Ball[16];
22.
23. /creates a new instance of the Vector2 class.
24. Vector2 collision;
25.
26. int mouseDrag = 0;
27.
28. //initialise method which is called the very first time the applet is called upon.
29. public void init(){
30.
31. //sets the size of the window giving it a width and height.
32. setSize(400, 600);
33.
34. //sets the background colour of the window.
35. setBackground(Color.GREEN);
36.
37. //adds the Mouse Listener to the window on running the program the very first time.
38. addMouseListener(this);
39.
40. //adds the Mouse Motion listener to the window on running the program the very first time.
41. addMouseMotionListener(this);
42.
43. }
44.
45. //start method is called every time the applet is started.
46. public void start(){
47.
48. for(int i = 0; i < b.length; i ++){
49.
50. //initialises the first instance of the Ball class using the defined methods.
51. b[i] = new Ball();
52.
53. }
54.
55. b[0] = new Ball();
56.
57. //initialises an instance of the Vector2 class.
58. collision = new Vector2(0.0, 0.0);
59.
60. /**Threads allow multiple events to occur at the same time.
61. * Thread declares a new thread which uses the parameter 'this' to use
62. * the run method in the class.
63. */
64. Thread thread = new Thread(this);
65.
66. //thread.start which begins the thread when the program starts.
67. thread.start();
68.
69. }
70.
71. //run method which contains the code needed to run the applet.
72. public void run() {
73.
74. //infinite loop which will always be true and loops through and repaints the canvas 60 times a second.
75. while(true){
76.
77. /**calls the update method in the Ball class which contains the code for moving
78. * the ball and rebounding off the cushions.
79. */
80. b[0].update(this);
81.
82.
83. /**calls the update method in the Vector2 class which contains the code for collisions between
84. * the balls.
85. */
86. collision.update(this, b);
87.
88. /**called 60 times a second initially goes to the update method then clears the
89. * screen and calls the paint method.
90. */
91. repaint();
92.
93. /**A try and catch statement which asks the thread to try and sleep. If it fails
94. * to do so then an exception occurs and the catch statement implemented.
95. */
96. try{
97.
98. /**Thread.sleep() method which refers to the Thread class and
99. * defines the frame rate of the game by dividing 1000 by 60.
100. * The Thread.sleep() is used to allow the program to reset so
101. * that the program doesn't crash.
102. */
103. Thread.sleep(20);
104.
105. }
106.
107. //catches the exception if the program does not sleep for 20ms.
108. catch(InterruptedException e){
109.
110. //If an error occurs the error is printed to the stack trace in the debugger.
111. e.printStackTrace();
112.
113. }
114.
115. }
116.
117.
118.
119. }
120.
121. /**Update method which copies the image currently on the screen and replaces the clear
122. * screen with the copied image so when the paint method is called it paints on top of
123. * the copied image.
124. */
125. public void update(Graphics g) {
126.
127. /**an if statement which when a blank screen occurs replaces the blank screen with the
128. * image on the screen originally.
129. */
130. if(i == null){
131.
132. //creates an image which sets the width and height the same as the applet.
133. i = createImage(this.getSize().width, this.getSize().height);
134.
135. //sets the graphics to the image which has been created.
136. doubleBuffer = i.getGraphics();
137.
138. }
139.
140. //sets colour to the current background colour.
141. doubleBuffer.setColor(getBackground());
142.
143. /**fills the rectangle from the top left x and y coordinates and sets its
144. * its width and height to that of the set size of the window.
145. */
146. doubleBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
147.
148. //sets the colour to the foreground colour when used.
149. doubleBuffer.setColor(getForeground());
150.
151. //calls the paint method and passes in the doubleBuffer image.
152. paint(doubleBuffer);
153.
154. /**g refers to the Graphics parameter passed into the Update method draws
155. * the i which was set up and places it at the starting point 0 for both
156. * x and y coordinates. Image observer uses 'this'.
157. */
158. g.drawImage(i, 0, 0, this);
159.
160. }
161.
162. /**paint method with object Graphics and names the variable g paints all
163. * the graphics to the applet.
164. */
165. public void paint(Graphics g) {
166.
167. //calls the display method in the Ball class and sets the colour of ball one to white.
168. b[0].display(g, Color.WHITE);
169.
170. //calls the paint method from the Vector2 class.
171. collision.paint(g);
172.
173.
174.
175. }
176.
177. public void cuePaint(Graphics g){
178.
179. g.setColor(Color.lightGray);
180. g.drawLine(b[0].getX(), b[0].getY(), getX(), getY());
181.
182. }
183.
184. public void mouseDragged(MouseEvent e) {
185.
186. if(mouseDrag > 0){
187.
188. }
189.
190. }
191.
192. public void mousePressed(MouseEvent e) {
193.
194. mouseDrag = 1;
195.
196. }
197.
198. public void mouseReleased(MouseEvent e) {
199.
200.
201.
202. }
203.
204.
205.
206. }
207.
208.