Results 1 to 8 of 8
Thread: Can someone check my code
- 02-16-2008, 04:27 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
Can someone check my code
I'm making the rush hour game using a 6x6 grid so that cars will snap to each square when being moved. I have 6 images loaded in my photobucket account - a 2 square vertical, 3 square vertical, target vertical, 2 square horizontal, 3 square horizontal and target horizontal. Each of these are called as and when they are needed.
I have 3 classes at the moment - Car, Board and GUI.
Can someone check my code so far so that I know what i'm doing is correct and any pointers/advice would be appreciated as I'm a bit stuck now as to what to do next.
I still need my collision detection, and mouse listeners and a couple of other bits.
Car:
Java Code:package rush; import java.awt.Image; import java.awt.Toolkit; public class Car_1{ public static final int HORIZONTALTWOBLOCK = 0; public static final int HORIZONTALTHREEBLOCK = 1; public static final int HORIZONTALTARGET = 2; public static final int VERTICALTWOBLOCK = 3; public static final int VERTICALTHREEBLOCK = 4; public static final int VERTICALTARGET = 5; private int xPos; private int yPos; private int orientandlength; public Car_1(int x, int y, int oandl) { this.xPos = x; this.yPos = y; this.orientandlength = oandl; } public void setX(int x) { this.xPos = x; } public int getX() { return xPos; } public void setY(int y) { this.yPos = y; } public int getY() { return yPos; } public void setOrientandLength(int oandl) { if(orientandlength == HORIZONTALTWOBLOCK) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/2BlockCarHorizontal.jpg"); } else if (orientandlength == HORIZONTALTHREEBLOCK) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/3BlockCarHorizontal.jpg"); } else if (orientandlength == HORIZONTALTARGET) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/CarTargetHorizontal.jpg"); } else if (orientandlength == VERTICALTWOBLOCK) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/2BlockCarVertical.jpg"); } else if (orientandlength == VERTICALTHREEBLOCK) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/3BlockCarVertical.jpg"); } else if (orientandlength == VERTICALTARGET) { Image image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage("http://i126.photobucket.com/albums/p85/geebs2006/CarTargetVertical.jpg"); } } }
Java Code:package rush; import java.awt.Color; /** * * @author Steven Gibbs */ public class Board extends Car_1 { public static final int NOOFROWS = 6; public static final int NOOFCOLS = 6; private int theExit; // Car Object, Name of Object, Row Number, Column Number, Image Required Car_1 targetCar = new Car_1(2, 3, 0); Car_1 car1 = new Car_1(1, 1, 0); Car_1 car2 = new Car_1(1, 2, 1); Car_1 car3 = new Car_1(1, 5, 1); Car_1 car4 = new Car_1(4, 2, 1); Car_1 car5 = new Car_1(3, 6, 0); Car_1 car6 = new Car_1(6, 1, 1); Car_1 car7 = new Car_1(5, 5, 0); /** Creates a new instance of Board */ public Board(int exit, int x, int y, int oandl) { super(x, y, oandl); this.theExit = exit; } public void settheExit(int theExit) { this.theExit = theExit; } public int gettheExit() { return theExit; } }
Java Code:package rush; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JPanel; /** * * @author Steven Gibbs */ public class InteractiveGUI extends Board{ /** Creates a new instance of InteractiveGUI */ public InteractiveGUI(int exit, int x, int y, int oandl) { super (exit, x, y, oandl); JPanel jp = new JPanel(); } }
- 02-16-2008, 08:41 PM #2
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
How wide are your rows and columns in pixels? Because the way you called the constructor in your Board class...
Java Code:Car_1 car1 = new Car_1(1, 1, 0); Car_1 car2 = new Car_1(1, 2, 1); etc.
You should space out your cars for each row. Like this if each row was 50 pixels wide.
Java Code:Car_1 targetCar = new Car_1(49, 99, 0); Car_1 car1 = new Car_1(0, 0, 0); Car_1 car2 = new Car_1(0, 49, 1);
Just a few things to think about, I hope that everything turns out alright.Last edited by Bluefox815; 02-16-2008 at 08:44 PM.
- 02-16-2008, 08:50 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
Thanks for reply
Basically where I have specified for each car something like Car_1 car1 = new Car_1(1, 1, 0);
the first number is the column number and the second number is the row number, and then the third relates to which picture needs to be displayed at that row and column coordinate.
- 02-16-2008, 09:48 PM #4
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
Yes, I know, but how are you going to display that? I would use arrays if you want to use that idea, and maybe start at 0 instead of 1 (makes it easier to work with arrays). If you need help with displaying, let me know what you want to do.
Last edited by Bluefox815; 02-16-2008 at 09:52 PM.
- 02-16-2008, 09:58 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
yea any help you can give would be great as i'm a little stuck with it. Haven't done java in 2 years and just getting back into it now so i'm impressed with stage i'm at now lol.
- 02-16-2008, 10:30 PM #6
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
Alright, well you're going to need to come up with a method that will take the cars and line them up correctly, right now you shouldn't be thinking about collisions or anything like that. You should take your pictures and split them to 50x50 pics so that you can instead display a square where you need it. An example would be this. 1 = Front car piece, 2 = back car piece (horizontal)
0 0 0
1 2 0
0 0 0
And each number is a square which can be represented by an array that holds the images, then you just select the image you want to display at each square. (You will also need a multi-dimensional array for the plotting coordinates)
Java Code:image[0] = an empty square image[1] = horizontal front car piece image[2] = horizontal back car piece image[3] = horizontal mid car piece (for a 3-block car) image[4] = vertical front car piece etc.
An example for selecting the right parts of your array to change would be
Java Code:coordinate[0] = image[0] coordinate[1] = image[1] coordinate[2] = image[2] coordinate[3] = image[0]
- 02-17-2008, 12:18 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
Surely there is a way of doing it keeping images the way they are?
- 02-18-2008, 03:58 AM #8
Member
- Join Date
- Feb 2008
- Location
- Oregon, USA
- Posts
- 49
- Rep Power
- 0
Yes, that's with collisions, that was just an example of using arrays. You could check to see if a car is at a certain position, which would be a bit difficult. You would have to know the position of each car, how wide it is (if you need to check for the right side of it) how tall (for bottom).
You will probably want a method in your GUI class that takes the numbers for row and column and converts them to pixels. You could multiply the row or column number by 50 and subtract 1 (that IS if row number = 0, it comes out as 0, and 1 as 50, so a little added math is used... yay).
I noticed your setOrientandLength method in Car_1 is pretty worthless, as it doesn't set any field to an image, or return an image, so it does nothing. I would make image a field (member variable) and call the method inside your constructor (which requires it be static). Then add a getImage() method.
Below is code that will work with the above.
Java Code:public void paint(Graphics g) { g.drawImage(car1.getImage(), (car1.getX() - 1) * 50, (car1.getY() - 1) * 50, this); /* the parenthesis used for getting the X and Y coordinates decide which math is done first, and are important */ }
Java Code:public void paint(Graphics g) { if (checkCollision(car1.getX(), car1.getY(), car1.getOrientandLength)) { /* getOrientandLength will need to be created, and should return an int */ g.drawImage(car1.getImage(), (car1.getX() - 1) * 50, (car1.getY() - 1) * 50, this); } // other drawing continues } private static boolean checkCollision(int x, int y, int orientAndLength) { /* let's say that 'used' is a boolean field/member variable (boolean[] used = new boolean[36]) */ switch (orientAndLength) { case 0: // for horizontal 2 block case 2: // for horizontal target, which is the same size as 2 block // assuming the number of total columns is 6 if (used[(y * 6) + x] == false && used[(y * 6) + x + 1] == false) { used[(y * 6) + x] = true; used[(y * 6) + x + 1)] = true; } else { return false; } break; case 1: // for horizontal 3 block if (used[(y * 6) + x] == false && used[(y * 6) + x + 1)] == false && used[(y * 6) + x + 2)] == false) { used[(y * 6) + x] = true; used[(y * 6) + x + 1)] = true; used[(y * 6) + x + 2)] = true; } else { return false; } break; case 3: if (used[(y * 6) + x] == false && used[((y + 1) * 6) + x] == false) { used[(y * 6) + x] = true; used[((y + 1) * 6) + x)] = true; } else { return false } break; // etc. } // end switch statement return true; }
Also, when you get everything drawing itself correctly, I can help you with making the mouse work, but be advised that I work with applets and don't really know ANYTHING about JFrame, or Swing and what not, I still need to learn that.
And one last thing, I noticed your classes extend each other, they don't need to do that at all, so you can take that out (and also don't worry about import, because they are in the same package)Last edited by Bluefox815; 02-18-2008 at 04:29 AM.
Similar Threads
-
CRC check program 1 error
By javakid9000 in forum New To JavaReplies: 1Last Post: 03-19-2008, 06:04 AM -
Check if a web page exists or not
By Java Tip in forum Java TipReplies: 0Last Post: 03-02-2008, 08:24 PM -
Spell check feature
By ravian in forum Advanced JavaReplies: 2Last Post: 12-27-2007, 10:28 AM -
spell check
By kazitula in forum Java AppletsReplies: 2Last Post: 12-20-2007, 12:37 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 04:52 PM
Bookmarks