Results 1 to 3 of 3
  1. #1
    M.a.T is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Help with this Swing app. please.

    Hey, im facing a problem with an application im trying to put together.

    Explanation:
    The application will have this ball moving around and saving the coords on objX[] and objY[].

    Whilst the ball is moving, the user will follow it with the mouse cursor. The mouse coords will be saved in coordX[] and coordY[].

    After that I'll compare the ball coords with the mouse coords and check how close the user tracked the ball.

    Problems:
    The coordinates from mouse arent matching the ball's. I know the mouse cursos uses the window's coordinate system and the ball uses the JPanel's coordinates.

    I don't know how to change the balls coords to match the cursors coords system.

    Also, if anyone bothers to run the application: The application prints the mouse coords + ball coords after it stop moving.
    You will notice the second half of the coordX and coordY are not being saved.
    It will print 1000 times.

    About 500 of them the mouse coords appear as [0,0] and i dont know why.

    Please help with this application, its the first "big" application im doing with SWING.

    This is my main class
    Java Code:
    import javax.swing.JFrame;
    
    public class BounceThread {
    	public static void main(String[] args) {
    		JFrame frame = new BounceThreadFrame();
    		frame.setVisible(true);
    		
    	}
    
    }
    And the Frame + ball (removed all imports to save space, but they are all there)
    Java Code:
    public class BounceThreadFrame extends JFrame {
    	public BounceThreadFrame() {
    		setSize(800, 600);
    		setTitle("Tracking");
    
    		addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    
    		Container contentPane = getContentPane();
    		canvas = new JPanel();
    		canvas.setSize(800, 600);
    		contentPane.add(canvas, "Center");
    
    		MouseHandler handler = new MouseHandler();
    		canvas.addMouseMotionListener(handler);
    		
    		JPanel p = new JPanel();
    		addButton(p, "Start", new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				Ball b = new Ball(canvas);
    				b.start();
    			}
    		});
    
    		addButton(p, "Close", new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				canvas.setVisible(false);
    
    				System.exit(0);
    			}
    		});
    
    		contentPane.add(p, "South");
    
    	}
    
    	public void addButton(Container c, String title, ActionListener a) {
    		JButton b = new JButton(title);
    		c.add(b);
    		b.addActionListener(a);
    	}
    	
    	private class MouseHandler implements MouseMotionListener {
    
    		public void mouseMoved(MouseEvent e) {
    			saveCoord(e.getX(), e.getY());
    		}
    
    		public void mouseDragged(MouseEvent e) {
    		}
    		
    
    	}
    	public void saveCoord(int x, int y) {
    		
    		coordX[m] = x;
    		coordY[m] = y;
    		m++;
    	}
    	public class Ball extends Thread {
    		public Ball(JPanel b) {
    			box = b;
    		}
    
    		public void draw() {
    			Graphics g = box.getGraphics();
    			g.fillOval(x, y, XSIZE, YSIZE);
    			g.dispose();
    		}
    
    		public void move() {
    			if (!box.isVisible())
    				return;
    			Graphics g = box.getGraphics();
    			
    			g.setXORMode(box.getBackground());
    			g.fillOval(x, y, XSIZE, YSIZE);
    			x += dx;
    			y += dy;
    
    			Dimension d = box.getSize();
    			if (x < 0) {
    				x = 0;
    				dx = -dx;
    			}
    			if (x + XSIZE >= d.width) {
    				x = d.width - XSIZE;
    				dx = -dx;
    			}
    			if (y < 0) {
    				y = 0;
    				dy = -dy;
    			}
    			if (y + YSIZE >= d.height) {
    				y = d.height - YSIZE;
    				dy = -dy;
    			}
    
    			g.fillOval(x, y, XSIZE, YSIZE);
    			g.dispose();
    		}
    
    		public void run() {
    			try {
    				
    				draw();
    				for (int i = 1; i <= 1000; i++) {
    					move();
    					sleep(5);
    					objX[i] = x;
    					objY[i] = y;
    					
    				}
    				for (int a = 0; a < objX.length; a++)
    					System.out.println("["+coordX[a]+","+coordY[a]+"]"+" - "+"["+objX[a] + ", " + objY[a]+"]");
    			} catch (InterruptedException e) {
    			}
    		}
    
    		private JPanel box;
    
    		private static final int XSIZE = 50;
    
    		private static final int YSIZE = 50;
    
    		private int x = 0;
    
    		private int y = 0;
    
    		private int dx = 1;
    
    		private int dy = 1;
    
    		private int[] objX = new int[1001];
    
    		private int[] objY = new int[1001];
    	}
    		
    	private int[] coordX = new int[1000000];
    
    	private int[] coordY = new int[1000000];
    
    	private int m = 0;
    
    	private JPanel canvas;
    
    }
    Sorry for the long post.

    I really need this help! :(

    Thanks in advance.

    M.a.T
    Last edited by M.a.T; 04-15-2010 at 03:48 PM. Reason: Changing Title to a more suitable one.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I see two problems here right off the bat, the first one that you know about: your mouse and your ball use two different coordinate systems. This can be easily solved by using screen coordinates throughout. The ball's screen coordinates can be calculated by getting the ball's coordinates and adding to (in a math vector fashion) it the screen coordinates of the JPanel that is displaying the ball. Don't forget to calculate the center of the ball, not the upper left coordinates, and this will require just a little more math.

    Your other and perhaps more pernicious problem is one of synchronization -- getting coordinates for the mouse and the ball at the same time, and this you're not doing. The ball has its coordinates recorded every 5 seconds or so (probably slower is my gues), but the mouse gets recorded only when it moves, and so there's a significant disconnect here. This is in fact why your mouse coordinates show many [0, 0] at the end. And your output displaying one set of coordinates next to the other is misleading as it suggests that both were obtained simultaneously when in fact they weren't.

    One solution to this is not to use a MouseListener that passively gets the mouse position when the mouse moves, but instead pull the mouse position from the mouse itself in your game loop by using the MouseInfo class's getPointerInfo() static method. Check out the API for this.
    Edit: Please note that I've never done this myself, but that this is my interpretation of what would likely work best based on my review of the API. If anyone knows differently, please correct me!

    Edit: also, I would not use a background thread for all of this but instead would use a Swing Timer. It decreases the risk of calling a Swing-related method off of the EDT.

    Edit 2: Also, you're getting your JPanel's Graphics object by calling getGraphics on the JPanel and this is not how to do most Swing painting. Instead you should be drawing your ball passively in the JPanel's paintComponent method, using the Graphics object passed to the method by the system.

    Edit 3: If you're wise, you'll delete all references to "urgent" and similar statements from your current and future posts. Yes, you may be in a rush, but we're not, and no one likes to feel pushed or rushed to do anything, least of all a volunteer who might otherwise help you. I've often seen folks not get help they otherwise would have because they were pushing the forum members too hard. Easing up can go a long way towards improving your chances of getting help.

    Best of luck
    Last edited by Fubarable; 04-15-2010 at 06:28 PM.

  3. #3
    M.a.T is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    I'll check on those things and post the results.

    Thanks a lot for the reply, Ill change the topic's title now :D

    Wasnt intending to hurry anyone. Its just that this app development is pretty much crossing all deadlines given. And I just didnt have enough time to study Swing and create such app.

    Anyways, Ill be back later.

    Thanks again.

Similar Threads

  1. Urgent!!!
    By winnie99 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-18-2009, 03:17 PM
  2. Need help urgent! Is this possible ?
    By meodihia in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 09-28-2008, 09:05 PM
  3. Swing Tables - urgent
    By varun2002 in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:44 PM
  4. Help me ...urgent!
    By googgoo in forum New To Java
    Replies: 7
    Last Post: 04-05-2008, 08:46 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •