使用repaint()方法

时间:2010-05-22 13:06:22

标签: java user-interface components

我仍然在努力创造这个游戏:Choosing design method for ladder-like word game。我已经让它几乎正常工作但是有一个问题。当我插入一个单词并且它是正确的时,整个窗口应该重新加载,并且包含字母的JButtons应该以不同的样式重新绘制。但不知何故,游戏面板的repaint()方法(在Main方法中)根本不会影响它。我究竟做错了什么 ?这是我的代码:

主:

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: ");

//setting height of the grid
        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }
        final int height = sc.nextInt();
/*
 *  I'm creating Grid[]game. Each row of game contains Grid of Element[]line.
 *  Each row of line contains Elements, which are single letters in the game.
 */
        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
//############################################

//create new game panel with box layout
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(Color.ORANGE);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

//for each row of the game array add panel containing letters Single panel
//is drawn with Grid's paint() method and then returned here to be added
        for(int i = 0; i < game.length; i++){
            panel.add(game[i].paint());
        }

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

        boolean end = false;
        boolean word = false;
        String text;
/*
 * Game continues until solved() returns true. First check if given word matches the length,
 * and then the value of any row. If yes - change state of each letter from EMPTY
 * to OTHER_LETTER. Then repaint the window.
 */
        while( !end ){
            while( !word ){
                text = JOptionPane.showInputDialog("Input word: ");
                for(int i = 1; i< game.length; i++){
                    if(game[i].equalLength(text)){
                        if(game[i].equalValue(text)){ 
                            game[i].changeState(3);
                            f.repaint();

                            //simple debug - I'm checking if letter, and
                            //state values for each Element are proper
                            for(int k=0; k<=i; k++){
                                System.out.print(game[k].e[k].letter());
                            }
                            System.out.println();
                            for(int k=0; k<=i; k++){
                                System.out.print(game[k].e[k].getState());
                            }
                            System.out.println();
                            //set word to true and ask for another word
                            word = true;
                        }
                    }
                }
            }
            word = false;
            //check if the game has ended
            for(int i = 0; i < game.length; i++){
                if(game[i].solved()){
                    end = true;
                }
                else {
                    end = false;
                }
            }
        }
    }
}

元素:

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);
        }
        return button;
    }

    public void changeState(int s){
        state = s;
    }
    public void setLetter(String s){
        letter = s;
    }
    public String letter(){
        return letter;
    }
    public int getState(){
        return state;
    }
}

网格:

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

public class Grid extends JPanel{
    public 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);
    }

//for Each element in line - change state to i
    public void changeState(int i){
        for(int j=0; j< e.length; j++){
            e[j].changeState(3);
        }
    }

//create panel which will be single row of the game. Add elements to the panel.
// return JPanel to be added to grid.
    public JPanel paint(){
        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(1, e.length));
        panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

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

//check if the length of given string is equal to length of row
    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;
    }

//check if the value of given string is equal to values of elements in row
    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;
    }

//check if the game has finished
    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;
    }
}

1 个答案:

答案 0 :(得分:0)

我自己做了:

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

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.Y_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(height));
        }

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

        boolean end = false;
        boolean word = false;
        String text;

        while( !end ){
            while( !word ){
                text = JOptionPane.showInputDialog("Input word: ");
                for(int i = 1; i< game.length; i++){
                    if(game[i].equalLength(text)){
                        if(game[i].equalValue(text)){
                            word = true;
                            game[i].changeState(3);
                            f.repaint();
                            System.out.println("Word correct !");
                        }
                    }
                }
            }
            word = false;

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


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 void changeState(int i){
        for(int j=1; j< e.length; j++){
            e[j].changeState(3);
        }
    }

    public JPanel create(int height){
        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(1, height));
        Border border = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.ORANGE);
        panel.setBorder(border);

        for(int j = 0; j < e.length; j++){
            panel.add(e[j].paint());
        }
        for (int j=0; j < height - e.length ; j++)
        {
            JPanel hiddenPanel = new JPanel();
            hiddenPanel.setBackground(Color.ORANGE);
            panel.add(hiddenPanel);
        }
        return panel;
    }

    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;
                    break;
                }
            }
            if(equal){
                for(int i = 1; i < e.length; i++){
                    e[i].changeState(3);
                }
            }
        return equal;
    }

    public boolean solved(){
        boolean solved = false;

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

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;
    private JButton button;

    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(){
        this.button = new JButton();

        changeState();
        button.setSize(20,20);

        return button;
    }

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

    private void changeState()
    {
        if( state == EMPTY ){
            button.setText(" ");
            button.setBackground(Color.WHITE);
        }
        else if ( state == FIRST_LETTER ){
            button.setText(letter);
            button.setBackground(Color.red);
        }
        else {
            button.setText(letter);
            button.setBackground(Color.yellow);
        }
    }

    public void setLetter(String s){
        letter = s;
    }

    public String letter(){
        return letter;
    }

    public int getState(){
        return state;
    }
}