Aero
Aero

Reputation: 1

How do I draw Random Lines that are connected?

I need to draw random lines in a program that I have. I have figured out how to draw the Random lines, using the Random class, but I can't figure out how to make them connect. The second part of the class is to draw black lines over the white ones to "make them disappear".

Basically, if line1 has coordinates (0,0,300,300), then the second line should have the coordinates (300,300,random number, random number). I can't figure out how to make this happen using the Random class.

My teacher included a hint: Inserting a number into the parameters of the Random class "seeds" a list that is not random. Once you call the Random class again with that same seed, then the same list will appear. I don't know how to implement this so that the black lines will completely cover the white lines.

Here is my code so far:

public void paint(Graphics g)
{
    super.paint(g);
    g.setColor(Color.black);
    g.fillRect(0, 0, 600, 600);
    Point2D.Double one = new Point2D.Double(50,50);
    Point2D.Double two = new Point2D.Double(300,300);

    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(new BasicStroke(5));

    g.setColor(Color.white);
    for (int i = 0; i < 10; i++)
    {

        Random gen = new Random();
        /*one.setLocation(gen.nextInt(600), gen.nextInt(600));
        two.setLocation(gen.nextInt(600), gen.nextInt(600));
        g.drawLine((int)one.getX(), (int)one.getY(), (int)two.getX(), (int)two.getY());*/
        int x1 = gen.nextInt(600);
        int y1 = gen.nextInt(600);
        int x2 = gen.nextInt(600);
        int y2 = gen.nextInt(600);
        g.drawLine(x1, y1, x2, y2);
        x1 = x2;
        y1 = y2;
        try 
        {
            Thread.currentThread().sleep(300);
        }
        catch (InterruptedException e)
        {
                e.printStackTrace();
        }
    }

    /*for (int i = 0; i < 10; i++)
    {
        g.setColor(Color.BLACK);
        Random gen = new Random(1);
        one.setLocation(gen.nextInt(600), gen.nextInt(600));
        two.setLocation(gen.nextInt(600), gen.nextInt(600));
        g.drawLine((int)one.getX(), (int)one.getY(), (int)two.getX(), (int)two.getY());
        try 
        {
            Thread.currentThread().sleep(300);
        }
        catch (InterruptedException e)
        {
                e.printStackTrace();
        }
    }*/

}

public static void main(String[] args) 
{
    /*int x = 0;
    for (int i = 0; i < 20; i++)
    {
        x = x + 1;
        Random gen = new Random(x);
        System.out.println(gen.nextInt(100));
    }*/
    PointsAndLines application = new PointsAndLines();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

The commented stuff is just things that we went over in class. I don't know if this will help me.

Please, no complicated stuff. This is only my second year of programming, and I'm not adept at it yet.

Upvotes: 0

Views: 282

Answers (2)

Blaatz0r
Blaatz0r

Reputation: 1205

This should be the working version :) I think the goal of your teacher is to make you understand the workings of the Random class.

package test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class PointsAndLines extends JFrame
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int seed;

    public static void main(String[] args) 
    {
        PointsAndLines application = new PointsAndLines(12); // <- upon changing the seed a different pattern will emerge
        application.setVisible(true);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public PointsAndLines(int seed)
    {
        this.seed = seed;       
        setBounds(100, 100, 600, 600);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.black);
        g.fillRect(0, 0, 600, 600);

        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(5));

        g.setColor(Color.white);

        Random gen = new Random(seed); // apply the seed

        int x1 = gen.nextInt(600);
        int y1 = gen.nextInt(600);

        for (int i = 0; i < 10; i++)
        {   
            int x2 = gen.nextInt(600);
            int y2 = gen.nextInt(600);

            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
            try 
            {
                Thread.sleep(300);
            }
            catch (InterruptedException e)
            {
                    e.printStackTrace();
            }
        }

        Random genTwo = new Random(seed); // <- apply the same seed again

        x1 = genTwo.nextInt(600);
        y1 = genTwo.nextInt(600);

        for (int i = 0; i < 10; i++)
        {
            g.setColor(Color.BLACK);

            int x2 = genTwo.nextInt(600);
            int y2 = genTwo.nextInt(600);

            g.drawLine(x1, y1, x2, y2);

            x1 = x2;
            y1 = y2;

            try 
            {
                Thread.sleep(300);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

    }
}

Upvotes: 0

Arjun Lajpal
Arjun Lajpal

Reputation: 308

You are doing it correct except that you don't need to generate x1,y1 again but assign it the older value. The initial random x1, y1 will be calculated beforehand, before getting inside the loop to draw lines. Below code may give you an insight.

    Random gen = new Random();
    int x1 = gen.nextInt(600);
    int y1 = gen.nextInt(600);//We get the first x1, y1 random values here itself
    for (int i = 0; i < 10; i++)
{

    int x2 = gen.nextInt(600);
    int y2 = gen.nextInt(600);
    g.drawLine(x1, y1, x2, y2);//Once we draw the line we assign x2, y2 to x1, y1 as you did below
    x1 = x2;
    y1 = y2;
    //Now the next time you enter this loop your line will start from where the line had ended and next point will be random
    //rest of the code goes below

I can't say how do you plan to make the lines disappear again. Do you intend to draw the lines and erase them?

Upvotes: 1

Related Questions