当我的GamePieces的ArrayList发生变化时,JPanel不会更新

时间:2012-02-04 16:57:46

标签: arraylist jpanel

我有一个正在进行的简单游戏。到目前为止,我想要做的就是点击绿色按钮,让第五和第三部分切换位置。在进行arraylist中的交换之后调用paintComponent,但不刷新JPanel以显示这些更改。当我运行我的应用程序时,我选择4为每一方的碎片。因此,内部的绿色和黑色部分应该改变位置。请帮忙。

Number1.java

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Container;

public class Number1 {

private static JButton greenButton,blackButton,newGameButton,inputButton;
private static JTextFieldNumber inputField;
static MyActionListen actionListen;
static Number1 myGame;
static JFrame myDialog,myFrame;
static DrawGamePieces gamePanel;
public int piecesPerSide = 0;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    myGame = new Number1();
    actionListen = new MyActionListen();
    myGame.getStartingTokensValue();

    System.out.println(Color.WHITE.toString());

}

private void makeComponents()  
{
    //Setting up the green button click
    greenButton = new JButton("Green");
    greenButton.setBounds(40, 0 , 100, 40);
    greenButton.setForeground(Color.GREEN);
    greenButton.addActionListener(actionListen);
    //Setting up the black button click
    blackButton = new JButton("Black");
    blackButton.setBounds(greenButton.getLocation().x + greenButton.getWidth() + 10
                , 0 , 100, 40);
    blackButton.setForeground(Color.BLACK);
    blackButton.addActionListener(actionListen);
    //Setting up the new game button click
    newGameButton = new JButton("New Game");
    newGameButton.setBounds(blackButton.getLocation().x + blackButton.getWidth() + 10
                , 0 , 100, 40);
    newGameButton.setForeground(Color.BLUE);
    newGameButton.addActionListener(actionListen);

    //init gamePanel
    gamePanel = new DrawGamePieces(myGame.piecesPerSide,myFrame.getSize().width, myFrame.getSize().height - 40);
    gamePanel.setLocation(0, 40);
    gamePanel.setBackground(Color.YELLOW);

}

private  void makeGameFrame()
{
    myFrame = new JFrame();
    if(myGame.piecesPerSide <= 10)  myFrame.setSize(400, 250);
    else                            myFrame.setSize(600, 250);

    myFrame.setLocation(300, 100);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //sets the default close method
    myFrame.setTitle("Solitary Game"); //sets the title of the window
    Container mainFrameContents = myFrame.getContentPane();//get the content pane to add components to
    mainFrameContents.setLayout(null); //allowed setBounds on components to work properly
    mainFrameContents.setBackground(Color.YELLOW);

    myGame.makeComponents(); //makes all the sub components

    //adds all subcomponents to content pane
    mainFrameContents.add(greenButton); 
    mainFrameContents.add(blackButton); 
    mainFrameContents.add(newGameButton); 
    mainFrameContents.add(gamePanel);
    myFrame.setVisible(true);
}

//gets the starting value of the tokens for the game
private void getStartingTokensValue()
{
    myDialog = new JFrame("New Game Information");
    myDialog.setSize(450, 150);
    myDialog.setLocation(300, 100);
    myDialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //sets the default close method
    JLabel l = new JLabel("Please Enter the Number of Pieces for each side of the game (1-20)");
    l.setBounds((myDialog.getSize().width - 420)/2, 0, 420, 30);
    inputField = new JTextFieldNumber("0123456789");
    inputField.setBounds((myDialog.getSize().width - 100)/2, 35, 100, 30);
    inputButton = new JButton("Submit");
    inputButton.setBounds((myDialog.getSize().width - 100)/2, 75, 100, 30);
    inputButton.addActionListener(actionListen);
    myDialog.setLayout(null);
    myDialog.add(inputButton);
    myDialog.add(l);
    myDialog.add(inputField);
    myDialog.setVisible(true);

}




private static class MyActionListen implements ActionListener
{
    public void actionPerformed(ActionEvent ap)
    {

        if(ap.getSource().equals(greenButton)){
            System.out.println("Requet to green move");
            gamePanel.greenMove();

        }else if(ap.getSource().equals(blackButton)){
            System.out.println("Requet to black move");

        }else if(ap.getSource().equals(newGameButton)){
                System.out.println("Requet to start new game");


        }else if(ap.getSource().equals(inputButton)){
            if(inputField.getText().length()>0)
            {
                myGame.piecesPerSide = Integer.parseInt(inputField.getText());
                if(inputField.checkAllCharacters() && (myGame.piecesPerSide <= 20) && (myGame.piecesPerSide>0))
                {
                    myDialog.dispose();
                    myGame.makeGameFrame();
                }else{
                    JOptionPane.showMessageDialog(inputField, "Please only type in an integer from 1 to 20");
                }
            }
        }
    }
}
}

DrawGamePieces.java

    import java.awt.*;
    import java.util.ArrayList;

    import javax.swing.JPanel;


    public class DrawGamePieces extends JPanel{

private ArrayList<GamePiece> gamePieces;
private int ballR = 15;

//paint or repaints the board
protected void paintComponent( Graphics g ){
    super.paintComponent(g);

    System.out.println("paint components called");

    for(int i=0;i<gamePieces.size();i++)
    {
        GamePiece temp = gamePieces.get(i);
        g.setColor(temp.getColor());
        g.fillOval(temp.x,temp.y,ballR,ballR);
    }
}

//init the game board
public DrawGamePieces(int piecesPerSide,int aWidth,int aHeight){
    gamePieces = new ArrayList<GamePiece>();
    super.setSize(aWidth, aHeight);
    //space between wall and first piece
    int blankSpace = (int)((aWidth - (ballR)*(2*piecesPerSide+1))/2);

    //initalized the pieces in the arraylist
    for(int i=0;i<(2*piecesPerSide+1);i++)
    {
        GamePiece temp = null;
        if(i == 0)                          temp = new GamePiece(blankSpace,80,Color.GREEN);
        if((i < piecesPerSide) && (i != 0)) temp = new GamePiece(ballR+gamePieces.get(i-1).x,80,Color.GREEN);
        if(i > piecesPerSide)               temp = new GamePiece(ballR+gamePieces.get(i-1).x,80,Color.BLACK);
        if(i == piecesPerSide)              temp = new GamePiece(ballR+gamePieces.get(i-1).x,80,Color.YELLOW);
        gamePieces.add(temp);
    }
}

public void greenMove(){
    GamePiece temp = gamePieces.get(5);
    gamePieces.set(5, gamePieces.get(3));
    gamePieces.set(3, temp);

    repaint();
}

public void blackMove(){
    GamePiece temp = gamePieces.get(5);
    gamePieces.set(5, gamePieces.get(3));
    gamePieces.set(3, temp);
}

private int pieceMoveable(Color c){
    int index = -1, start = 0, end = 0,change = 0;
    if(c == Color.GREEN){
        start = 0;
        end = gamePieces.size();
        change = 1;
    }else{
        start = gamePieces.size();
        end = 0;
        change = -1;
    }
    for (int i=start;i<end;i= i+change){
        //if(change = )

    }


    return index;
}

    }

GamePiece.java

    import java.awt.Color;
    import java.awt.Point;

    public class GamePiece extends Point{

private Color pieceColor;

public Color getColor(){
    return pieceColor;
}

public GamePiece()
{
    super();
}

public GamePiece(int x,int y,Color aColor)
{
    super(x,y);
    pieceColor = aColor;
}

    }

1 个答案:

答案 0 :(得分:0)

添加更多printf以获取有关正在发生的事情的更多信息。在greenMove()内,在执行交换后打印出ArrayList的内容。在paintComponent内做同样的事情。在printf中,还指示从哪里打印这些调试消息。在printComponent的循环中,在绘制时打印出每个部分的位置和颜色。