Random Drawing coordinates

Hey I need to write a code that would be randomly generating coordinates for squares, circles and so on. I tried it that way (I skipped Main class):

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Oval extends JPanel {

        Random random1 = new Random(100);
        Random random2 = new Random(100);
        Random random3 = new Random(100);
        Random random4 = new Random(100);

        int x1 = random1.nextInt();
        int x2 = random2.nextInt();
        int x3 = random3.nextInt();
        int x4 = random4.nextInt();

        protected void paintComponent(Graphics g) 
        {
            super.paintComponent(g);      

            //oval
            g.setColor(Color.blue);
            g.fillOval(30, 40, 20, 20);

            //rectangle
            g.setColor(Color.red);
            g.fillRect(x1, x2, x3, x4);

            //triangle
            int xpoints[] = {165, 145, 205, 145, 145};
            int ypoints[] = {65, 125, 115, 145, 105};
            int npoints = 3;
            g.setColor(Color.pink);
            g.fillPolygon(xpoints, ypoints, npoints);

            //trapezoid
            int trxpoints[] = {265, 285, 305, 245, 245};
            int trypoints[] = {165, 165, 215, 225, 205};
            int trnpoints = 4;
            g.setColor(Color.black);
            g.fillPolygon(trxpoints, trypoints, trnpoints);  
        }
 }

I filled rectangle' coordinates with random numbers, but in the end it fills my whole JFrame with a color reserved for a rectangle. What is wrong?

Answers


First of all:

    Random random1 = new Random(100);
    Random random2 = new Random(100);
    Random random3 = new Random(100);
    Random random4 = new Random(100);

This gives you identical sequences for every random* variable.

Secondly:

nextInt method:

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 2^32 possible int values are produced with (approximately) equal probability

It gives you values that are really big (much bigger than your frame)

I suggest doing as follows:

random.nextInt(this.getWidth()), random.nextInt(this.getHeight())

Why does this happens? This happens because you are creating 4 instances of Random with the same seed. You are passing 100 (same seed) as argument when you initialize the Random objects. This seed is useful to repeat random calls. In your case, each Random object will return the same random sequence when a method is called. If you want to try this yourself, create two Random instances with the same seed and try calling and printing the result f some methods.

How to solve this? Just create one, and call nextInt() for each variable x1, x2. Also, I would recommend you to do this (initialization) in the constructor:

Random random1;
int x1;
int x2;
int x3;
int x4;

public Test()
{
    random1 = new Random(100); // Give a seed if you want to run 
                               // the program with same random sequence each time
                               // If you want different sequences each time
                               // you run the program use 'new Random()'
    x1 = random1.nextInt();
    x2 = random1.nextInt();
    x3 = random1.nextInt();
    x4 = random1.nextInt();
}

Need Your Help

How to make Coding Simple Request Form Validation

javascript forms validation registration

Having to make a Request Form from scratch and a little on confused on what all i need to make it work. Here is the HTML I'm having to use and the javacode is what i've put together by scratch, its