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.
Aucun commentaire:
Enregistrer un commentaire