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);
}
}
public class RhTest 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 RhTest() {
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
}
}
public class Main {
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 RhTest());
f.setSize(317,337);
f.setVisible(true);
}
}