# Thread: Image between 2 points.

1. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Image between 2 points.

Is there any image drawing command where i can draw image between 2 points. I want to draw a lightning bolt in different angles. I have the object from whitch the lightning will come, and i have the objects to where the lightning must go. All of these are moveing objects. How do i draw a lightnign bolt between them?

2. ## Re: Image between 2 points.

3. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Well, i did not find what i was looking for.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,929
Rep Power
10

## Re: Image between 2 points.

Drawing an image between two points is easy. The difficulty, IMHO, is to properly maintain the aspect ratio and scaling to make the bolt look realistic(not to fat or thin when the points change proximity to one another).

My approach would be to write a lightning bolt generator using some algebra and/or trig to get the jagged edges correct.

Regards,
Jim

5. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

i see. il look into it, and see whati can find and make :D

6. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

I have a ArrayList<Point> lightning. Each member of the list is a Point(X,y). How can i use my arraylist to draw line. g.drawLine(lightning.get(i), lightning.get(i+1)) doesent work.

SOLVED:
Just needed to use getX() and getY() :D
Last edited by vastrolorde; 06-28-2013 at 04:01 PM. Reason: Solved

7. ## Re: Image between 2 points.

Assuming g is Graphics, have a look here:

Graphics (Java Platform SE 6)

Look up the syntax for the drawLine method.

edit: There might even be another method you can use to draw the lines.
Last edited by Toll; 06-28-2013 at 04:03 PM.

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,929
Rep Power
10

## Re: Image between 2 points.

What method are you calling? The Graphics API does not offer a drawLine method which takes points. You must use
g.drawLine(x1,y1,x2,y2). Did your program even compile?

Regards,
Jim

9. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

I have arraylist with 2 points in it. LEts call em start and end. I want to ad a new point called mid in between them. THen i want to add new points between start and mid, mid and end, and so on. How to i but it into a loop?

10. ## Re: Image between 2 points.

Path2D.Double

db

11. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Java Code:
```lightning.add(start);
int offset = offmax;
for(int x = 0; x < 3; x++){
int j = lightning.size();
System.out.println(random.nextInt(offset));
for(int i = 0; i < j; i++){
if(i+1 < j){
Point mid = new Point((int)((lightning.get(i).getX() + lightning.get(i+1).getX())/2),
(int)((lightning.get(i).getY() + lightning.get(i+1).getY())/2));

mid = new Point((int)(mid.getX() + random.nextInt(offset)),
(int)(mid.getY() + random.nextInt(offset)));

}
}
offset /= 2;
}```
This is the code generating the points for lightning.
Problem is adding the point i calculate later on. I dont have a clue how to find the formula for butting the new point int to the corresponding locations.
Last edited by vastrolorde; 06-28-2013 at 10:14 PM.

12. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Java Code:
```lightning.add(start);
int offset = offmax;
for(int x = 0; x < 3; x++){
System.out.println(random.nextInt(offset));
for(int i = 0; i < lightning.size(); i++){
if(i+1 < lightning.size()){
Point mid = new Point((int)((lightning.get(i).getX() + lightning.get(i+1).getX())/2),
(int)((lightning.get(i).getY() + lightning.get(i+1).getY())/2));

mid = new Point((int)(mid.getX() + randoms(offset)),
(int)(mid.getY() + randoms(offset)));

}
}
lightning.clear();
for(int j = 0; j < buffer.size(); j++){
}
offset /= 2;
}
}```
This is the code atm. X represents the generations. 2gen gives a normal line, with 3 bend points. 3gen starts giving weird results.
Last edited by vastrolorde; 06-29-2013 at 07:45 PM.

13. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Java Code:
```lightning.add(new Line2D.Double(start, end));
int offset = offmax;
for(int x = 0; x < 1; x++){
int j = lightning.size();
//System.out.println(j);
for(int i = 0; i < j; i++){
System.out.println(i);
start = new Point2D.Double(lightning.get(i).getX1(), lightning.get(i).getY1());
end = new Point2D.Double(lightning.get(i).getX2(), lightning.get(i).getY2());

Point2D.Double mid = new Point2D.Double((start.getX() + end.getX())/2 ,
(start.getY() + end.getY())/2 );

mid = new Point2D.Double(mid.getX() + randoms(offset),
mid.getY() + randoms(offset));

lightning.clear();

}
}
offset /= 2;```
Why does the second loop run 4 times?
Last edited by JosAH; 07-01-2013 at 04:12 PM.

14. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,929
Rep Power
10

## Re: Image between 2 points.

Did you print out j before, during, and at the end of the loop?

Regards,
Jim

15. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Yes. i found the problem and fixed it.
Java Code:
```lightning.add(new Line2D.Double(start, end));
int offset = offmax;
for(int x = 0; x < 4; x++){
for(int i = 0; i < lightning.size(); i++){
start = new Point2D.Double(lightning.get(i).getX1(), lightning.get(i).getY1());
end = new Point2D.Double(lightning.get(i).getX2(), lightning.get(i).getY2());

Point2D.Double mid = new Point2D.Double((start.getX() + end.getX())/2 ,
(start.getY() + end.getY())/2 );

mid = new Point2D.Double(mid.getX() + randoms(offset),
mid.getY() + randoms(offset));

}
lightning.clear();
for(int j = 0; j < buffer.size(); j++){
buffer.get(j).getX2(), buffer.get(j).getY2()));
}
buffer.clear();
}
offset /= 2;```
This is a working code, but it still doesent generate correctly. It has a 50:50 chance of generatign a normal lightning. Other 50 is some weir jagged line that does loops. THe highr the x vlue the lower the chance for a normal lighning bolt.
Offset begins out as 50.
Last edited by vastrolorde; 06-30-2013 at 08:11 PM.

16. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

Anyway to make it to generate normal bolts? And the other thing. I have this in cycle. But only 1st bolt is normal, goes from start to end. NExt ones stick to the right side of the window. Why is that?
Last edited by vastrolorde; 07-01-2013 at 03:56 PM.

17. Senior Member
Join Date
Jan 2013
Posts
124
Rep Power
0

## Re: Image between 2 points.

How can i make this code work with any offset, while the x is 8. Atm offset is
Java Code:
```private int offmax = 40;
private final int DELAY = 100;

public Board() {
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
ingame = true;

setSize(400, 300);

lightning = new ArrayList<Line2D.Double>();
buffer = new ArrayList<Line2D.Double>();
buffer2 = new ArrayList<Line2D.Double>();
random = new Random();
valk = new Color(0,213,255);
lai = new BasicStroke(2);
ball = new Energyball(100,100);
End = new ArrayList<Point2D.Double>();

for(int z = 0; z < 5; z++){
}

}

B_WIDTH = getWidth();
B_HEIGHT = getHeight();
animator.start();
}

public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
if (ingame) {
g2.setColor(valk);
g2.setStroke(lai);
for(int i = 0; i < lightning.size(); i++){
g2.drawLine((int)lightning.get(i).getX1(), (int)lightning.get(i).getY1(),
(int)lightning.get(i).getX2(), (int)lightning.get(i).getY2());
}
g2.drawImage(ball.getFrame(), ball.getX(), ball.getY(), this);
}

Toolkit.getDefaultToolkit().sync();
g2.dispose();
}

public double randoms(int x){
return random.nextInt(2*x) - x;
}

public void cycle() {
lightning.clear();
bstart = new Point2D.Double(ball.getX()+ 8, ball.getY()+ 9);
for(int h = 0; h < End.size(); h ++){
bend = new Point2D.Double(End.get(h).getX(), End.get(h).getY());
int offset = offmax;
for(int x = 0; x < 6; x++){
for(int i = 0; i < buffer2.size(); i++){
Point2D.Double start = new Point2D.Double(buffer2.get(i).getX1(), buffer2.get(i).getY1());
Point2D.Double end = new Point2D.Double(buffer2.get(i).getX2(), buffer2.get(i).getY2());

Point2D.Double mid = new Point2D.Double((start.getX() + end.getX())/2 ,
(start.getY() + end.getY())/2 );

mid = new Point2D.Double(mid.getX() + randoms(offset),
mid.getY() + randoms(offset));

}
buffer2.clear();
for(int j = 0; j < buffer.size(); j++){
buffer.get(j).getX2(), buffer.get(j).getY2()));
}
buffer.clear();
offset /= 1.8;
}
for(int j = 0; j < buffer2.size(); j++){
buffer2.get(j).getX2(), buffer2.get(j).getY2()));
}
buffer2.clear();
}
}```
the offet is 40. If i vant torais x to 7 i must raise the offset. I want to keep the offset but i want to raise te x to 8. But then it doesetn work anymore.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•