gpt4 book ai didi

java - 我怎样才能让这个 JButton 重绘我的 Die Graphics?

转载 作者:行者123 更新时间:2023-11-29 06:00:19 26 4
gpt4 key购买 nike

这是我要实现鼠标按钮的主类。我在使用 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;
}
}

最佳答案

我认为您需要更改 Die 类并为其提供一个 roll() 方法,它调用 Roe 的 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(...) 中,在显示的 Die 对象上调用这个新方法。

关于java - 我怎样才能让这个 JButton 重绘我的 Die Graphics?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com