这是我要实现鼠标按钮的主要类。我在 actionPerformed 区域中的代码有问题,让骰子获得一个新值并在按钮单击时在同一位置重新绘制自身
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;
}
}