Having a couple of problems in my code below. The first one is this line in RushFrame class is highlighted as wrong:
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:
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:
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:
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);
}
}