views:

29

answers:

0

Ok so I've written my own version of game which should look like this :

http://img199.imageshack.us/img199/6859/lab9a.jpg

but mine looks like that :

http://img444.imageshack.us/img444/7671/98921674.jpg

How can I fix this ? Is there a way to do the layout completely differently ?

Here's the code :

Main.java :

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;

public class Main {  
    public static void main(String[] args){

        final JFrame f = new JFrame("Ladder Game");

        Scanner sc = new Scanner(System.in);
        System.out.println("Creating game data...");
        System.out.println("Height: ");

        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }

        final int height = sc.nextInt();
        Grid[]game = new Grid[height];

        for(int L = 0; L < height; L++){
            Grid row = null;
            int i = L+1;
            String s;

            do {
                System.out.println("Length "+i+", please!");
                s = sc.next();
            } while (s.length() != i);

            Element[] line = new Element[s.length()];
            Element single = null;
            String[] temp = null;
            String[] temp2 = new String[s.length()];

            temp = s.split("");
            for( int j = temp2.length; j>0; j--){
                temp2[j-1] = temp[j];
            }

            for (int k = 0 ; k < temp2.length ; k++) {
                if( k == 0 ){
                    single = new Element(temp2[k], 2);
                }
                else{
                    single = new Element(temp2[k], 1);
                }
                line[k] = single;
            }

            row = new Grid(line);
            game[L] = row;
        }

//############################################
//THE GAME STARTS HERE
//############################################
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setBackground(Color.ORANGE);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        for(int i = 0; i < game.length; i++){
            panel.add(game[i].create());
        }

        f.setContentPane(panel);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);


        boolean end = false;
        boolean word = false;
        String tekst;

        while( !end ){

            while( !word ){
                tekst = JOptionPane.showInputDialog("Input word: ");
                for(int i = 0; i< game.length; i++){
                    if(game[i].equalLength(tekst)){
                        if(game[i].equalValue(tekst)){
                            word = true;
                            for(int j = 0; j< game.length; j++){
                                game[i].repaint();
                            }
                        }
                    }
                }
            }
            word = false;

            for(int i = 0; i < game.length; i++){
                if(game[i].solved()){
                    end = true;
                }
                else {
                    end = false;
                }
            }
        }
    }
}

Grid.java

import javax.swing.*;
import java.awt.*;

public class Grid extends JPanel{
    private Element[]e;
    private Grid[]g;


    public Grid(){}

    public Grid( Element[]elements ){

        e = new Element[elements.length];
        for(int i=0; i< e.length; i++){
            e[i] = elements[i];
        }
    }

    public Grid(Grid[]grid){

        g = new Grid[grid.length];
        for(int i=0; i<g.length; i++){
            g[i] = grid[i];
        }

        Dimension d = new Dimension(600, 600);
        setMinimumSize(d);
        setPreferredSize(new Dimension(d));
        setMaximumSize(d);
    }


    public JPanel create(){

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

        for(int j = 0; j < e.length; j++){
            panel.add(e[j].paint());
        }

        return panel;
    }

    @Override
    public void repaint(){       
    }

    public boolean equalLength(String s){

        int len = s.length();
        boolean equal =  false;

        for(int j = 0; j < e.length; j++){
            if(e.length == len){
                equal = true;
            }           
        }
        return equal;
    }

    public boolean equalValue(String s){
        int len = s.length();
        boolean equal =  false;
        String[] temp = null;
        String[] temp2 = new String[len];

        temp = s.split("");
        for( int j = len; j>0; j--){
            temp2[j-1] = temp[j];
        }
            for(int j = 0; j < e.length; j++){
                if( e[j].letter().equals(temp2[j]) ){
                    equal = true;
                }
                else {
                    equal = false;
                }
            }
            if(equal){
                for(int i = 0; i < e.length; i++){
                    e[i].changeState(3);
                }
            }
        return equal;
    }

    public boolean solved(){
        boolean solved = false;

        for(int j = 0; j < e.length; j++){
            if(e[j].getState() == 3){
                solved = true;
            }
            else {
                solved = false;
            }
        }
        return solved;
    }

    @Override
    public String toString(){
        return "";
    }
}

Element.java

import javax.swing.*;
import java.awt.*;

public class Element {

    final int INVISIBLE = 0;
    final int EMPTY = 1;
    final int FIRST_LETTER = 2;
    final int OTHER_LETTER = 3;

    private int state;
    private String letter;

    public Element(){      
    }

//empty block    
    public Element(int state){
        this("", 0);
    }

//filled block
    public Element(String s, int state){
        this.state = state;
        this.letter = s;
    }

    public JButton paint(){

        JButton button = null;

        if( state == EMPTY ){
            button = new JButton("");
            button.setBackground(Color.WHITE);
        }
        else if ( state == FIRST_LETTER ){
            button = new JButton(letter);
            button.setBackground(Color.red);
        }
        else {
            button = new JButton(letter);
            button.setBackground(Color.yellow);
        }
        button.setSize(20,20);

        return button;
    }

    public void changeState(int s){
        state = s;
    }

    public String letter(){
        return letter;
    }

    public int getState(){
        return state;
    }

    @Override
    public String toString(){
        return "["+letter+"]";
    }
}