- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我要实现鼠标按钮的主类。我在使用 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/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!