Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2007, 06:37 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
help
am making the Rush Hour Game. I have all my rectangles (cars) set up and they can be dragged about on the board horizontally or vertically on the board depending on their orientation. My problem which I'm having now is setting up collision detection.

Am I right in thinking that because i'm using x and y coordinates to define where each car is placed on the board that i can use the intersects method ie.

public boolean intersects(Rectangle rect)

or am I going down the wrong path here?

If its the right path then I have 8 cars so I guess I would use a for loop to make sure that car selected to be moved does not collide with any other car on the board - is this correct?

Any tips/pointers you can give me would be much appreciated. Below is my three classes that i have just now to get the game to stage of setting up the collision detection.

Thanks



Code:
/* * RushHour.java * * Created on 26 November 2007, 18:50 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package rushhourgamefinal; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class RushHour { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame f = new JFrame("The Rush Hour Game"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new RushHourPanel()); f.setSize(317,337); f.setVisible(true); } }
Code:
/* * RushHourPanel.java * * Created on 26 November 2007, 18:52 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package rushhourgamefinal; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.BorderFactory; import javax.swing.JPanel; public class RushHourPanel extends JPanel { // Very easy to add othe cars to the board, just specify position, size and colour // Double array to enter x position, y position, width and height int[][] dims = { // x y w h { 50, 100, 100, 50 }, { 0, 0, 100, 50 }, { 250, 0, 50, 150 }, { 150, 50, 50, 150 }, { 0, 50, 50, 150 }, { 0, 200, 50, 100 }, { 200, 200, 100, 50 }, { 100, 250, 150, 50 } }; // Colours of the cars are entered here Color[] colors = { Color.red, Color.green, Color.yellow, Color.blue, Color.pink, Color.orange, Color.cyan, Color.black }; Car[] cars; int selectedIndex; boolean drag = false; // added here boolean mouseInside, collide; // end of add final int OFFSET = 1; public RushHourPanel() { initCars(); setBorder(BorderFactory.createLineBorder(Color.black)); addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ Point p = e.getPoint(); // Check to see if user has clicked on a car for(int j = 0; j < cars.length; j++) { if(cars[j].contains(p)) { selectedIndex = j; drag = true; break; } } } public void mouseReleased(MouseEvent e) { drag = false; } }); addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ if(drag) { moveCar(e.getX(),e.getY()); } } }); } private void moveCar(int x, int y){ final int CURR_X = cars[selectedIndex].getX(); final int CURR_Y = cars[selectedIndex].getY(); final int CURR_W = cars[selectedIndex].getWidth(); final int CURR_H = cars[selectedIndex].getHeight(); final int OFFSET = 1; if ((CURR_X!=x) || (CURR_Y!=y)) { // The car is moving, repaint background // over the old car location. repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET); // Update coordinates. if (CURR_W > CURR_H) { cars[selectedIndex].setX(x); } if (CURR_H > CURR_W) { cars[selectedIndex].setY(y); } // Repaint the car at the new location. repaint(cars[selectedIndex].getX(), cars[selectedIndex].getY(), cars[selectedIndex].getWidth()+OFFSET, cars[selectedIndex].getHeight()+OFFSET); } } private void initCars() { cars = new Car[colors.length]; for(int j = 0; j < cars.length; j++) { int x = dims[j][0]; int y = dims[j][1]; int w = dims[j][2]; int h = dims[j][3]; cars[j] = new Car(x, y, w, h, colors[j]); } } public Dimension getPreferredSize() { return new Dimension(300,300); } public void paintComponent(Graphics g) { super.paintComponent(g); for(int j = 0; j < cars.length; j++) cars[j].paintCar(g); // added here if(collide) g.drawString("Collision!", 10, 20); //end of add } }
Code:
/* * Car.java * * Created on 26 November 2007, 18:55 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package rushhourgamefinal; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; class Car { private int xPos; private int yPos; private int width; private int height; Color color; Car(int x, int y, int w, int h, Color color) { this.xPos = x; this.yPos = y; this.width = w; this.height = h; this.color = color; } public void setX(int xPos){ this.xPos = xPos; } public int getX(){ return xPos; } public void setY(int yPos){ this.yPos = yPos; } public int getY(){ return yPos; } public int getWidth(){ return width; } public int getHeight(){ return height; } public boolean contains(Point p) { return new Rectangle(xPos, yPos, width, height).contains(p); } public void paintCar(Graphics g){ g.setColor(color); g.fillRect(xPos,yPos,width,height); g.setColor(Color.BLACK); g.drawRect(xPos,yPos,width,height); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-04-2007, 01:21 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,019
hardwired is on a distinguished road
Am I right in thinking that because i'm using x and y coordinates to define where each car is placed on the board that i can use the intersects method ie.
Yes.
or am I going down the wrong path here?
No.
If its the right path then I have 8 cars so I guess I would use a for loop to make sure that car selected to be moved does not collide with any other car on the board - is this correct?
Yes.
Any tips/pointers you can give me would be much appreciated.
Many changes:
Code:
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseMotionAdapter; public class RushHourRx { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame f = new JFrame("The Rush Hour Game"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new RushHourPanelRx()); // f.setSize(317,337); f.pack(); f.setVisible(true); } } class RushHourPanelRx extends JPanel { int[][] dims = { // x y w h { 55, 105, 100, 50 }, { 0, 0, 100, 50 }, { 250, 0, 50, 150 }, { 160, 55, 50, 150 }, { 0, 55, 50, 150 }, { 0, 210, 50, 100 }, { 205, 210, 100, 50 }, { 110, 265, 150, 50 } }; Color[] colors = { Color.red, Color.green, Color.yellow, Color.blue, Color.pink, Color.orange, Color.cyan, Color.black }; Car[] cars; int selectedIndex; Point offset = new Point(); boolean drag = false; final int OFFSET = 1; public RushHourPanelRx() { initCars(); setBorder(BorderFactory.createLineBorder(Color.black)); addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ Point p = e.getPoint(); // Check to see if user has clicked on a car for(int j = 0; j < cars.length; j++) { if(cars[j].contains(p)) { selectedIndex = j; offset.x = p.x - cars[j].getX(); offset.y = p.y - cars[j].getY(); drag = true; break; } } } public void mouseReleased(MouseEvent e) { drag = false; } }); addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ if(drag) { Point p = e.getPoint(); int x = p.x - offset.x; int y = p.y - offset.y; if(!willCollide(x, y)) moveCar(x, y); } } }); } private boolean willCollide(int x, int y) { Rectangle r = cars[selectedIndex].getBounds(); r.setLocation(x, y); for(int j = 0; j < cars.length; j++) { if(j == selectedIndex) continue; if(r.intersects(cars[j].getBounds())) return true; } return false; } private void moveCar(int x, int y){ final int CURR_X = cars[selectedIndex].getX(); final int CURR_Y = cars[selectedIndex].getY(); final int CURR_W = cars[selectedIndex].getWidth(); final int CURR_H = cars[selectedIndex].getHeight(); // final int OFFSET = 1; if ((CURR_X!=x) || (CURR_Y!=y)) { // The car is moving, repaint background // over the old car location. repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET); // Update coordinates. cars[selectedIndex].setX(x); //redCar.setY(y); // Repaint the car at the new location. repaint(cars[selectedIndex].getX(), cars[selectedIndex].getY(), cars[selectedIndex].getWidth()+OFFSET, cars[selectedIndex].getHeight()+OFFSET); } } private void initCars() { cars = new Car[colors.length]; for(int j = 0; j < cars.length; j++) { int x = dims[j][0]; int y = dims[j][1]; int w = dims[j][2]; int h = dims[j][3]; cars[j] = new Car(x, y, w, h, colors[j]); } } public Dimension getPreferredSize() { return new Dimension(350,350); } public void paintComponent(Graphics g) { super.paintComponent(g); for(int j = 0; j < cars.length; j++) cars[j].paintCar(g); } } class Car { private int xPos; private int yPos; private int width; private int height; Color color; Car(int x, int y, int w, int h, Color color) { xPos = x; yPos = y; width = w; height = h; this.color = color; } public void setX(int xPos){ this.xPos = xPos; } public int getX(){ return xPos; } public void setY(int yPos){ this.yPos = yPos; } public int getY(){ return yPos; } public int getWidth(){ return width; } public int getHeight(){ return height; } public boolean contains(Point p) { return getBounds().contains(p); } public Rectangle getBounds() { return new Rectangle(xPos, yPos, width, height); } public void paintCar(Graphics g){ g.setColor(color); g.fillRect(xPos,yPos,width,height); g.setColor(Color.BLACK); g.drawRect(xPos,yPos,width,height); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-09-2007, 02:53 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
thanks for that - i have had a look at that code - how would you set it though so that cars with a short width and long height can only be moved vertically and ones with a long width and short height can only be moved horizontally - is that something i need to setup in the "moveCar" method?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-09-2007, 06:04 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,019
hardwired is on a distinguished road
Yes. You could try something like this:
Code:
// Update coordinates. Car car = cars[selectedIndex]; if(car.getWidth() > car.getHeight()) car.setX(x); else car.setY(y);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-09-2007, 03:06 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Hmmm... yea I hd tried that and somthing similare but seems to have no effect on it so it mut be slsewhere that needs changed
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-09-2007, 04:18 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
its ok I got it sorted - thanks. Just looking at how to stp the cars being able to be dragged off the JPanel now. thanks for your help
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-09-2007, 05:47 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,019
hardwired is on a distinguished road
Code:
r.setLocation(x, y); Rectangle bounds = getBounds(); if(!bounds.contains(r)) return true;
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-10-2007, 01:05 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Where would that go - in the moveCar method?

Or would I make a new method since void cannot return?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-13-2007, 04:30 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
What the hell?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-13-2007, 06:49 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
What the hell?
Spam posts are removed and the user is warned.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-13-2007, 12:24 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Thank you JavaBean
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-13-2007, 11:03 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Ok after a meeting with my supervisor we have decided it would be better to code it so that it uses a grid ie. the 6x6 grid with the rows numbered 1-6 or 0-5 and same with the columns ON A 300X300 JPANEL (each square therefore would be 50). Cars will then be placed on the board in the format (1,2) or 4,3) etc instead of having to use coordinates of 0-300.

I believe that its not too much work to convert what I have (similar to post 2 of this thread with a few changes here and there) to this format. I have started to convert my Car class first of all to get that set up. Could someone look over it and see if i'm on the right path (The orientation bits further down the class I am not sure about). My plan after is to use an arraylist to store the cars.

My Car class:

Code:
import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; class Car extends Rectangle{ private int xPos; private int yPos; private int width; private int height; private int orientation; Color color; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; Car(int x, int y, int w, int h, int o, Color color) { this.xPos = x; this.yPos = y; this.width = w; this.height = h; this.orientation = o; this.color = color; } public void setX(int xPos){ this.xPos = xPos; } public int getIntX(){ return xPos; } public void setY(int yPos){ this.yPos = yPos; } public int getIntY(){ return yPos; } public int getIntWidth(){ return width; } public int getIntHeight(){ return height; } public void SetOrientation(int orientation){ this.orientation = orientation; } public int getOrientation(){ return orientation; } public boolean contains(Point p) { return new Rectangle(xPos, yPos, width, height).contains(p); } public void paintCar(Graphics g){ g.setColor(color); g.fillRect(xPos,yPos,width,height); g.setColor(Color.BLACK); g.drawRect(xPos,yPos,width,height); } }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-18-2007, 02:29 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
I'm guessing in my panel class I am going to have to have something like:

Code:
private static final int ROWS = 6; private static final int COLS = 6; private static final int SQUARE_SIZE = 50; //50 pixels per square - 36 in total
And then to create board size i could have:

Code:
private static final int WIDTH = COLS*SQUARE_SIZE; private static final int HEIGHT = ROWS*SQUARE_SIZE;
would i be on the right track with this?

Last edited by joz_12345 : 12-18-2007 at 02:35 AM.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-18-2007, 02:55 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,019
hardwired is on a distinguished road
Yes. You can set the preferred size of your JPanel to the WIDTH and HEIGHT you have calculated and draw your grid lines every SQUARE_SIZE apart. Then call pack on the JFrame which will then (try to) display the JPanel at its preferred size.
Another idea is to draw the grid so that it fills the component, ie, make it resizable, like this:
Code:
import java.awt.*; import javax.swing.*; public class ResizableGrid extends JPanel { private static final int ROWS = 6; private static final int COLS = 6; final int PAD = 20; protected void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); int dx = (w - 2*PAD)/COLS; int dy = (h - 2*PAD)/ROWS; for(int j = 0; j <= ROWS; j++) { int y = PAD + j*dy; g.drawLine(PAD, y, w-PAD, y); } for(int j = 0; j <= COLS; j++) { int x = PAD + j*dx; g.drawLine(x, PAD, x, h-PAD); } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new ResizableGrid()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 12-18-2007, 03:09 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
What is:

Quote:
final int PAD = 20;
for?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 12-18-2007, 03:11 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
its ok - worked it out - just me being stupid lol
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 07:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org