2012-04-30 95 views
1

這是我想要實現鼠標按鈕的主要類。我有在actionPerformed區域代碼的麻煩有死得到一個新的值,並重新繪製自己在按鈕上的同一位置單擊我怎樣才能讓這個JButton重繪我的Die Graphics?


import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Game 
{ 
private static Die die; 
public static void main(String[] agrs) 
{ 
    JFrame frame = new JFrame(); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 

    JButton roll = new JButton("roll"); 
    class RollListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      die.roll(); 
          //This is where it is just replacing the first die 
         die2.roll2(); 
     } 
    } 
    RollListener listener = new RollListener(); 
    roll.addActionListener(listener); 
    panel.add(roll,BorderLayout.NORTH); 

    die = new Die(20,20, Roll.die1()); 
    panel.add(die, BorderLayout.CENTER); 

       //This im pretty sure wont add correctly its just overlapping the first die 
       //but from what iv seen if i use different layouts it draws different and sometimes not at all. 
       die2 = new Die(75,20,Roll.die2()); 
       panel.add(die2, BorderLayout.CENTER); 

    frame.add(panel); 

    //When program closed it terminates program execution 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //Makes frame visible 
    frame.setVisible(true); 
} 
} 

這是創建死

我模具類
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.geom.Ellipse2D; 

import javax.swing.JComponent; 


public class Die extends JComponent 
{ 
private static final long serialVersionUID = 1L; 

//Variable for creating the die background 
private Rectangle dieBorder; 
//private int number = Roll.die1(); 
//Variable for creating the number of circles on die 
private Ellipse2D numCircle; 
private Ellipse2D numCircle2; 
private Ellipse2D numCircle3; 
private Ellipse2D numCircle4; 
private Ellipse2D numCircle5; 
private Ellipse2D numCircle6; 
private Ellipse2D numCircle7; 

     private int width = 50; 
     private int height = 50; 

/* 
* This constructs the DieComponent 
* with @param number. 
*/ 
public Die(int xPos, int yPos, int number1) 
{ 
    number = number1; 
    dieBorder = new Rectangle(xPos, yPos, width, height); 

    numCircle = new Ellipse2D.Double(locationX(xPos, 2, 0), locationY(yPos, 2, 0), 8, 8); 
    numCircle2 = new Ellipse2D.Double(locationX(xPos, 1, -7), locationY(yPos, 1, -7), 8, 8); 
    numCircle3 = new Ellipse2D.Double(locationX(xPos, 1, -43), locationY(yPos, 1, -43), 8, 8); 
    numCircle4 = new Ellipse2D.Double(locationX(xPos, 1, -7), locationY(yPos, 1, -43), 8, 8); 
    numCircle5 = new Ellipse2D.Double(locationX(xPos, 1, -43), locationY(yPos, 1, -7), 8, 8); 
    numCircle7 = new Ellipse2D.Double(locationX(xPos, 2, -18), locationY(yPos, 2, 0), 8, 8); 
    numCircle6 = new Ellipse2D.Double(locationX(xPos, 2, 18), locationY(yPos, 2, 0), 8, 8); 
} 

/* 
* This will create the die graphics for 
* the game. With @param g. 
*/ 
public void paintComponent(Graphics g) 
{ 
    Graphics2D g2 = (Graphics2D) g; 
    super.paintComponent(g2); 

    //Sets color of field 
    g2.setColor(Color.RED); 
    //Draws the die background 
    g2.draw(dieBorder); 
    //Fills in the background 
    g2.fill(dieBorder); 

    if(number == 1) 
    { 
    //Sets color to draw the circle with 
    g2.setColor(Color.WHITE); 
    //Draws the circle 
    g2.draw(numCircle); 
    //Fills in the circle 
    g2.fill(numCircle); 
    } 

    if(number == 2) 
    { 
     g2.setColor(Color.WHITE); 
     g2.draw(numCircle2); 
     g2.draw(numCircle3); 
     g2.fill(numCircle3); 
     g2.fill(numCircle2); 
    } 

    if(number == 3) 
    { 
     g2.setColor(Color.WHITE); 
     g2.draw(numCircle2); 
     g2.draw(numCircle3); 
     g2.draw(numCircle); 
     g2.fill(numCircle3); 
     g2.fill(numCircle2); 
     g2.fill(numCircle); 
    } 

    if(number == 4) 
    { 
     g2.setColor(Color.WHITE); 
     g2.draw(numCircle2); 
     g2.draw(numCircle3); 
     g2.draw(numCircle4); 
     g2.draw(numCircle5); 
     g2.fill(numCircle3); 
     g2.fill(numCircle2); 
     g2.fill(numCircle4); 
     g2.fill(numCircle5); 

    } 

    if(number == 5) 
    { 
     g2.setColor(Color.WHITE); 
     g2.draw(numCircle); 
     g2.draw(numCircle2); 
     g2.draw(numCircle3); 
     g2.draw(numCircle4); 
     g2.draw(numCircle5); 
     g2.fill(numCircle3); 
     g2.fill(numCircle2); 
     g2.fill(numCircle4); 
     g2.fill(numCircle); 
     g2.fill(numCircle5); 
    } 

    if(number == 6) 
    { 
     g2.setColor(Color.WHITE); 
     g2.draw(numCircle2); 
     g2.draw(numCircle3); 
     g2.draw(numCircle4); 
     g2.draw(numCircle5); 
     g2.draw(numCircle6); 
     g2.draw(numCircle7); 
     g2.fill(numCircle3); 
     g2.fill(numCircle2); 
     g2.fill(numCircle4); 
     g2.fill(numCircle5); 
     g2.fill(numCircle6); 
     g2.fill(numCircle7); 
    } 
} 

public static int locationX(int xPos, int spot, int move) 
{ 
    int xPosition = (xPos + 50/spot) - 4 + move; 

    //Returns the x position of where the circle should be 
    return xPosition; 
} 

/* 
* This method will get the height of 
* the border of the die and formulate where 
* to place the y position of the numCircle based 
* on value of die and uses spot to determine where 
* to draw circle. 
*/ 
public static int locationY(int yPos, int spot, int move) 
{ 
    //Formula to calculate position of circle 
    int yPosition = (yPos + 50/spot) - 4 + move; 

    //Returns the y position of where circle should be 
    return yPosition; 
} 

     public void roll() 
     { 
     number = Roll.die1(); 
     repaint(); 
     } 
     //This is where i think im messing up cuz it just replaces the first die 
     public void roll2() 
     { 
     number = Roll.die2(); 
     repaint(); 
     } 
} 

這是我的卷類,得到值的死


import java.util.Random; 

/* 
* This class sets up methods that 
* retrieve what random number is to 
* be displayed on the die for each 
* one. 
*/ 
public class Roll 
{ 
//Sets up var for die1 
private static int die1; 
//Sets up var for die2 
private static int die2; 

/* 
* This method creates the die1 
* and it assigns a random number 
* between 1-6 to the die. 
*/ 
public static int die1() 
{ 
    //Create random number generator 
    Random gen = new Random(); 

    //Uses generator to assign number 
    die1 = gen.nextInt(6) + 1; 

    //Returns die1 value 
    return die1; 
} 


/* 
* This method creates die2 and 
* assigns a random number between 
* 1-6 to the die. 
*/ 
public static int die2() 
{ 
    //Create random number generator 
    Random gen2 = new Random(); 

    //Uses generator to assign number 
    die2 = gen2.nextInt(6) + 1; 

    //Returns die2 value 
    return die2; 
} 

/* 
* This method gets the value of both 
* die1 and die2 and adds them together. 
* This will allow the game to decide 
* what bets win and what bets lose. 
*/ 
public static int total() 
{ 
    //Gets die1 and die2 and adds them 
    int total = die1 + die2; 
    //Returns total value 
    return total; 
} 
} 
+1

我在你的RollListener的'actionPerformed(...)'方法中看不到任何代碼。您可能想先嚐試一下,然後告訴我們您嘗試了什麼,然後讓我們知道它是否無效,它做錯了什麼。 –

+0

1)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 2)這是[標籤:家庭作業]?如果是這樣,那麼標記它是有利的。 –

+2

@霍夫*「因爲你沒有迴應..」*(懷舊)我記得那些日子,回到usenet上,當我挑剔人們挑戰反應,碰到問題等等,直到48小時過去。 Days gone by ..;) –

回答

3

我認爲你需要改變你的模具類,並給它在那裏叫羅伊的die1()方法,並設置號字段該數值和調用重繪滾動()方法。在半僞代碼:

public void roll() { 
    Use Roll to get a new int value 
    set number field to this new int value 
    call repaint on itself 
} 
中的ActionListener的 actionPerformed(...)通話時顯示的模具對象上這種新方法

然後。

+0

好吧生病試試,謝謝 –

+0

它工作!非常感謝爲什麼做這個工作,當我嘗試調用Roll類時它不工作? –

+0

@亞歷克斯:不客氣。 Roll類只創建一個隨機數,沒有什麼更多。您需要更新Die對象的數字字段,並且我懷疑您沒有這樣做。 –