Results 1 to 4 of 4
- 05-05-2011, 08:54 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
How to make object stop moving with mouse click
Hi my assignment is to make a checkers board with 4 squares and two checkers moving in opposite directions. I have to then make it so the checkers stop moving when you press and hold the mouse then they start moving again when you release it. I have the board and checkers set up and moving, im just not sure how to make them stop with a mouse click. I know your supposed to use the mouse listener but im not sure how to write the code that goes in mouse pressed and mouse released(ie how to make the checker stop moving.. If someone could steer me in the right direction that would be great. My thinking would be to make it stop repainting when the mouse clicks then start repainting when the mouse is released but im not too sure on how to do that. Heres my code thus far.
Java Code:import java.awt.Graphics; import java.awt.Color; import java.awt.Image; public class Checkers extends java.applet.Applet implements Runnable, MouseListener { Thread runner; int ypos, xpos; Image offscreenImg; Graphics offscreenG; int num = 1; public void init() { offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); } public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while(num ==1) { while (ypos <= 105) { for (ypos = 5; ypos <= 105; ypos+=5) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } while(ypos >= 5) { for (ypos = 105; ypos >= 5; ypos-=5) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // Draw background onto the buffer area offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,100,100); offscreenG.setColor(Color.black); offscreenG.fillRect(100,100,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(100,0,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(0,100,100,100); // Draw checker offscreenG.setColor(Color.red); offscreenG.fillOval(ypos,5,90,90); offscreenG.setColor(Color.blue); offscreenG.fillOval(105,ypos,90,90); // Now, transfer the entire buffer onto the screen g.drawImage(offscreenImg,0,0,this); } public void destroy() { offscreenG.dispose(); } }Last edited by mackavelirip; 05-05-2011 at 08:58 AM.
- 05-05-2011, 04:00 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 12
- Rep Power
- 0
This may not be the best way to do it, but what you could do is set up a MouseListener on your panel. In the mousePressed method you can "pause" the thread that your graphics are working over. Then in the mouseReleased just resume the thread.
Java Code:gamePanel.addMouseListener(new MouseListener(){ @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // Pause thread. } @Override public void mouseReleased(MouseEvent arg0) { // Resume thread. } });Last edited by erversteeg; 05-05-2011 at 04:19 PM.
- 05-06-2011, 02:52 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Thank you for you help. I tried what you said and now when I click it stops but when I release it doesnt start moving again.
Heres my code.
Java Code:import java.awt.Graphics; import java.awt.Color; import java.awt.Image; import java.awt.event.*; public class Checkers extends java.applet.Applet implements Runnable, MouseListener { Thread runner; int xpos, ypos; Image offscreenImg; Graphics offscreenG; boolean pleaseWait = false; public void init() { offscreenImg = createImage(this.size().width, this.size().height); offscreenG = offscreenImg.getGraphics(); addMouseListener(this); } public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) { while(true) { pleaseWait = true; } } public void mouseReleased(MouseEvent e) { pleaseWait = false; } public void start() { if (runner == null); { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void run() { while(pleaseWait == false) { //Top checker starting position xpos = 5; //Bottom Checker starting position ypos = 105; //infinit loop to keep checkers moving while(true) { //moves checkers the from one side to the other while (xpos < 105 && ypos > 5) { xpos += 5; ypos -= 5; repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } //moves checkers from the other side while(xpos > 5 && ypos < 105) { xpos -= 5; ypos += 5; repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // Draw background onto the buffer area offscreenG.setColor(Color.black); offscreenG.fillRect(0,0,100,100); offscreenG.setColor(Color.black); offscreenG.fillRect(100,100,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(100,0,100,100); offscreenG.setColor(Color.gray); offscreenG.fillRect(0,100,100,100); // Draw checker offscreenG.setColor(Color.red); offscreenG.fillOval(xpos,5,90,90); offscreenG.setColor(Color.blue); offscreenG.fillOval(ypos,105,90,90); // Now, transfer the entire buffer onto the screen g.drawImage(offscreenImg,0,0,this); } public void destroy() { offscreenG.dispose(); } }
-
You need to use a background thread for this so you don't call Thread.sleep on the GUI's thread putting the GUI to sleep, either that or use a timer of some sorts. I've used a Swing Timer for Swing apps, but I'm not sure if it would work for AWT apps -- don't know.
Similar Threads
-
Moving java shapes with mouse and buttons
By Haraldjjones in forum New To JavaReplies: 1Last Post: 01-20-2011, 09:26 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM -
Help with mouse moving object and colisions
By macwadu in forum AWT / SwingReplies: 12Last Post: 07-01-2010, 11:18 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
change object color on mouse click
By gotenks05 in forum Java AppletsReplies: 1Last Post: 04-05-2009, 07:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks