gpt4 book ai didi

Java图形结账模拟;

转载 作者:行者123 更新时间:2023-12-01 23:15:46 24 4
gpt4 key购买 nike

我正在开发一个结帐模拟项目。我有让它运行的代码,但我很难理解和实现一旦特定条件成立如何添加图形(在我的例子中是一个正方形)。例如,我编写了我的代码,以便它通过随机数,如果随机生成 2、4、6 或 8,则某人将被添加到队列中,如果它们是偶数 1 或 3,则某人将被添加到队列中被从队列中删除。我基本上只是想知道一旦满足我的条件如何在屏幕上添加一个正方形(例如,生成 4 应该在屏幕上添加一个正方形,但事实并非如此)任何帮助将不胜感激!

公共(public)类 MainPanel 扩展 JPanel {

    private Queue<String> tillQueue;
private int rndNumber;
private int currentLength;
private ArrayList<Integer> lengthList;
private double mean;
private Random rand;
private int MAXLENGTH;

private static Random r = new Random();
private static Random r2 = new Random();
Color colour;
private static final int IMAGE_SIZE = 600;

private Timer timer;
private int delay;

private JButton startButton;
private JButton stopButton;
private BufferedImage buffer;
JToolBar toolbar;

public MainPanel() {
startButton = new JButton("START");
stopButton = new JButton("STOP");
toolbar = new JToolBar();
toolbar.add(startButton);
toolbar.add(stopButton);

this.buffer = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_INT_ARGB);
setDoubleBuffered(false);

StartActionHandler start = new StartActionHandler();
StopActionHandler stop = new StopActionHandler();
TimerEvent timerEvt = new TimerEvent();

startButton.addActionListener(start);
stopButton.addActionListener(stop);
delay = 50;
timer = new Timer(delay, timerEvt);
}

public class TimerEvent implements ActionListener {

public void actionPerformed(ActionEvent e) {
//drawNext(buffer.getGraphics());

for (int time = 1; time < 9; time++) {
rndNumber = rand.nextInt(6) + 1; //generates random number

if (rndNumber == 2 || rndNumber == 4 || rndNumber == 6 || rndNumber == 8) {
//time is added to queue
tillQueue.add(String.valueOf(time));
drawNext(buffer.getGraphics());
repaint();
}
}
}
}

public class StartActionHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
timer.start();
}
}

private void drawNext(Graphics g) {
int x = r.nextInt(IMAGE_SIZE);
int y = r.nextInt(IMAGE_SIZE);
int red = r2.nextInt(255);
int green = r2.nextInt(255);
int blue = r2.nextInt(255);
Color randomColour = new Color(red, green, blue);
g.setColor(randomColour);
g.fillRect(x, y, 10, 10);
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(buffer, 0, 0, this);
}
}

最佳答案

注意渲染工作的一些更改:

  • 为方便起见,请使用缓冲区的 createGraphics() 方法,并在完成后使用 dispose() 方法。

  • 将离屏缓冲区初始化为已知状态。

  • 一个 Random 实例通常就足够了。

  • 尽可能限制变量范围,例如私有(private)类TimerEvent

  • 覆盖getPreferredSize()确定渲染区域大小。

image

经测试:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.Timer;

/**
* @see https://stackoverflow.com/a/21238669/230513
*/
public class Test {

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MainPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new Test().display();
}
});
}

private static class MainPanel extends JPanel {

private static final int SIZE = 500;
private static final int DELAY = 100;
private static final Random r = new Random();
private final Queue<String> tillQueue = new LinkedList<>();
private Timer timer;
private JButton startButton;
private JButton stopButton;
private BufferedImage buffer;
private JToolBar toolbar;

public MainPanel() {
super(new BorderLayout());
startButton = new JButton("START");
stopButton = new JButton("STOP");
toolbar = new JToolBar();
toolbar.add(startButton);
toolbar.add(stopButton);
buffer = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = buffer.createGraphics();
g.clearRect(0, 0, SIZE, SIZE);
g.dispose();
StartActionHandler start = new StartActionHandler();
TimerEvent timerEvt = new TimerEvent();
timer = new Timer(DELAY, timerEvt);
startButton.addActionListener(start);
add(new JLabel(new ImageIcon(buffer)));
add(toolbar, BorderLayout.SOUTH);
}

private class TimerEvent implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
for (int time = 1; time < 9; time++) {
if (r.nextInt(6) % 2 == 0) {
tillQueue.add(String.valueOf(time));
drawNext();
}
}
}
}

private class StartActionHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
timer.start();
}
}

private void drawNext() {
Graphics2D g = buffer.createGraphics();
int x = r.nextInt(SIZE);
int y = r.nextInt(SIZE);
g.setColor(new Color(r.nextInt()));
g.fillRect(x, y, 10, 10);
g.dispose();
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(buffer, 0, 0, this);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
}
}

关于Java图形结账模拟;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232313/

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