Search code examples
javarandomgraphics2d

How can I show a random number between 1 to 10 with graphics2D?


I'm thinking about how to create it, I can make it with System.out.println on a thread, it works well, but, I'm inquirring about how to show the random data each second, with the methodGraphics2D, anyone could help?

here's what I did:

String[] numbers= {"1","2", "3", "4", "5", "6", "7", "8", "9", "10"};
int random = (int) (Math.random()*numbers.length);

//here's the part I struggle
public void paint (Graphics g) {
    g2.setColor(Color.black);
    g2.drawString("The number is below", 50, 70);

    //won't run
    g2.draw(numbers, 50, 90);
}

Solution

  • You can create a panel that generates a random number from a min value to a max value.

    First off, this is incorrect. You correctly used drawString in the call right before this.

    g2.draw(numbers, 50, 90); // draw is not a method of Graphics2D
    

    Second, unless your array is going to be something other than integers, you can just display the random value as-is.

    String.format("%d", getRandomInteger()) // Display a random number as a string
    

    Lastly, in a JComponent (swing), you must override paintComponent not paint. In your example, you can just use standard graphics (g.drawString).

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g; // Optional, has more methods
    
    }
    
    import java.awt.*;
    import javax.swing.*;
    
    public class RandomNumberPanel extends JPanel {
        private static final long serialVersionUID = -4376447503776020320L;
        private int min;
        private int max;
    
        public RandomNumberPanel() {
            this(0, 10);
        }
    
        public RandomNumberPanel(int max) {
            this(0, max);
        }
    
        public RandomNumberPanel(int min, int max) {
            this.min = min;
            this.max = max;
        }
    
        public int getRandomInteger() {
            return getRandomInteger(min, max);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);
            g2.drawString("The number is below", this.getWidth() / 4, this.getHeight() / 3);
            g2.drawString(String.format("%d", getRandomInteger()), this.getWidth() / 2, this.getHeight() / 2);
        }
    
        public static final int getRandomInteger(final int max, final int min) {
            return ((int) (Math.random() * (max - min))) + min;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame mainFrame = new JFrame();
                    JPanel panel = new RandomNumberPanel(1, 10); // [1, 10]
    
                    panel.setPreferredSize(new Dimension(200, 100));
                    mainFrame.setContentPane(panel);
                    mainFrame.pack();
                    mainFrame.setLocationRelativeTo(null);
                    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    mainFrame.setVisible(true);
                }
            });
        }
    }