Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Object not following mouse!?

  1. #1
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Object not following mouse!?

    Hello everyone. I'm still trying to make a game. XD Right now I'm trying to make a square so that you select it and then click a place and the square will move to that place. I have a program where one square can do that but I need to make it where more than one square can move. Can you help me?
    Here is the program where one square will move:
    public class GameMove extends JFrame
    {
    	//Alot of global vairables i took out to decrease size of post
    	public static void main(String[] args)
    	{
    		new GameMove();
    	}
    	public GameMove()
    	{
    		Runnable h = new AnimateThread(this);
    		Thread t = new Thread(h);
    		t.start();
    		this.setSize(WIDTH,HEIGHT);
    		this.setTitle("Game");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.add(new PaintScreen(), BorderLayout.CENTER);
    		this.setVisible(true);
    	}
    	private class PaintScreen extends JComponent
    	{
    		public PaintScreen()
    		{
    			this.addMouseListener(new MouseAdapter()
    			{
    				public void mousePressed(MouseEvent e)
    				{
    					mouseX = e.getX();
    					mouseY = e.getY();
    				}
    				public void mouseClicked(MouseEvent e)
    				{
    					//Sees if mouse is in the square
    					if(s.contains(e.getPoint()))
    					{
    						if(isIn)
    						{
    							if(isClicked == true)
    							{	
    								isClicked = false;
    							}
    							else if(isClicked == false)
    							{
    								isClicked = true;
    							}
    						}
    						else 
    						{
    							isIn = true;
    						}
    					}
    					else
    					{
    						isIn = false;
    					}
    				}
    			});
    		}
    		public void paint(Graphics g)
    		{
    			Graphics2D g2 = (Graphics2D) g;
    			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    			s = new Rectangle2D.Float(x,y,20,20);
    			s2 = new Rectangle2D.Float(x2,y2,20,20);
    			if (isClicked == true)
    			{
    				g2.setColor(Color.GREEN);
    				g2.fill(s);
    				g2.fill(s2);
    			}
    			else
    			{
    				g2.setColor(Color.BLACK);
    				g2.fill(s);
    				g2.fill(s2);
    			}
    			if(isClicked && isIn == false)
    			{
    				if(mouseX != x)
    				{
    					if(mouseX > x)
    					{
    						x+= 1;
    					}
    					if(mouseX < x)
    					{
    						x -= 1;
    					}
    				}
    				if (mouseY != y)
    				{
    					if (mouseY > y)
    					{
    						y += 1;
    					}
    					if (mouseY < y)
    					{
    						y -= 1;
    					}
    				}
    			}
    			g2.draw(s);
    		}
    	}
    	private class AnimateThread implements Runnable
    	{
    		JFrame c;
    		public AnimateThread(JFrame c)
    		{
    			this.c = c;
    		}
    		public void run()
    		{
    			while(true)
    			{
    				c.repaint();
    			}
    		}
    	}
    }
    class Unit implements Runnable
    {
    	public boolean isClicked = false;
    	public boolean isIn = false;
    	public Rectangle2D s;
    	int x;
    	int y;
    	ArrayList<Unit> unitList;
    	public Unit(int x,int y,int w, int h)
    	{
    		s = new Rectangle2D.Float(x,y,w,h);
    		this.x = x;
    		this.y = y;
    		this.unitList = unitList;
    	}
     
    	public void run()
    	{
    		while(true)
    		{
    			try
    			{
    				Thread.sleep(100);
    			}
    			catch(InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    			detect();
    		}
    	}
    	public void mousePressed(MouseEvent e)
    	{
    		GameMove.mouseX = e.getX();
    		GameMove.mouseY = e.getY();
    	}
    	public void mouseClicked(MouseEvent e)
    	{
    		//Sees if mouse is in the square
    		if(s.contains(e.getPoint()))
    		{
    			if(isIn)
    			{
    				if(isClicked == true)
    				{		
    					isClicked = false;
    				}
    				else if(isClicked == false)
    				{
    					isClicked = true;
    				}
    			}	
    			else 
    			{
    				isIn = true;
    			}
    		}
    		else
    		{
    			isIn = false;
    		}
    	}	
     
    	public void detect()
    	{
    		// collision
    		for(Unit u: unitList)
    		{
    			if(u != this && u.getBox().intersects(s))
    			{
    				if(x > u.getX())
    				{
    					x += 3;
    				}
    				if (x < u.getX())
    				{
    					x -= 3;
    				}
    				if(y > u.getY())
    				{
    					y += 3;
    				}
    				if (y < u.getY())
    				{
    					y -= 3;
    				}
    			}
    		}
    	}
     
    	public int getX()
    	{
    		return x;
    	}
    	public int getY()
    	{
    		return y;
    	}
    	public Rectangle2D getBox()
    	{
    		return s;
    	}
    }
    So how would I make this so I can make mulitple squares selectable and moveable? My original idea was that I would have an array of squares and they each would implement runnable so that they would constantly check if they are selected and where to go. Thank you to anyone who replys!
    lil_misfit


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,375
    My Mood
    Mellow
    Thanks
    254
    Thanked 787 Times in 772 Posts

    Default Re: Object not following mouse!?

    My original idea was that I would have an array of squares and they each would implement runnable . . .
    Swing is not thread safe. Implementing Runnable in the design of a Swing app should not be a consideration. If needed, a SwingWorker could be used, but it's not needed to do what you've described.

    Overriding paint() is not how graphics is done (or should be done) in a Swing app. Refer to the Lesson: Performing Custom Painting to see how it should be done.

    A Swing app should be run on the EDT. Step 1 of the above lesson shows how this is done.

    Once you've worked through the lesson and modified your program accordingly, draw multiple squares on a container's graphics context, add a mouse listener that selects as many of the squares as you'd like and then a point to move them to. This can be accomplished much more simply than the approach used in your existing code.

    And a nit, statements like this:

    if (isClicked == true)

    and

    if(isClicked && isIn == false)

    can be simplified to:

    if (isClicked)
    if(isClicked && !isIn) // I wonder if this is what you meant

    Good luck!

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Ada Lovelace (August 3rd, 2014)

  4. #3
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Object not following mouse!?

    Thank you GregBrannon. I will try this and see if this helps.
    lil_misfit

Similar Threads

  1. Get the mouse x , y?
    By GNecro in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 18th, 2014, 01:39 PM
  2. Replies: 2
    Last Post: September 26th, 2013, 05:13 PM
  3. read & write object in object file
    By amirsarem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2013, 12:35 PM
  4. How do I select an object in a JFrame using the mouse?
    By Tr1N1tro in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 6th, 2013, 10:55 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 07:11 AM