簡體   English   中英

Java取消Applet GUI的麻煩

[英]Java Craps Applet GUI Trouble

我在了解Craps游戲中何時何地repaint()時遇到了困難。 我了解在事件的每個實例之后,例如當選擇“開始游戲”或“擲骰子”時,我需要放置repaint() 但是,當我將字符串輸出從“”更改為“ You Won !!”時, 在每種情況下,然后重新打印,應用程序均無法識別。 我已經對該站點進行了掃描以尋找可能的補救措施,但是找不到類似我想做的事情,因為我在骰子圖像上使用.gif並編寫了applet,因此我不能僅在主要方法中退出。 任何批評都歡迎,我可以接受。

到目前為止,我有:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.util.Random;

public class Craps extends JApplet implements ActionListener {

Random gen = new Random();

// constant variables for game status
final int WON = 0, loss = 1, CONTINUE = 2;

// other variables used
boolean firstRoll = true; // true if first roll of dice
int diceSum = 1; // sum of the dice
int aPoint = 1; // point if no win/loss on first roll
int stillGame = CONTINUE; // game not over yet
int dice1 = gen.nextInt(6) + 1;
int dice2 = gen.nextInt(6) + 1;
int diceSec, dice2Sec;
int Horizon = gen.nextInt(260) + 25;
int secHorizon = gen.nextInt(260) + 25;
int Vertical = gen.nextInt(150) + 40;
int SecVerto = gen.nextInt(150) + 40;
Image[] dice = new Image[6];
int Low = 35, High = 335;
int Up = 50, Down = 250;
int wins = 0;
String s1 = "";
// GUI
JButton rollButton, startButton;

public void init() {
    Button rollButton = new Button("Roll Dice");
    Button startButton = new Button("Start Game");

    setSize(400, 400);
    setLayout(null);
    for (int i = 0; i < 6; i++) {
        dice[i] = getImage(getCodeBase(), "dice" + (i + 1) + ".gif");
    }

    // create button to start the game
    startButton.setBounds(40, 300, 100, 20);
    add(startButton);
    startButton.addActionListener(this);
    startButton.setEnabled(true);

    // create button to roll dice
    rollButton.setBounds(230, 300, 100, 20);
    add(rollButton);
    rollButton.addActionListener(this);
    rollButton.setEnabled(true);

} // end of init

public void paint(Graphics g) {
    super.paint(g);

    // draw craps table
    g.setColor(Color.red);
    g.fillRect(1, 1, 400, 400);

    // draw playing field
    g.setColor(Color.green);
    g.fillRoundRect(25, 40, 310, 210, 75, 75);

    // paint the images of the dice
    g.drawImage(dice[dice1 - 1], Horizon, Vertical, 32, 32, this);
    g.drawImage(dice[dice2 - 1], secHorizon, SecVerto, 32, 32, this);

    g.setColor(Color.black);
    g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 22));
    g.drawString(s1, 33, 280);

}

public void actionPerformed(ActionEvent e) {
    // first roll of dice
    Horizon = gen.nextInt(260) + 25;
    secHorizon = gen.nextInt(260) + 25;
    Vertical = gen.nextInt(150) + 40;
    SecVerto = gen.nextInt(150) + 40;

    if (e.getSource() == rollButton) {

//          while (stillGame == CONTINUE) {

            if (firstRoll) {
                diceSum = diceRoller(); // roll dice
                // repaint();

                switch (diceSum) {

                // user victory on first roll
                case 7:
                case 11:
                    stillGame = WON;
                    s1 = "You Win";
                    wins++;
                    break;

                // user loss on first roll
                case 2:
                case 3:
                case 12:
                    stillGame = loss;
                    s1 = "You Lose";
                    break;

                default:
                    stillGame = CONTINUE;
                    aPoint = diceSum;
                    firstRoll = false;
                    s1 = "The Point is " + aPoint + "";
                    break;
                } // end switch
             // end if (firstRoll) statement
            repaint();
            }

            else {
                diceSum = diceRoller(); // roll dice

                // determine game status
                if (diceSum == aPoint) // win by making point
                    s1 = "You Win!!";
                else if (diceSum == 7) // lose by rolling seven
                    s1 = "Suck It";
            }

         // end while loop

    } // end if structure body

    // subsequent roll of dice
    else {

        diceSum = diceRoller(); // roll dice

        // determine game status
        if (diceSum == aPoint) { // win by making point
            s1 = "You Win!!";
            stillGame = WON;

        } else if (diceSum == 7) { // lose by rolling seven
            s1 = "You've Lost";
            stillGame = loss;
        }
    }// end else structure

    if (e.getSource() == startButton) {
        s1 = "";

    }
    repaint();
}

// roll dice, calculate sum and display results
public int diceRoller() {
    int sum;

    dice1 = gen.nextInt(6) + 1; // pick random dice values
    dice2 = gen.nextInt(6) + 1;

    sum = dice1 + dice2; // sum die values

    return sum; // return the sum of dice

} // end method rollDice

} // end

接下來似乎是您的問題:在init()方法中,您聲明了局部變量:

Button rollButton = new Button("Roll Dice");
Button startButton = new Button("Start Game");

並向其中添加ActionListener ,但是在actionPerformed(ActionEvent e)方法中,將source與null進行比較:

e.getSource() == rollButton
e.getSource() == startButton

在這里: rollButton == nullstartButton == null ,因此,您的if語句從不執行,只有else語句。

像下面這樣在init()方法中聲明按鈕:

rollButton = new JButton("Roll Dice");
startButton = new JButton("Start Game");

我認為這對您有幫助。

另請閱讀有關Java中變量的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM