2011-12-12 9 views
6

मैं क्रेप्स के जीयूआई गेम को कोडिंग कर रहा हूं। "रोल" नामक एक जेबटन है जिसे गेम के लिए पासा रोल करते समय क्लिक किया जाता है। जीयूआई तब दिखाता है कि आपने जेपीईजी के मरने वाले चेहरों का उपयोग करके क्या किया था।जावा स्विंग डाइस रोलिंग एनीमेशन

सबकुछ बढ़िया काम करता है, सिवाय इसके कि मुझे अब जीयूआई में एनीमेशन जोड़ना है। मेरा विचार जेपीईजी को प्रदर्शित करने की एक ही विधि का उपयोग करके किसी भी तरह से थोड़े समय के लिए अलग-अलग चेहरे के मूल्यों को तेजी से प्रदर्शित करना था (एक "रोल" सिमुलेट करना)। हालांकि, जैसा कि मुझे यकीन है कि आप सभी जानते हैं, यह काम नहीं करता है।

मैं ईडीटी और टाइमर कक्षा के विचार से परिचित हूं, लेकिन मुझे यकीन नहीं है कि उनका उपयोग कैसे किया जाए। असल में मैं यह एनीमेशन तब करना चाहता हूं जब मैं "रोल" बटन दबाता हूं, और जब एनीमेशन समाप्त हो जाता है, तो मैं इसे प्रदर्शित करना चाहता हूं जो वास्तव में पहले की तरह लुढ़का था।

किसी भी मदद की सराहना की जाएगी। यहां मेरे पास अब तक का कोड है:

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

अद्यतन कोड के साथ संपादित करें !!!

यहाँ जहां टाइमर और सरणी की घोषणा की जाती है:

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

private ImageIcon imageArray[];` 

/* CODE */ 

और यहाँ जहां सरणी NCrapsGUI निर्माता अंदर से भर जाता है है:

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

    initComponents();` 

इस पूरी rollActionPerformed तरीका है। मुझे लगता है कि शुरुआत में टाइमर सही शुरू हो गया है, लेकिन जब भी मैं इसे शुरू करने का प्रयास करता हूं तो मुझे त्रुटि का भार मिलता है। हालांकि, जब मैं मैंने एक नया जेपीनेल अलग से बनाया, और इसे एक्शन श्रोता को कार्यान्वित किया, तो मुझे त्रुटियां नहीं मिलीं। मैं इस घोषणा को लागू करता ActionListner जोड़ने की कोशिश की है, लेकिन NetBeans सचमुच मुझ में कुछ भी लिखने नहीं जाने देंगे।

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

सुनिश्चित नहीं है कि आपको किसने वोट दिया या क्यों (एक ड्राइव-डाउन-वोट), लेकिन मैंने आपको इसका विरोध करने के लिए वोट दिया। Http://sscce.org/ +1 –

+0

। मैं पहले एक ही मरने की समस्या को हल करने पर भी विचार करता हूं। – mKorbel

उत्तर

6

एनीमेशन के पीछे आपका मूल विचार एक अच्छा एक मुझे लगता है कि है, लेकिन क्या यह काम करता है या पाठ्यक्रम के कार्यान्वयन विवरण में सभी नहीं है। मेरा सुझाव है कि

  • जो आपने अपनी छवियों में पढ़ा है और शायद प्रोग्राम की शुरुआत में छवि आइकन बनाते हैं।
  • कि आपने आइकनों को एक छवि आइकन एरे में 7 की लंबाई के साथ रखा है - लेकिन आप 0 वें आइटम को छोड़कर, 1-6 स्लॉट में आइकन डाल देंगे।
  • कि आप कुछ उचित विलंब के साथ यादृच्छिक रूप से इन आइकनों को स्वैप करने के लिए एक स्विंग टाइमर का उपयोग करते हैं, 200 या 300 एमएससी कहते हैं।
  • कि आप 1 और 6 के बीच यादृच्छिक संख्या प्राप्त करने के लिए एक रैंडम ऑब्जेक्ट का उपयोग करते हैं, और उसके बाद इस नंबर के साथ आपकी सरणी अनुक्रमणिका के रूप में, आइकन को सरणी से बाहर निकालें।
  • कि आप जेएलएबल में छवि आइकन प्रदर्शित करते हैं (या दो जेएलएबल्स यदि आप 2 मर रहे हैं) और जेएलएबल की setIcon(...) विधि को कॉल करके आइकन को स्वैप करें।

संपादित अपनी टिप्पणी में
आप राज्य है कि आप की कोशिश की:

timer = new Timer(100,this); 

और वह आपकी समस्या है - this के आपके उपयोग। आपको सब कुछ के लिए एक ही एक्शनलिस्टर का उपयोग करने की कोशिश नहीं करनी चाहिए। इसके बजाय वहां एक एक्शनलिस्टर बनाएं, जहां आपको इसकी आवश्यकता है। कुछ,

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

+1 सलाह पोस्ट करने के लिए – trashgod

+0

क्या मैं यह सब कोड अपने रोलएक्शनप्रोर्टेड विधि में डालता हूं? सरणी populating के अलावा। – lessthanjacob

+1

आप अपने इमेज आइकॉन को कन्स्ट्रक्टर में बनाएंगे। रोलएक्शनप्रोफॉर्म की संभावना एक बटन द्वारा शुरू की जाएगी और टाइमर शुरू करेगा जिसमें अपना स्वयं का एक्शनलिस्टर होगा, और वहां आप जहां तक ​​एक्स को कई बार दोहराते हैं, तब तक आप यादृच्छिक रूप से आइकन स्वैप करते हैं (आप इसे एक इंट काउंटर वेरिएबल देंगे जो आप बढ़ते हैं और चेक)। –

संबंधित मुद्दे