Results 1 to 16 of 16

Thread: help

  1. #1
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default 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



    Java 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); 
       } 
    }
    Java 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 
    
       } 
    }
    Java 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); 
       } 
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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:
    Java 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); 
        } 
    }

  3. #3
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    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?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Yes. You could try something like this:
    Java Code:
    // Update coordinates.
    Car car = cars[selectedIndex];
    if(car.getWidth() > car.getHeight())
        car.setX(x); 
    else
        car.setY(y);

  5. #5
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    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

  6. #6
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    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

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    r.setLocation(x, y);
    Rectangle bounds = getBounds();
    if(!bounds.contains(r))
        return true;

  8. #8
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Where would that go - in the moveCar method?

    Or would I make a new method since void cannot return?

  9. #9
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    What the hell?

  10. #10
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    What the hell?
    Spam posts are removed and the user is warned.

  11. #11
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Thank you JavaBean

  12. #12
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    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:

    Java 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;
            [B]this.orientation = o;[/B]
            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;
        }
        
        [B]public void SetOrientation(int orientation){
            this.orientation = orientation;
        }
        
        public int getOrientation(){
            return orientation;
        }[/B]
        
        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);
        }
    }

  13. #13
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    I'm guessing in my panel class I am going to have to have something like:

    Java 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:

    Java 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 01:35 AM.

  14. #14
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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:
    Java 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);
        }
    }

  15. #15
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    What is:

    final int PAD = 20;
    for?

  16. #16
    joz_12345 is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

Posting Permissions

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