2012-10-28 33 views
0

我想用Java編寫一個簡單的GUI骰子游戲HighLow。基本上,用戶以50英鎊的初始餘額開始,並且他們可以選擇三種不同的下注金額,並且如果他們選擇低,那麼骰子必須落在2,3,4,5或6上並且支付1:1,並且如果他們選擇高位擲骰子必須在8,9,10,11或12落地並支付1:1。如果他們選擇Sevens,骰子必須落在七號上並支付4:1。有兩個骰子,所以兩者的價值應該被添加。這不是我的問題的重點,但只是認爲我會清楚這一點,所以你明白了比賽的重點。如何在Java中使用JButton來重繪組件?

我完全不熟悉面板,事件處理和類似的東西,所以我目前正在學習,但我有一些困難,試圖解決這個問題。我有五個類:Dice.java包含一個getFaceValue()和throwDie()方法來模擬一個骰子的隨機投擲。 DiceFace.java創建骰子/點的骰子。 DiceFaceViewer.java是框架類。 DiceFaceComponent是兩個骰子的組件,GamePanel是所有JButton和JComboBox等所在的面板。

這是我迄今爲止所做的:創建一個框架,用按鈕等粘在面板上,並將骰子圖像添加到框架中。我想要的是,只要點擊「投擲骰子」按鈕,就可以用隨機的臉部將骰子重新粉刷到框架上。如果我打開和關閉窗口,它現在所做的就是生成一個隨機面部。

骰子

import java.util.Random; 
public class Dice { 

    // Instance variables 
    private int faceValue; 
    private int sides; 
    private Random generator; 

    public Dice(int s) { 
     generator = new Random(); 
     sides = s; 
     faceValue = generator.nextInt(sides) + 1; 
    } 

    // Simulates the throw of a die landing on a random face. 
    public int throwDie() { 
     return faceValue = (int)(Math.random() * sides) + 1; 
    } 

    // Returns the current face value of the die 
    public int getFaceValue() { 
     return faceValue; 
    } 

    // Set face value of die 
    public void setValue(int v) { 
     faceValue = v; 
    } 
} 

DiceFace

import java.awt.*; 
import java.awt.geom.*; 

public class DiceFace { 

    // Holds the seven possible dot positions on a standard die 
    private Ellipse2D.Double[] dots = new Ellipse2D.Double[7]; 

    private Rectangle box; 
    private int xLeft; 
    private int yTop; 
    private int side; 
    private int diceValue; 

    public DiceFace(int s, int x, int y, int v) { 
     side = s;  // Dimension of dice face 
     xLeft = x;  // xPos 
     yTop = y;  // yPos 
     diceValue = v; // Pip value 
    } 

    public void draw(Graphics2D g2) { 
     box = new Rectangle(xLeft, yTop, side, side); 
     makeDots(); 

     // Black background 
     g2.setColor(Color.BLACK); 
     g2.fill(box); 

     // White dots on black 
     g2.setColor(Color.WHITE); 

     // Draw dots 
     if (diceValue == 1) 
      g2.fill(dots[0]); 

     else if (diceValue == 2) { 
      g2.fill(dots[1]); g2.fill(dots[2]); 
     } 

     else if (diceValue == 3) { 
      g2.fill(dots[0]); g2.fill(dots[1]); g2.fill(dots[2]); 
     } 

     else if (diceValue == 4) { 
      g2.fill(dots[1]); g2.fill(dots[2]); 
      g2.fill(dots[3]); g2.fill(dots[4]); 
     } 

     else if (diceValue == 5) { 
      g2.fill(dots[0]); g2.fill(dots[1]); 
      g2.fill(dots[2]); g2.fill(dots[3]); g2.fill(dots[4]); 
     } 

     else if (diceValue == 6) { 
      g2.fill(dots[1]); g2.fill(dots[2]); g2.fill(dots[3]); 
      g2.fill(dots[4]); g2.fill(dots[5]); g2.fill(dots[6]); 
     } 
    } 

    public void makeDots() { 

     int w = side/6; // Dot width 
     int h = side/6; // Dot height 

     dots[0] = new Ellipse2D.Double(xLeft + (2.5 * side)/6, 
       yTop + (2.5 * side)/6, h, w); 

     dots[1] = new Ellipse2D.Double(xLeft + (3.75 * side)/6, 
       yTop + (3.75 * side)/6, h, w); 

     dots[2] = new Ellipse2D.Double(xLeft + (1.25 * side)/6, 
       yTop + (1.25 * side)/6, h, w); 

     dots[3] = new Ellipse2D.Double(xLeft + (1.25 * side)/6, 
       yTop + (3.75 * side)/6, h, w); 

     dots[4] = new Ellipse2D.Double(xLeft + (3.75 * side)/6, 
       yTop + (1.25 * side)/6, h, w); 

     dots[5] = new Ellipse2D.Double(xLeft + (1.25 * side)/6, 
       yTop + (2.5 * side)/6, h, w); 

     dots[6] = new Ellipse2D.Double(xLeft + (3.75 * side)/6, 
       yTop + (2.5 * side)/6, h, w); 
    } 
} 

DiceFaceViewer

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

public class DiceFaceViewer { 
    public static void main(String[] args) { 

     // Create frame 
     JFrame frame = new JFrame(); 

     // Set frame attributes 
     frame.getContentPane().setPreferredSize(new Dimension(360, 320)); 
     frame.pack(); 
     frame.setTitle("High Low"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 

     // Set location of frame to centre screen 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

     int w = frame.getSize().width; 
     int h = frame.getSize().height; 
     int x = (dim.width-w)/2; 
     int y = (dim.height-h)/2; 
     frame.setLocation(x, y); 

     DiceFaceComponent component = new DiceFaceComponent(); 
     frame.add(component); 

     // Add panel to frame 
     GamePanel panel = new GamePanel(); 
     frame.add(panel, BorderLayout.NORTH); 

     frame.setVisible(true); 
    } 
} 

DiceFaceComponent

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

public class DiceFaceComponent extends JComponent { 

    // Instance variables 
    private int faceValue1; 
    private int faceValue2; 

    public DiceFaceComponent() { 
     faceValue1 = new Dice(6).getFaceValue(); 
     faceValue2 = new Dice(6).getFaceValue(); 
    } 

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

     // Recover Graphics2D 
     Graphics2D g2 = (Graphics2D) g; 

     DiceFace dice1 = new DiceFace(75, 66, 160, faceValue1); // s, x, y, v 
     dice1.draw(g2); 
     repaint(); 

     DiceFace dice2 = new DiceFace(75, 219, 160, faceValue2); 
     dice2.draw(g2); 
     repaint(); 
    } 
} 

的GamePanel

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

public class GamePanel extends JPanel { 

    private int balance = 50; 

    public GamePanel() { 

     // Components 
     JRadioButton highButton = new JRadioButton("High"); 
     JRadioButton lowButton = new JRadioButton("Low"); 
     JRadioButton sevensButton = new JRadioButton("Sevens"); 

     // Button group 
     ButtonGroup bg = new ButtonGroup(); 
     bg.add(highButton); 
     bg.add(lowButton); 
     bg.add(sevensButton); 

     add(highButton); 
     add(lowButton); 
     add(sevensButton); 

     // Array to hold bet amounts 
     String betAmounts[] = {"£10", "£5", "£1"}; 

     // Bets combo box 
     JComboBox bets = new JComboBox(betAmounts); 
     add(bets); 

     // Balance 
     JLabel bal = new JLabel(" Balance: £" + balance); 
     add(bal); 

     // Throw dice button 
     final JButton throwButton = new JButton("Throw Dice"); 
     add(throwButton, FlowLayout.RIGHT); 

     class Action implements ActionListener { 

      Dice dice = new Dice(6); 

      public void actionPerformed(ActionEvent e) { 

       dice.throwDie(); 
       dice.getFaceValue(); 
      } 
     } 

     throwButton.addActionListener(new Action()); 
    } 
} 

回答

1

不要從任何paint方法中重繪。這將導致重繪管理器安排另一個重繪事件,最終消耗你的CPU並拖延你的編程。

改爲調用repaint來更新潛水的面值的動作偵聽器。

骰子和骰子DiceFaceComponent之間也沒有連接,它永遠不會知道它應該繪製什麼值。

此外,在你的DiceFace課程中,我不會在每次繪製中重新創建「點」,這只是一種浪費。改爲在構造函數中創建它們。

對於paintComponent方法也是如此,不用重新創建DiceFace,只需在構造類並根據需要更新其面值時創建兩個引用即可。

我會使用Dice作爲將所有各種元素連接在一起的基本模型。通過使用類似ChangeListerner的東西,Dice可能會通知各種組件發生更改,允許它們更新屏幕