Java图形检验模拟;

时间:2014-01-20 11:04:35

标签: java swing graphics paintcomponent

我正在开发一个Checkout Simulation项目。我有代码使它运行,但我很难理解并实现如何在某个条件为真时添加图形(在我的情况下为正方形)。例如,我已经制作了我的代码,以便它通过随机数字,如果2,4,6或8是随机生成的,有人将被添加到队列中,如果它们是偶数1或3,那么同样如此从队列中删除。我基本上只是想知道如果我满足了我的条件,如何在屏幕上添加一个方块(例如,生成一个4应该在屏幕上添加一个方块,但它没有) 任何帮助真的很感激!

公共类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);
    }
}

2 个答案:

答案 0 :(得分:3)

注意若干更改以使渲染工作:

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

  • 将屏幕外buffer初始化为已知状态。

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

  • 尽可能限制变量范围,例如: private class 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);
        }
    }
}

答案 1 :(得分:0)

如何接受工作?当你遇到这个条件时,一个项目被添加到tillQueue,但直到Queue永远不会被... ...

我想画一些你可以在方法paintComponent中绘制的东西。

要绘制矩形,只需使用:

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawRect(int,int,int,int)

您可以在paintComponent方法中迭代tillQueue并绘制相应的矩形。