2011-12-12 44 views
6

Tôi đang mã hóa một trò chơi GUI của craps. Có một JButton gọi là "cuộn" mà khi nhấp vào cuộn xúc xắc cho trò chơi. GUI sau đó hiển thị những gì bạn đã cuộn bằng cách sử dụng khuôn mặt chết của jpeg.Java Swing Dice Rolling Animation

Mọi thứ hoạt động tốt, ngoại trừ tôi phải thêm hoạt ảnh vào GUI. Ý tưởng của tôi là bằng cách nào đó nhanh chóng hiển thị các giá trị khuôn mặt khác nhau trong một khoảng thời gian ngắn (mô phỏng một "cuộn") bằng cách sử dụng cùng một phương pháp hiển thị của jpeg. Tuy nhiên, như tôi chắc chắn tất cả các bạn biết, điều đó không hoạt động.

Tôi quen thuộc với ý tưởng về EDT và lớp Timer, nhưng tôi không chắc chắn chính xác cách sử dụng chúng. Về cơ bản, tôi muốn hoạt ảnh này xảy ra khi tôi nhấn nút "cuộn" và khi hoạt ảnh kết thúc, tôi muốn nó hiển thị những gì đã thực sự được cuộn như trước đây.

Mọi trợ giúp sẽ được đánh giá cao. Đây là mã tôi có, cho đến nay:

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


/* This is the GUI declaration */ 
public class NCrapsGUI extends JFrame { 
    //code... 
/* Action when "roll" is clicked */ 
    private void rollActionPerformed(java.awt.event.ActionEvent evt){           
     game.rollDice(); 
      //Rolls both die 
      sumOfDice.setText(Integer.toString(game.getSum())); 
      //Displays the sum of the die 
      numRolls.setText(Integer.toString(game.getNumRolls())); 
      //Displays the number of rolls in each game 
      // <editor-fold defaultstate="collapsed" desc="Die JPEG's"> 
      // If statements display the die face based on the number rolled 
      if (game.getDie1Value() == 1) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg"))); 
      } 
      if (game.getDie1Value() == 2) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg"))); 
      } 
      if (game.getDie1Value() == 3) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg"))); 
      } 
      if (game.getDie1Value() == 4) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg"))); 
      } 
      if (game.getDie1Value() == 5) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg"))); 
      } 
      if (game.getDie1Value() == 6) { 
       die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg"))); 
      } 
      if (game.getDie2Value() == 1) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg"))); 
      } 
      if (game.getDie2Value() == 2) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg"))); 
      } 
      if (game.getDie2Value() == 3) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg"))); 
      } 
      if (game.getDie2Value() == 4) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg"))); 
      } 
      if (game.getDie2Value() == 5) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg"))); 
      } 
      if (game.getDie2Value() == 6) { 
       die2Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg"))); 
      } 
      //</editor-fold> 
      /* 
      * If the game is beyond the first roll, it checks to see if the sum of the 
      * values rolled equal 7 or the point value for a loss or win respectively. 
      * If it is a win, it adds a win. Likewise for a loss. 
      */ 
      if (game.getGameStatus() == 2) { 
       if (game.getSum() == game.getPoint()) { 
        game.addWin(); 
        numWins.setText(Integer.toString(game.getWins())); 
        game.setGameStatus(1); 
        game.setPoint(0); 
        game.resetRolls(); 
        return; 
       } 
       if (game.getSum() == 7) { 
        game.addLoss(); 
        numLosses.setText(Integer.toString(game.getLosses())); 
        game.setGameStatus(1); 
        game.setPoint(0); 
        game.resetRolls(); 
        return; 
       } 
      } 

      /* 
      * This checks to see if the game is on the first roll. If it is, it checks 
      * if the sum of the die is 7 or 11 for a win, or 2, 3, or 12 for a loss. If 
      * not, it passes it on to the next roll and sets the point value to the sum 
      */ 
      if (game.getGameStatus() == 1) { 
       game.setPoint(game.getSum()); 
       dieSum.setText(Integer.toString(game.getPoint())); 

       if (((game.getSum() == 7) || ((game.getSum() == 11)))) { 
        game.addWin(); 
        numWins.setText(Integer.toString(game.getWins())); 
        game.setPoint(0); 
        dieSum.setText(Integer.toString(game.getPoint())); 
        game.resetRolls(); 
        return; 
       } 

       if (((game.getSum() == 2) || ((game.getSum()) == 3)) || (game.getSum()) == 12) { 
        game.addLoss(); 
        numLosses.setText(Integer.toString(game.getLosses())); 
        game.setPoint(0); 
        dieSum.setText(Integer.toString(game.getPoint())); 
        game.resetRolls(); 
        return; 
       } else { 
        game.setGameStatus(2); 
       } 
      } 
     }          

CHỈNH SỬA VỚI MÃ CẬP NHẬT !!!

Đây là nơi các Timer và mảng được khai báo:

public class NCrapsGUI extends JFrame 
{ 
private Timer timer; 
private int numPlayers; 
private int totalIcons = 6; 

private ImageIcon imageArray[];` 

/* CODE */ 

Và đây là nơi mảng được dân cư bên trong constructor NCrapsGUI:

imageArray = new ImageIcon[totalIcons]; 
    for (int i = 0; i < 6 ;i++) 
    { 
     int temp = i + 1; 
     imageArray[i] = new ImageIcon("face" + temp + ".jpg"); 
    } 

    initComponents();` 

Đây là toàn bộ phương pháp rollActionPerformed. Tôi đoán Timer được bắt đầu ngay ngay từ đầu, nhưng bất cứ khi nào tôi cố gắng khởi động nó, tôi nhận được rất nhiều lỗi. Tuy nhiên, khi tôi Tôi đã tạo một JPanel riêng biệt và làm cho nó thực hiện công cụ lắng nghe hành động, tôi không bị lỗi . Tôi đã cố gắng thêm dụng cụ ActionListner để tuyên bố này, nhưng NetBeans nghĩa đen sẽ không cho phép tôi gõ bất cứ điều gì trong.

private void rollActionPerformed(java.awt.event.ActionEvent evt) {          



game.rollDice(); 
//Rolls both die 

sumOfDice.setText(Integer.toString(game.getSum())); 
//Displays the sum of the die 

numRolls.setText(Integer.toString(game.getNumRolls())); 
//Displays the number of rolls in each game 

// <editor-fold defaultstate="collapsed" desc="Die JPEG's"> 
// If statements display the die face based on the number rolled 
if (game.getDie1Value() == 1) 
{ 
    die1Disp.setIcon(imageArray[0]); 
} 

if (game.getDie1Value() == 2) 
{ 
    die1Disp.setIcon(imageArray[1]); 
} 

if (game.getDie1Value() == 3) 
{ 
    die1Disp.setIcon(imageArray[2]); 
} 

if (game.getDie1Value() == 4) { 
    die1Disp.setIcon(imageArray[3]); 
} 

if (game.getDie1Value() == 5) { 
    die1Disp.setIcon(imageArray[4]); 
} 

if (game.getDie1Value() == 6) 
{ 
    die1Disp.setIcon(imageArray[5]); 
} 

if (game.getDie2Value() == 1) 
{ 
    die2Disp.setIcon(imageArray[0]); 
} 

if (game.getDie2Value() == 2) 
{ 
    die2Disp.setIcon(imageArray[1]); 
} 


if (game.getDie2Value() == 3) 
{ 
    die2Disp.setIcon(imageArray[2]); 
} 

if (game.getDie2Value() == 4) 
{ 
    die2Disp.setIcon(imageArray[3]); 
} 

if (game.getDie2Value() == 5) 
{ 
    die2Disp.setIcon(imageArray[4]); 
} 

if (game.getDie2Value() == 6) 
{ 
    die2Disp.setIcon(imageArray[5]); 
} 

//</editor-fold> 

/* 
* If the game is beyond the first roll, it checks to see if the sum of the 
* values rolled equal 7 or the point value for a loss or win respectively. 
* If it is a win, it adds a win. Likewise for a loss. 
*/ 

if (game.getGameStatus() == 2) { 
    if (game.getSum() == game.getPoint()) { 
     game.addWin(); 
     numWins.setText(Integer.toString(game.getWins())); 
     game.setGameStatus(1); 
     game.setPoint(0); 
     game.resetRolls(); 
     return; 
    } 

    if (game.getSum() == 7) { 
     game.addLoss(); 
     numLosses.setText(Integer.toString(game.getLosses())); 
     game.setGameStatus(1); 
     game.setPoint(0); 
     game.resetRolls(); 
     return; 
    } 
} 

'

+0

Không chắc bạn đã bỏ phiếu cho bạn hoặc tại sao (lái xe bằng cách bỏ phiếu), nhưng tôi đã bỏ phiếu cho bạn để chống lại nó. –

+0

để đăng http://sscce.org/ +1 – mKorbel

Trả lời

6

ý tưởng cơ bản của bạn đằng sau những hình ảnh động là một trong những tốt tôi nghĩ, nhưng dù nó hoạt động hoặc không phải là tất cả trong các chi tiết thực hiện của khóa học. Tôi đề xuất

  • Bạn đọc trong hình ảnh của mình và tạo ImageIcons một lần, có thể là lúc bắt đầu chương trình.
  • Bạn đặt các biểu tượng vào một mảng ImageIcon với chiều dài là 7 - nhưng bạn sẽ đặt một biểu tượng vào các khe 1-6, để lại mục thứ 0 không có giá trị.
  • Bạn sử dụng Bộ hẹn giờ xoay để hoán đổi các biểu tượng này ngẫu nhiên với một số độ trễ thích hợp, nói 200 hoặc 300 msec.
  • Bạn sử dụng đối tượng Ngẫu nhiên để lấy số ngẫu nhiên từ 1 đến 6, và sau đó với số này làm chỉ mục mảng của bạn, hãy lấy Biểu tượng ra khỏi mảng.
  • Bạn hiển thị ImageIcons trong một JLabel (hoặc hai JLabels nếu bạn đang hiển thị 2 biểu tượng chết) và hoán đổi biểu tượng bằng cách đơn giản gọi phương thức setIcon(...) của JLabel.

Sửa
Bạn nhà nước trong bình luận của bạn rằng bạn đã cố gắng:

timer = new Timer(100,this); 

Và đó là vấn đề của bạn - việc bạn sử dụng this. Bạn không nên cố gắng sử dụng cùng một ActionListner cho mọi thứ. Thay vào đó tạo một ActionListener ngay tại đó, nơi bạn cần nó. Một cái gì đó như,

timer = new Timer(100, new ActionListener() { 
    public void actionPerformed(ActionEvent actionEvt) { 
     // ... put your ActionListener's code here 
    } 
    }); 
+2

1 lời khuyên. Tôi cũng xem xét giải quyết vấn đề dựng hình một cái chết duy nhất, trước tiên. – trashgod

+0

Tôi có đặt tất cả mã này trong phương thức rollActionPerformed của mình không? Ngoài việc điền vào mảng. – lessthanjacob

+1

Bạn sẽ tạo ImageIcons của mình trong một hàm tạo. RollActionPerformed có thể sẽ được bắt đầu bằng một nút và sẽ khởi động Timer sẽ có ActionListener của riêng nó, và có nơi bạn hoán đổi các biểu tượng ngẫu nhiên cho đến khi nó lặp lại x lần (bạn sẽ cho nó một biến truy cập int mà bạn tăng và kiểm tra). –