2016-12-02 71 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(); 
      } 
     } 

    } 
}