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: Rushhour - X and Y Positions

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rushhour - X and Y Positions

    Hello Guys!

    I have to make a Rush hour game for my project. The basic idea of Rush hour is to "Unstuck" you car and get it to the exit by moving other vehicles out of the way. My problem is that I want to create multiple vehicles of the same type ( By using a factory ) and have them start at different X or Y positions depending on the vehicle. As an example I will use a "Bus":

    rushhour_example.jpg

    Now, the idea is that the bus can ONLY move in the Y directions and is stuck to it's X position. The problem is, because I use the same factory for the busses they snap to the same X position as soo as I start moving them ( which at the moment is x = 0 ). So what I want them to do is stay at the x position they are at while I a moving the Y direction. Here is my code for the bus: (All my methods, classes etc. are in Dutch, sorry for that)

    package RushHourSpel;
     
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
     
    public class Bus extends SpelFiguren implements MouseMotionListener {
     
    	private int clickedY, clickedX;
     
    	public Bus(int x, int y)
    	{
     
    		this.setSize(lengthBusX, lengthBusY);
    		this.setBackground(Color.BLUE);
    		this.addMouseMotionListener(this);
    		this.setLocation(x, y);
     
    	}
     
     
    	@Override
    	public void mouseDragged(MouseEvent e)
    	{
    		int muisY = e.getY();
    		int verschilY = clickedY - muisY;
    		int busY = this.getY() - verschilY - 75;
    		//int muisX = e.getX();
    		//int verschilX = clickedX - muisX;
    		//int busX = this.getX() - verschilX - 20;
     
    		this.setLocation(LocationBusX, busY);
    		//  Vanaf Hieronder Border Collisions
     
    		if(busY > 350)
    		{
    			setLocation(LocationBusX, 350);
    		}
     
    		if(busY < 0)
    		{
    			setLocation(LocationBusX, 0);
    		}
     
    		// Vanaf Hieronder Collisions met andere spelfiguren.
    		try
    		{
    			if(collisionRodeAuto())
    			{
     
    				this.setLocation(LocationBusX, 80);
    				System.out.println("Collision!");
    			}
     
    			if(collisionAuto())
    			{
     
    				this.setLocation(LocationBusX, 250);
    				System.out.println("Collision!");
    			}
     
     
     
    		}
    		catch(NullPointerException ex)
    		{
     
    		}
    	}
     
     
     
     
     
    	public boolean collisionRodeAuto()
    	{
     
    		return SpeelLevels.rd.getBounds().intersects(this.getBounds());
     
    	}
     
     
    	public boolean collisionAuto()
     
    	{
     
    		return SpeelLevels.auto1.getBounds().intersects(this.getBounds());
     
    	}
     
    	@Override
    	public void mouseMoved(MouseEvent e)
    	{
    	}
     
     
    }

    package RushHourSpel;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JPanel;
     
    public class SpeelLevels extends SpelFiguren implements ActionListener{
     
    		static int BusX;
    		static RodeAuto rd;
    		static Auto auto1;
    		private JPanel speelveld;
    		private int LocationAutoX = 300, LocationAutoY = 400, LocationRdX = 0, LocationRdY = 230;
     
    	public SpeelLevels(){
     
    			this.setSize(500, 500);
    			this.setLayout(null);
     
    			rd = new RodeAuto(20, 20);
    			rd.setLocation(LocationRdX, LocationRdY);
     
    			speelveld = new JPanel();
    			speelveld.setSize(500,500);
    			speelveld.setLayout(null);
     
    			for (int i = 0; i < 1; i++)
    			{
    				BusX = 160 + i * 60;
    				bus2 = new Bus(BusX, 50);
    				bus2.setLocation(BusX, 50);
    				speelveld.add(bus2);
    			}
     
    			for (int i = 0; i < 1; i++)
    			{
    				int BusX = 260 + i * 60;
    				bus1 = new Bus(BusX, 150);
    				bus1.setLocation(BusX, 150);
    				speelveld.add(bus1);
    			}
     
    			this.add(speelveld);
     
     
    			auto1 = new Auto(20, 20);
    			auto1.setLocation(LocationAutoX, LocationAutoY);
     
    			speelveld.add(rd);
     
    			speelveld.add(auto1);
     
    		}
     
    	@Override
    	public void actionPerformed(ActionEvent e)
    	{
    		// TODO Auto-generated method stub
     
    	}
     
     
     
     
     
     
     
     
    }

    package RushHourSpel;
     
    import javax.swing.JPanel;
     
    public class SpelFiguren extends JPanel {
     
    		protected int lengthCarX;
    		protected int lengthCarY;
    		protected int lengthBusX;
    		protected int lengthBusY;
    		protected int LocationBusY, LocationRdX = 0, LocationRdY = 230, LocationBusX2 = 200, LocationBusY2;
    		protected static int LocationBusX;
    		protected static Bus bus1;
    		static Bus bus2;
     
    		public SpelFiguren(){
     
    			lengthCarX = 150;
    			lengthCarY = 60;
    			lengthBusX = 60;
    			lengthBusY = 230;
     
     
     
    		}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,484
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Rushhour - X and Y Positions

    I don't really understand your question or problem. What does using a factory have to do with snapping all vehicles created to x = 0? If you don't want that to happen, don't set x = 0 in your factory or any of its methods.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rushhour - X and Y Positions

    I made a video with the problem showing, hope this helps to understand it better. So the left bus needs to stay on it's X position, but it snap to the other bus's X position. now I know where the problem is coming from, but I don't know how to solve it without making a entire different class for the second bus.


Similar Threads

  1. Numerous Java Contract Positions based in London, UK
    By daniel2500 in forum Paid Java Projects
    Replies: 0
    Last Post: January 13th, 2014, 10:39 AM
  2. [Swing] JPanel is being added twice to my JFrame in different positions?
    By Benner in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 12th, 2013, 07:59 PM
  3. Creating the game Rushhour.
    By CeB in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 07:38 AM
  4. Help with checking JButton positions please
    By sim18 in forum Java Theory & Questions
    Replies: 60
    Last Post: December 4th, 2012, 09:39 AM