Results 1 to 9 of 9
Thread: Help tile TimeScrambler game
- 01-23-2012, 06:55 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Help tile TimeScrambler game
Hey guys,
This is my first post on java-forums, I have a bunch of questions that may seem kind of stupid, try to bare with me :P. I'm taking an AP Computer Science course at my high school and for my final project I'm making a GUI tile-scrambler game just like this one .
So I don't actually have much code yet, which I know is not good, but the way I want to do it is by creating a Board that extends JFrame, that will have a component layout 4x3 3x4 or 3x3 . I want to use GridLayout to fill it with my class Tile (an extension of JButton). Each Tile will have a cut up version of an imported image except for the 16th one which will be blank. For the tile switching I was planning on creating a event-listener that would (when you click a tile) check to see if it's near the 16th tile and, then switch places with it if it was. Each tile would store the number of the picture it got, so when the button positions and the picture numbers match up the game would be won.
So as I said I'm kind of a newbie, but I'm trying! Does this approach seem doable? I had some more specific questions too.
-What packages should I be importing in the beginning?
-Can my tiles be re-arranged on the board during the game? or should I just make it so the images switch(if that makes sense)?
-I want to have a difficulty option for the game, so I was trying to create a initiation for the board that relies on a variable. What is the best way to do this?
Here is my futile attempt.
Java Code:import javax.swing.*; import java.awt.*; public class Board extends JFrame{ public void init(int width , int height ){ setLayout( new GridLayout( height, width )); for ( x = 0; x < (width * height) ; x++){ add( new Tile(x));} }}
Awful!
Anyway i'd really appreciate any sort of help, I'm not asking for someone to do it for me, I've just done alot of online research and can't seem to get my foot in the right direction
- 01-24-2012, 11:38 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Help tile TimeScrambler game
BUMP? ( sorry if you're not allowed to do this i checked in the rules didnt' see any negatives)
- 01-25-2012, 12:00 AM #3
Re: Help tile TimeScrambler game
The import statements will depend on what packages you are using. Add them as needed.
I don't know how to move components around once they have been laid out by GridLayout. Swapping images might be easier.
- 01-25-2012, 02:51 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Help tile TimeScrambler game
Thanks!
I think I am definitely missing some packages because I keep getting this error message
public static void Main (String[] args ) {
^
Main.java:5: class, interface, or enum expected
}
^
2 errors
Java Code:public static void Main (String[] args ) { new board(easy); }
Java Code:import javax.swing.*; import java.awt.*; public class Board { Tile[][] tileset; JFrame frame = new JFrame(); public Board( String difficulty){ int width = 3; int height = 3; if ( difficulty == "easy"){ height = 3; width = 3;} else { if ( difficulty == "medium" ){ width = 3; height = 4; } else{ height = 4; width = 4; }} frame.setLayout(new GridLayout ( width, height)); tileset = new Tile[width][height]; for (int i = 0; i < width; i++){ for (int j = 0; j < height ; j++){ tileset[i][j] = new Tile ( " ( "+i+", " +j+ ")"); frame.add(tileset[i][j]); }} frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main (String[] args){ new Board(easy); }}
Java Code:ort javax.swing.*; import java.awt.*; public class Tile extends JButton { public Tile(String text){} }
Last edited by DrCooksAlot; 01-25-2012 at 03:13 AM.
- 01-25-2012, 03:01 AM #5
Re: Help tile TimeScrambler game
The Main.java file has no class statement.
To execute the Board class, you need to add a main() method to it.
- 01-25-2012, 03:14 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Help tile TimeScrambler game
Okay, so I added the same main method, and now I get this instead ( I edited my code above to reflect my changes)
Board.java:41: cannot find symbol
symbol : variable easy
location: class Board
new Board(easy);
^
1 error
Confusion Continues
- 01-25-2012, 03:36 AM #7
Re: Help tile TimeScrambler game
Board.java:41: cannot find symbol
symbol : variable easy
Should easy be in "s to make it a String?
You should use the equals() method to compare Strings, not the == operator.
- 01-25-2012, 06:47 AM #8
Re: Help tile TimeScrambler game
Swing components should be constructed and their methods called on the EDT, not the main thread.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Why are you jumping ahead to GUIs when you still haven't learned the fundamentals of writing Java code? Learn to walk before you try to run.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 01-25-2012, 04:12 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Simple Tile Game Question
By sgthale in forum Java AppletsReplies: 9Last Post: 06-17-2011, 12:20 PM -
Complete Game Engine for beginner and intermediate game programmers
By rdjava in forum Java GamingReplies: 1Last Post: 06-02-2011, 10:29 AM -
Concept of Tile map via database
By GrInd3r in forum New To JavaReplies: 0Last Post: 12-08-2010, 06:18 PM -
I am having a compile tile error. please help.Exception in thread "main" java.lang.Ar
By nicholil in forum New To JavaReplies: 3Last Post: 11-06-2010, 01:33 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 05:49 AM
Bookmarks