Java 如何让这个JButton重新绘制模具图形?
这是我的主要类,我想实现鼠标按钮。我在actionPerformed区域的代码中遇到了问题,无法让模具获得一个新值,并在单击按钮的相同位置重新绘制自身Java 如何让这个JButton重新绘制模具图形?,java,swing,graphics,jbutton,graphics2d,Java,Swing,Graphics,Jbutton,Graphics2d,这是我的主要类,我想实现鼠标按钮。我在actionPerformed区域的代码中遇到了问题,无法让模具获得一个新值,并在单击按钮的相同位置重新绘制自身 这是我创建模具的模具类 这是我的roll类,它获取骰子的值 我认为您需要更改Die类,并为其提供roll()方法,其中它调用Roe的die1()方法,并将number字段设置为该值,然后调用repaint。在半伪码中: public void roll() { Use Roll to get a new int value se
这是我创建模具的模具类
这是我的roll类,它获取骰子的值
我认为您需要更改Die类,并为其提供roll()方法,其中它调用Roe的die1()方法,并将number字段设置为该值,然后调用repaint。在半伪码中:
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对象调用此新方法。我在RollListener的actionPerformed(…)
方法中没有看到任何代码。您可能想先尝试一些东西,向我们展示您尝试过的东西,然后让我们知道它是否起作用,它做错了什么。1)要更快获得更好的帮助,请发布一个。2) 是这个吗?如果是这样的话,把它贴上这样的标签是有好处的。@Hov“既然你没有回应……”(怀旧)我记得在usenet上,有几天我会责骂人们质疑回答、质疑问题等,直到48小时过去。日子一天天过去……)抱歉,我正在吃东西,我已经尝试过的代码是Die die1=新的Die(0,0,50,50),然后是Die.repaint();出于某种原因,我认为它创建了模具,但它只使它变大了一个像素,因为在我点击按钮后,它在模具旁边显示了一个红色的小像素button@Andrew:点对了。根据新信息提供的答案。否决票被取消了,成功了!非常感谢你为什么这么做有效,而当我试着给点名课打电话时却没有用?@Alex:不客气。Roll类只创建一个随机数,不多不少。你需要更新Die对象的number字段,我怀疑你没有这样做。是的,这是有道理的,我知道我没有在原来的Roll类中重新绘制,但我现在得到了,谢谢again@Alex:您在尝试中还创建了一个新的模具对象,是的,您将在创建该对象时更改该对象上的数字字段,但它是一个全新的对象,与GUI中显示的模具对象无关。该部分帮助我更新了模具,但它不会让我在另一个模具旁边创建第二个模具,它所做的只是更换第一个模具。我是否创建了类似于绘制两个骰子的数组?我更新了新代码
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;
}
}
public void roll() {
Use Roll to get a new int value
set number field to this new int value
call repaint on itself
}