Avatar billede dml Nybegynder
15. december 2011 - 12:01 Der er 7 kommentarer

opdatere JLabel

Hej
Jeg har et problem med en spil hvor der vises et image efter hvis tur det er. Den er gemt i en JLabel. Når spiller en har lagt en brik bør billedet ændres til spiller 2. Det virker bare ikke... har den her kode:

package connectfour;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class ConnectFourGUI extends JFrame {
 
  static JLabel[][] board;
  ImageIcon redPieceSat = new ImageIcon("Images/redPieceSat.png");
  ImageIcon bluePiece = new ImageIcon("Images/bluePieceSat.png");
  ImageIcon noBricks = new ImageIcon("Images/noBricks.png");
  ImageIcon player1Img = new ImageIcon("Images/red.png");
  ImageIcon player2Img  = new ImageIcon("Images/blue.png");
  ImageIcon playerImg;
  JLabel lblImg = new JLabel();
  static int CurrentPlayer=1;
 
  public ConnectFourGUI(){
 
  JPanel BoardPanel = new JPanel(new GridLayout(6,7));
  BoardPanel.setBorder(new LineBorder(Color.BLACK));
             
  JLabel[][] board = new JLabel[6][7];
   
    for (int r=0;r<6;r++){
        for (int c=0;c<7;c++){
        board[r][c] = new JLabel(noBricks);
        board[r][c].setBorder(new LineBorder(Color.BLACK));
        board[r][c].setBackground(Color.YELLOW);
        BoardPanel.add(board[r][c]);
        board[r][c].addMouseListener(new listener());
    }
    }
   
  JPanel TurnPanel = new JPanel();
  JLabel lblTurn;
  Font font = new Font("Verdana", Font.BOLD, 25);
  JLabel lblTurnTxt = new JLabel("Your turn player: ");
  lblTurnTxt.setFont(font);
  showPlayer();
  // lblImg = new JLabel(showPlayer());
  TurnPanel.add(lblTurnTxt);
  TurnPanel.add(lblImg);
 
  setLayout(new BorderLayout());
  add(TurnPanel, BorderLayout.NORTH);
  add(BoardPanel, BorderLayout.CENTER);
  }
 
  public int getCurrentPlayer(){
    return CurrentPlayer;   
    }
 
  private void showPlayer(){
    int player = getCurrentPlayer();
      System.out.println("show fanger" +player);
      System.out.println(lblImg.toString());
   
    if (player==1){
        lblImg.setIcon(new ImageIcon("Images/red.png")); 
    }
    else{
        lblImg.setIcon(new ImageIcon("Images/blue.png"));
    }   
  }

    public void exitGame(){
  }
   
    public void nextPlayer(){
    CurrentPlayer = (getCurrentPlayer()%2)+1;
        System.out.println("Næste spiller er:" + CurrentPlayer);
        showPlayer();
    }

 
    public void setBrick(int column, int row) {
       
    if (CurrentPlayer==1){
        System.out.println("her");
        System.out.println(row);
        System.out.println(column);
        //board[column][row]= (new JLabel("ok"));
                //.setIcon(new ImageIcon("Images/bluePieceSat.png"));
       
    }
    else {
        System.out.println("eller her");
        //board[column][row].setIcon(redBrick);
    }
    //currentPlayer = (currentPlayer%2)+1;
  }   
}


class listener implements MouseListener{
     
  ConnectFourGUI gui;
  ConnectFour game;
 
  public void mouseClicked(MouseEvent e) {
            ConnectFourGUI gui = new ConnectFourGUI();
            gui.nextPlayer();
            gui.setBrick(4,2);
           
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }     
}

   
Er der nogen der kan se hvad jeg gør galt?
På forhånd tak :)
Avatar billede dml Nybegynder
15. december 2011 - 12:02 #1
hov det er lblImg der skal ændres.
Den der setBrick bruger jeg ikke endnu... lige nu skal billede bare skifte når man trykker med musen (så kommer resten senere ;))
Avatar billede arne_v Ekspert
15. december 2011 - 15:33 #2
Kan du poste et eksempel med problemet som man kan koere?

Hvis ikke er mit bedste bud at du skal proeve at kalde repaint efter at have opdateret icon.
Avatar billede dml Nybegynder
15. december 2011 - 17:33 #3
Den her kan godt køre :


package connectfour;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class ConnectFourGUI extends JFrame {
 
  static JLabel[][] board;
  ImageIcon redPieceSat = new ImageIcon("Images/redPieceSat.png");
  ImageIcon bluePiece = new ImageIcon("Images/bluePieceSat.png");
  ImageIcon noBricks = new ImageIcon("Images/noBricks.png");
  ImageIcon player1Img = new ImageIcon("Images/red.png");
  ImageIcon player2Img  = new ImageIcon("Images/blue.png");
  ImageIcon playerImg;
  JLabel lblImg = new JLabel();
  static int CurrentPlayer=2;
 
 
 
 
  public ConnectFourGUI(){
 
  JPanel BoardPanel = new JPanel(new GridLayout(6,7));
  BoardPanel.setBorder(new LineBorder(Color.BLACK));
             
  JLabel[][] board = new JLabel[6][7];
   
    for (int r=0;r<6;r++){
        for (int c=0;c<7;c++){
        board[r][c] = new JLabel(noBricks);
        board[r][c].setBorder(new LineBorder(Color.BLACK));
        board[r][c].setBackground(Color.YELLOW);
        BoardPanel.add(board[r][c]);
        board[r][c].addMouseListener(new listener());
    }
    }
   
  JPanel TurnPanel = new JPanel();
  JLabel lblTurn;
  Font font = new Font("Verdana", Font.BOLD, 25);
  JLabel lblTurnTxt = new JLabel("Your turn player: ");
  lblTurnTxt.setFont(font);
  showPlayer();
  // lblImg = new JLabel(showPlayer());
  TurnPanel.add(lblTurnTxt);
  TurnPanel.add(lblImg);
 
  setLayout(new BorderLayout());
  add(TurnPanel, BorderLayout.NORTH);
  add(BoardPanel, BorderLayout.CENTER);
  }
 
  public int getCurrentPlayer(){
    return CurrentPlayer;   
    }
 
  public static void main(String[] args) {
  // GameLogic game = new GameLogic();
  ConnectFourGUI frame = new ConnectFourGUI();
  frame.setTitle("Connect 4");
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  frame.setResizable(false);
    }
 
 
  private void showPlayer(){
    int player = getCurrentPlayer();
      System.out.println("show fanger" +player);
      System.out.println(lblImg.toString());
   
    if (player==1){
        lblImg.setIcon(new ImageIcon("Images/red.png")); 
    }
    else{
        lblImg.setIcon(new ImageIcon("Images/blue.png"));
    }   
  repaint();
  }

    public void exitGame(){
  }
   
    public void nextPlayer(){
    CurrentPlayer = (getCurrentPlayer()%2)+1;
        System.out.println("Næste spiller er:" + CurrentPlayer);
        showPlayer();
    }

 
    public void setBrick(int column, int row) {
       
    if (CurrentPlayer==1){
        System.out.println("her");
        System.out.println(row);
        System.out.println(column);
        //board[column][row]= (new JLabel("ok"));
                //.setIcon(new ImageIcon("Images/bluePieceSat.png"));
       
    }
    else {
        System.out.println("eller her");
        //board[column][row].setIcon(redBrick);
    }
    //currentPlayer = (currentPlayer%2)+1;
  }   
}


class listener implements MouseListener{
     
  ConnectFourGUI gui;
  ConnectFour game;
 
  public void mouseClicked(MouseEvent e) {
            ConnectFourGUI gui = new ConnectFourGUI();
            gui.nextPlayer();
            gui.setBrick(4,2);
           
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }     
}

   
Den opretter bare en spille plade og sætter det billede, der svarer til spiller 1 på... men den opdaterer ikke når man trykker med musen
Avatar billede dml Nybegynder
15. december 2011 - 21:24 #4
Er der slet ingen der har nogen ideer??
Avatar billede arne_v Ekspert
17. december 2011 - 03:56 #5
repaint();

er ikke engang noedvendig - du skal bare kalde nextPlayer paa det rigtige GUI objekt.
Avatar billede arne_v Ekspert
17. december 2011 - 03:57 #6
board[r][c].addMouseListener(new listener(this));

og:

class listener implements MouseListener{
   
  ConnectFourGUI gui;
  ConnectFour game;

  listener(ConnectFourGUI gui) {
      this.gui = gui;
  }
  public void mouseClicked(MouseEvent e) {
            //ConnectFourGUI gui = new ConnectFourGUI();
            gui.nextPlayer();
            gui.setBrick(4,2);
         
        }
Avatar billede arne_v Ekspert
12. januar 2012 - 22:01 #7
ok?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester