Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-16-2008, 04:27 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
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:
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"); } } }
Board
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; } }
GUI
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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 08:41 PM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
How wide are your rows and columns in pixels? Because the way you called the constructor in your Board class...
Code:
Car_1 car1 = new Car_1(1, 1, 0); Car_1 car2 = new Car_1(1, 2, 1); etc.
you have each row and column set to 1 pixel, which is really small.
You should space out your cars for each row. Like this if each row was 50 pixels wide.
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);
Or if you can't that or collisions to work, you could leave the constructor calls as they are and make your board out of an array, but that would require making more pictures (breaking up 100x50 pictures into two 50x50 pictures for each car and another picture of each square for each direction it would face) then you can display each square one by one.

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-16-2008, 08:50 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-16-2008, 09:48 PM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-16-2008, 09:58 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-16-2008, 10:30 PM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
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)
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.
And then you need to display the array, making sure each square is 50 pixels apart (which will display them as being right next to each other).

An example for selecting the right parts of your array to change would be
Code:
coordinate[0] = image[0] coordinate[1] = image[1] coordinate[2] = image[2] coordinate[3] = image[0]
That's a few ideas to help you.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-17-2008, 12:18 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Surely there is a way of doing it keeping images the way they are?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-18-2008, 03:58 AM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
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.
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 */ }
This will draw car1 at 0, 0 (column 1, minus 1 = 0, times 50 = 0) and width and height just depend on the image used. So for collision checking you would need to know the orient&length number, so you know width and height.
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; }
That's a start on doing it without arrays (except used, which is very useful)

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRC check program 1 error javakid9000 New To Java 1 03-19-2008 06:04 AM
Check if a web page exists or not Java Tip Java Tips 0 03-02-2008 08:24 PM
Spell check feature ravian Advanced Java 2 12-27-2007 10:28 AM
spell check kazitula Java Applets 2 12-20-2007 12:37 PM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM


All times are GMT +3. The time now is 12:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org