如何绘制连接的随机线?

时间:2016-12-02 13:35:14

标签: random

我需要在我拥有的程序中绘制随机行。我已经弄清楚如何使用Random类绘制Random行,但我无法弄清楚如何使它们连接。该课程的第二部分是在白色上绘制黑线以“使它们消失”。

基本上,如果line1有坐标(0,0,300,300),那么第二行应该有坐标(300,300,随机数随机数)。我无法弄清楚如何使用Random类来实现这一点。

我的老师提供了一个提示:在随机类“种子”的参数中插入一个非随机的列表。使用相同的种子再次调用Random类后,将显示相同的列表。我不知道如何实现这一点,黑色线条将完全覆盖白线。

到目前为止,这是我的代码:

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);
}

评论的内容只是我们在课堂上讨论的内容。我不知道这是否会对我有所帮助。

拜托,没有复杂的东西。这只是我编程的第二年,我还不熟练。

2 个答案:

答案 0 :(得分:1)

除非您不需要再次生成x1,y1,而是为其指定较旧的值,否则您的操作是正确的。初始随机x1,y1将在进入循环绘制线之前预先计算。下面的代码可以为您提供见解。

    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

我不知道你怎么打算让这些线条再次消失。你打算画线并擦掉它们吗?

答案 1 :(得分:0)

这应该是工作版:) 我认为你老师的目标是让你理解Random课程的工作原理。

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();
            }
        }

    }
}