Results 1 to 9 of 9
  1. #1
    DrCooksAlot is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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));}
        }}
    I know I know!
    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

  2. #2
    DrCooksAlot is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help tile TimeScrambler game

    BUMP? ( sorry if you're not allowed to do this i checked in the rules didnt' see any negatives)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  4. #4
    DrCooksAlot is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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){}
    }
    Just updating my code! any more suggestions? sorry to be annoying!
    Last edited by DrCooksAlot; 01-25-2012 at 03:13 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  6. #6
    DrCooksAlot is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help tile TimeScrambler game

    Board.java:41: cannot find symbol
    symbol : variable easy
    Where is the variable: easy defined? The compiler can not find a definition for it.

    Should easy be in "s to make it a String?

    You should use the equals() method to compare Strings, not the == operator.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DrCooksAlot is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help tile TimeScrambler game

    Yeah I was definitely way too over-ambitious in picking my project, I had thought I had a pretty good grasp of the language so far, but I hadn't actually written any code ia month, which can do wonders to my syntax :P Sorry for the stupid questions and such.

Similar Threads

  1. Simple Tile Game Question
    By sgthale in forum Java Applets
    Replies: 9
    Last Post: 06-17-2011, 12:20 PM
  2. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  3. Concept of Tile map via database
    By GrInd3r in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 06:18 PM
  4. Replies: 3
    Last Post: 11-06-2010, 01:33 PM
  5. Replies: 2
    Last Post: 08-05-2010, 05:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •