2016-12-02 1 views
0

나는 가지고있는 프로그램에 임의의 줄을 그려야한다. Random 클래스를 사용하여 임의 선을 그리는 방법을 알아 냈습니다. 그러나이를 연결하는 방법을 알 수는 없습니다. 수업의 두 번째 부분은 흰색 줄 위에 검은 선을 그어 "사라지게"하는 것입니다. 라인 1은 (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); 
} 

주석으로 물건을 우리가 수업 시간에 가서 그냥 가지입니다 :

여기에 지금까지 내 코드입니다. 이게 내가 도움이 될지 모르겠다.

제발, 복잡한 것들. 이것은 프로그래밍의 두 번째 해에 불과하며 아직 익숙하지 않습니다.

답변

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

    } 
} 
관련 문제