2016-12-02 76 views
0

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

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

我的老师收录了一条提示:在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); 
} 

注释的东西就是东西,我们在课堂上走过去。我不知道这是否会帮助我。

请不要复杂的东西。这只是我编程的第二年,我还不熟练。

回答

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 

我不能说你打算如何让这条线再次消失。你打算画线并擦除它们吗?

+0

他希望我们这样做的方式,我们应该画白线,然后用黑线画出它们。 – Aero

+0

是的,在这种情况下,您可以使用重载的构造函数或setSeed方法轻松地为随机对象创建相同的值。但我认为你应该问你的老师澄清它的用例。因为在白线之间绘制黑线时,随机绘制白线并没有任何内容,所以不会带出可能很明显的用例!如果老师试图提供的唯一一个教训是使用相同的种子,那么将生成相同的随机数字序列,然后可能需要使用更好的用例。这就是我个人的想法! –

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

    } 
}