Java图形签出模拟; [英] Java Graphics Checkout Simulation;

查看:84
本文介绍了Java图形签出模拟;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Checkout Simulation项目。我有让代码正常运行的代码,但是我一直在努力理解和实现如何在一定条件成立时添加图形(在我的情况下是方形)。例如,我编写了我的代码,以便通过随机数字,如果2,4,6或8是随机生成的,则会将某人添加到队列中,并且如果它们是偶数1或3,从队列中移除。我基本上只是想知道如何在屏幕上添加一个正方形(例如,生成一个4应该在屏幕上添加一个方块,但它不会)
任何帮助真的会被赞赏!


$ b public class MainPanel extends JPanel {

  private Queue< String> tillQueue; 
private int rndNumber;
private int currentLength;
private ArrayList< Integer> lengthList;
私人双重均值;
private随机rand;
private int MAXLENGTH;

private static Random r = new Random();
private static Random r2 = new Random();
颜色的颜色;
private static final int IMAGE_SIZE = 600;

私人定时器定时器;
private int delay;

私人JButton startButton;
私人JButton stopButton;
专用BufferedImage缓冲区;
JToolBar工具栏;

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()); (int time = 1; time <9; time ++){
rndNumber = rand.nextInt(6)+1;

; //产生随机数

if(rndNumber == 2 || rndNumber == 4 || rndNumber == 6 || rndNumber == 8){
//将时间添加到队列中
untilQueue.add(String.valueOf(time));
drawNext(buffer.getGraphics());
repaint();




$ b public class StartActionHandler实现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);
颜色randomColour =新颜色(红色,绿色,蓝色);
g.setColor(randomColour);
g.fillRect(x,y,10,10);
repaint();
}

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


解决方案

为了方便,使用缓冲区的 createGraphics() c $ c>方法和 dispose()完成。 初始化屏幕外的 buffer 为一个已知状态。

  • 一个随机通常是足够的。


  • 尽可能限制变量范围,例如 private class TimerEvent


  • 覆盖 getPreferredSize() 以建立渲染区域大小。






  • 经测试:

      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;

    / **
    * @ https 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(){
    $ b $ @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> untilQueue = new LinkedList<>();
    私人定时器定时器;
    私人JButton startButton;
    私人JButton stopButton;
    专用BufferedImage缓冲区;
    私人JToolBar工具栏;

    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(工具栏,BorderLayout.SOUTH);

    $ b $ private class TimerEvent implements ActionListener {
    $ b $ @Override
    public void actionPerformed(ActionEvent e){
    for(int time = 1; time <9; time ++){
    if(r.nextInt(6)%2 == 0){
    untilQueue.add(String.valueOf(time));
    drawNext();




    $ b Private class StartActionHandler实现了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);

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


    I am working on a project which is a Checkout Simulation. I have the code to make it work run but i am struggling to understand and implement how to add graphics(in my case a square) once a certain condition is true. For example i have made my code so that it goes through random numbers and if 2,4,6 or 8 has been randomly generated, someone will be added to the queue and the same goes for if they are even numbers 1 or 3, someone is removed from the queue. I basically just want to know how to add a square to the screen once i have met my condition (for example, generating a 4 should add a square to the screen but it doesn't) ANY help would really be appreciated!

    public class MainPanel extends 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);
        }
    }
    

    解决方案

    Note several changes to get rendering working:

    • For convenience, use the buffer's createGraphics() method and dispose() it when done.

    • Initialize the offscreen buffer to a known state.

    • One instance of Random is usually sufficient.

    • Limit variable scope to the extent possible, e.g. private class TimerEvent.

    • Override getPreferredSize() to establish the rendering area size.

    As tested:

    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图形签出模拟;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆