0

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user3008643
  • 89
  • 2
  • 8

2 Answers2

3

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.

image

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);
        }
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

How is suposed to work? when you met the condition an item is added to tillQueue, but tillQueue is never readed...

I you want to draw something you can draw it in the method paintComponent.

To draw a rectangle simply use:

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

You can iterate the tillQueue in the paintComponent method and draw the corresponding rectangles.

Brierson
  • 96
  • 4
  • Cheers for the help, bu the modified code that you have provided is what i originally had before I posted on here. Well similar code which did the same thing, but the bit that I am really struggling to understand is if the random number generated is 2, 4 or 6 i want to add a square (not just keep adding them) Also, if 3 or 5 is generated i want it to remove a square... – user3008643 Jan 22 '14 at 16:57