2012-02-04 69 views
1

我有一個簡單的遊戲正在進行中。截至目前,我想要做的就是點擊綠色按鈕,讓第五和第三塊切換位置。 paintComponent在arraylist中的交換後被調用,但JPanel不刷新以顯示這些更改。當我運行我的應用程序時,我選擇4作爲每一方的棋子。因此,內部的綠色和黑色的碎片應該改變的地方。請幫忙。當我的GamePieces的ArrayList發生變化時,JPanel不會更新

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(""); 
    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; 
} 

    } 

回答

0

添加更多printf s到獲得所發生的事情的詳細信息。在greenMove()內部,在完成交換後打印出ArrayList的內容。在paintComponent內部做同樣的事情。在printf中,還指出從哪裏打印這些調試消息。在printComponent的循環中,繪製每個部分的位置和顏色。

+0

我會給你一個鏡頭 – ElectricTech 2012-02-04 17:47:44

+0

謝謝。我交換球的顏色順序,而不是球的順序。 – ElectricTech 2012-02-04 18:11:40

+0

很高興這有幫助!一個upvote將不勝感激。 – 2012-02-04 19:08:33

相關問題