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

    Default Couple of Problems

    Having a couple of problems in my code below. The first one is this line in RushFrame class is highlighted as wrong:

    Java Code:
    RushPanel panel_for_game =new RushPanel(board_for_game);
    I know this is wrong because in my RushPanel class I just have RushPanel() but i'm not sure what I need to put in my brackets - any ideas - it could just be me missing something.

    The second error is in my RushPanel in my paint method at the bottom. I am trying to draw a 6x6 grid using lines.

    The for loop which is not commented out seems to be causing problems. When you look at the for loop below that one which is currently commented out - it will happily draw my vertical lines but when I change that to same format as for loop above it doesn't draw anymore.

    Can anyone give me some advice on how to cure this?

    I have only posted my Board, RushPanel, and RushFrame classes. I also have a Car and Exit class but I think they are ok.

    Board Class:

    Java Code:
    package rush;
    
    import java.util.TreeSet;
    import rush.Car;
    
    public class Board
    {    
        public static final int NOOFROWS       = 6;
        public static final int NOOFCOLS       = 6;
        public static final boolean EMPTY      = false;  // The cell is empty.
        public static final boolean OCCUPIED   = true;  // The cell is occupied
    
        private int                         width;
        private int                         height;
        
        private TreeSet<Car> car;
            
        Exit exit;
            
        /** Creates a new instance of Board */
        public Board(int width, int height, Car[] cars, Exit exit) 
        {
            this.width          = width;
            this.height         = height;
            this.exit           = exit;
            car                 = new TreeSet<Car>();
                    
            for(int i = 0; i < cars.length; i++)
            {
                car.add(cars[i]);
            }        
        }
        
        public void setWidth(int width)
        {
            this.width = width;
        }
        
        public int getWidth()
        {
            return width;
        }
        
        public void setHeight(int height)
        {
            this.height = height;
        }
        
        public int getHeight()
        {
            return height;
        }
    }//end Board class
    RushPanel Class:

    Java Code:
    package rush;
    
    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.JPanel;
    
    public class RushPanel extends JPanel
    {
        public Board            myBoard;
        Car[] car ;
    
        private boolean isDraggable  = false;
    
        /** Creates a new instance of RushPanel */
        public RushPanel() {
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
                    // ******Code Here******
                    Point p = e.getPoint();
                    for(int i = 0; i < car.length; i++) {
                        if(car[i].contains(p)) { 
                            e.getX();
                            e.getY();
                            isDraggable = true;
                        }
                    }
                }
                
                @Override    
                public void mouseReleased(MouseEvent e){
                    // ******Code Here******
                    isDraggable = false;
                }
            });
    
            addMouseMotionListener(new MouseMotionAdapter(){
                @Override
                public void mouseDragged(MouseEvent e){
                    // ******Code Here******
                    if(isDraggable)
                    {
                        Point p = e.getPoint();
                    }
                }
            });
        }   
        
        public void paint(Graphics g) {
            
            for (int y = 0; y <= Board.NOOFROWS; y = y + myBoard.getHeight()/Board.NOOFROWS)
            {
                 g.drawLine(0, myBoard.getHeight()/Board.NOOFROWS, myBoard.getWidth(), myBoard.getHeight()/Board.NOOFROWS);
            }
            
            /*for (int x = 0; x <= 300; x = x + 50)
            {
                g.drawLine(x, 0, x, 300);
            }*/
        }
    }
    RushFrame Class:

    Java Code:
    package rush;
    
    import javax.swing.JFrame;
    
    public class RushFrame
    {
        public static void main(String[] args) 
        { 
            Car[] car_set_1 ={
                                new Car(1, 1, 2),
                                new Car(4, 4, 1),
                             };
            
            Exit exit_1=new Exit(0, 3, 3);
            
            Board board_for_game=new Board(300, 300, car_set_1, exit_1);
            RushPanel panel_for_game =new RushPanel(board_for_game);
            
            JFrame f = new JFrame("The Rush Hour Game"); 
            //RushPanel rushPanel = new RushPanel(); // create paint panel
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.getContentPane().add(panel_for_game);
            //f.add(new RushPanel()); 
            f.setSize(350,350);
            f.setVisible(true);
            f.setResizable(false);    
        }
    }
    Last edited by joz_12345; 03-04-2008 at 11:06 PM.

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

    Default

    Ok I now have lines appearing on my panel but only one vertical one and one horizontal line. How do I get more lines to appear so it creates a 6x6 grid. I'm guessing its to do with my for loops in the paint method in my RushPanel.

    What its doing so far:



    Java Code:
    package rush;
    
    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.JPanel;
    
    public class RushPanel extends JPanel
    {
        public Board            myBoard;
        Car[] car ;
    
        private boolean isDraggable  = false;
    
        /** Creates a new instance of RushPanel */
        public RushPanel(Board board) {
            myBoard = board;
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
                    // ******Code Here******
                    Point p = e.getPoint();
                    for(int i = 0; i < car.length; i++) {
                        if(car[i].contains(p)) { 
                            e.getX();
                            e.getY();
                            isDraggable = true;
                        }
                    }
                }
                
                @Override    
                public void mouseReleased(MouseEvent e){
                    // ******Code Here******
                    isDraggable = false;
                }
            });
    
            addMouseMotionListener(new MouseMotionAdapter(){
                @Override
                public void mouseDragged(MouseEvent e){
                    // ******Code Here******
                    if(isDraggable)
                    {
                        Point p = e.getPoint();
                    }
                }
            });
        }   
        
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            for (int y = 0; y <= Board.NOOFROWS; y = y + myBoard.getHeight()/Board.NOOFROWS)
            {
                 g.drawLine(0, myBoard.getHeight()/Board.NOOFROWS, myBoard.getWidth(), myBoard.getHeight()/Board.NOOFROWS);
            }
            
            for (int x = 0; x <= Board.NOOFROWS; x = x + myBoard.getWidth()/Board.NOOFCOLS)
            {
                 g.drawLine(myBoard.getWidth()/Board.NOOFCOLS, 0, myBoard.getWidth()/Board.NOOFCOLS, myBoard.getHeight());
            }
                   
        }
    }
    Last edited by joz_12345; 03-06-2008 at 12:15 AM.

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

    Default

    Java Code:
    Board board_for_game = new Board(300, 300, car_set_1, exit_1);
    RushPanel panel_for_game = new RushPanel(board_for_game);
    
    JFrame f = new JFrame("The Rush Hour Game");
    // "panel_for_game" is a reference to this new RushHour
    // instance you created with the [i]new[/i] operator.
    // Since RushHour extends JPanel then this instance
    // ("panel_for_game") _is_ a JPanel and can be added to
    // your frames contentPane as a component. There is no
    // need to make another one. This instance of RushHour
    // comes with a [i]paint[/i] method override and is ready to go.
    //RushPanel rushPanel = new RushPanel(); // create paint panel
    
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.getContentPane().add(panel_for_game);
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class RushPanelRx extends JPanel {
        Board myBoard;
    
        RushPanelRx(Board board) {
            myBoard = board;
        }
    
        // Overriding this method is easier for custom drawing.
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            double yInc = (double)(myBoard.getHeight()-1)/Board.NOOFROWS;
            for (int row = 0; row <= Board.NOOFROWS; row ++) {
                int y = (int)(row * yInc);
                g.drawLine(0, y, myBoard.getWidth(), y);
            }
            double xInc = (double)(myBoard.getWidth()-1)/Board.NOOFCOLS;
            for (int col = 0; col <= Board.NOOFROWS; col++) {
                int x = (int)(col * xInc);
                g.drawLine(x, 0, x, myBoard.getHeight());
            }
        }
    
        public static void main(String[] args) {
            Board board = new Board(300, 300);
            RushPanelRx rushPanel = new RushPanelRx(board);
            JFrame f = new JFrame("The Rush Hour Game");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.getContentPane().add(rushPanel);
            f.setSize(350, 350);
            f.setVisible(true);    
        }
    }
    
    class Board {
        public static final int NOOFROWS = 6;
        public static final int NOOFCOLS = 6;
        int width;
        int height;
    
        Board(int width, int height) {
            this.width = width;
            this.height = height;
        }
    
        public int getWidth() { return width; }
    
        public int getHeight() { return height; }
    }

Similar Threads

  1. Java assignment - couple methods don't know how to figure out
    By Snowboardmylife in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:52 AM
  2. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 10:06 AM
  3. GUI problems.
    By saytri in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 10:27 PM
  4. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:34 AM

Posting Permissions

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