gpt4 book ai didi

java - 在我的 java GUI 中实现 updateVal(int i) 方法时遇到问题

转载 作者:行者123 更新时间:2023-11-30 03:03:28 25 4
gpt4 key购买 nike

这是我的任务的规范:

编写一个名为 Die 的类,它扩展了 JPanel。代表标准六面骰子的此类对象应在面板上绘制其自身的表示。绘制显示“当前”滚动数字的骰子单个面的自上而下 View 就足够了。该类还应该提供一个方法 updateVal(int i)。它接受 1 到 6 之间的 int,然后更新显示以显示新值。

我已经完成了直到最后一个 updateVal(int i) 方法为止的所有事情,但我遇到了麻烦,并使其覆盖分配给我的paintComponent(Graphics g)中滚动的随机值。基本上这个方法应该能够指定程序在启动时应该显示哪个面。

package weekThree;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.*;

@SuppressWarnings("serial")
public class Die extends JPanel {

static Random rand = new Random();

private Color circleColor = Color.BLACK;
private int circX = 75;
private int circY = circX;
private int circW = 75;
private int circH = circW;

public Die() {
addMouseListener(new MyMouse());
}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int roll = (rand.nextInt(6) + 1); //rolls the dice

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes edges
g2.setColor(circleColor);

if (roll == 1) { //each body updates the associated dots
g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);
}

if (roll == 2) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);
}

if (roll == 3) {
g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);
}

if (roll == 4) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);
}

if (roll == 5) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);
}

if (roll == 6) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(600, 325, circW, circH);
g2.drawOval(600, 325, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);

g2.fillOval(circX, 325, circW, circH);
g2.drawOval(circX, 325, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);
}

}

public void updateVal(int i) { //takes an int i between 1 and 6 and updates the face

if (i < 1 || i > 6) {
new IndexOutOfBoundsException("Choose a number between 1 and 6!");
}

else {
//here the face of the die should be updated according to what value of i is passed through

repaint();
}
}

private class MyMouse extends MouseAdapter {

Random rand = new Random();

public void mousePressed(MouseEvent e) {
int roll = rand.nextInt(6); //re-roll dice on click
repaint(); //repaint components accordingly
}
}

private static void createAndShowGui() { //creation of GUI visuals
JFrame frame = new JFrame("Die Roller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Die());

frame.setSize(800, 800);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

感谢您的帮助

最佳答案

您需要将随机逻辑与图形部分解耦。那么我们有两个选择:

  1. 随机化并“制作图形”
  2. 静态分配和“制作图形”

查看内嵌评论

package weekThree;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.*;

@SuppressWarnings("serial")
public class Die extends JPanel {


static Random rand = new Random();
// make roll a field to be seen by all methods
private int roll;
private Color circleColor = Color.BLACK;
private int circX = 75;
private int circY = circX;
private int circW = 75;
private int circH = circW;

public Die() {
addMouseListener(new MyMouse());
}

protected void paintComponent(Graphics g) {

super.paintComponent(g);
// remove the randomize logic from here
//int roll = (rand.nextInt(6) + 1); //rolls the dice

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes edges
g2.setColor(circleColor);

if (roll == 1) { //each body updates the associated dots
g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);
}

if (roll == 2) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);
}

if (roll == 3) {
g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);
}

if (roll == 4) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);
}

if (roll == 5) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);

g2.fillOval(325, 325, circW, circH);
g2.drawOval(325, 325, circW, circH);
}

if (roll == 6) {
g2.fillOval(circX, circY, circW, circH);
g2.drawOval(circX, circY, circW, circH);

g2.fillOval(600, circY, circW, circH);
g2.drawOval(600, circY, circW, circH);

g2.fillOval(600, 325, circW, circH);
g2.drawOval(600, 325, circW, circH);

g2.fillOval(600, 600, circW, circH);
g2.drawOval(600, 600, circW, circH);

g2.fillOval(circX, 325, circW, circH);
g2.drawOval(circX, 325, circW, circH);

g2.fillOval(circX, 600, circW, circH);
g2.drawOval(circX, 600, circW, circH);
}

}

public void updateVal(int i) { //takes an int i between 1 and 6 and updates the face

if (i < 1 || i > 6) {
new IndexOutOfBoundsException("Choose a number between 1 and 6!");
}

else {
//here the face of the die should be updated according to what value of i is passed through
// static assinment and 'make the graphic'
roll = i;
repaint();
}
}

private class MyMouse extends MouseAdapter {

Random rand = new Random();

public void mousePressed(MouseEvent e) {
//
// updateVal(2); // can be used for test
// randomize and 'make the graphic'
roll = rand.nextInt(6); //re-roll dice on click
repaint(); //repaint components accordingly
}
}

private static void createAndShowGui() { //creation of GUI visuals
JFrame frame = new JFrame("Die Roller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Die());

frame.setSize(800, 800);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 在我的 java GUI 中实现 updateVal(int i) 方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35373839/

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