Results 1 to 17 of 17
Like Tree2Likes
  • 2 Post By jim829

Thread: Image between 2 points.

  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Image between 2 points.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Image between 2 points.

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

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default 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
    DarrylBurke and Fubarable like this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Image between 2 points.

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

  6. #6
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Image between 2 points.

    Path2D.Double

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Image between 2 points.

    Java Code:
    lightning.add(start);
    		lightning.add(end);
    		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)));
    				
    				
    				
    				lightning.add(mid);
    				}
    			}
    			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. #12
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Image between 2 points.

    Java Code:
    lightning.add(start);
    		lightning.add(end);
    		buffer.add(start);
    		buffer.add(end);
    		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)));
    				
    				
    				
    					if(i == 0){ buffer.add(1, mid);}
    					else{ buffer.add(3 +(i-1)*2, mid);}
    				}
    			}
    			lightning.clear();
    				for(int j = 0; j < buffer.size(); j++){
    					lightning.add(buffer.get(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. #13
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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();
    					lightning.add(new Line2D.Double(start, mid));
    					lightning.add(new Line2D.Double(mid, end));
    				
    				}
    			}
    			offset /= 2;
    Why does the second loop run 4 times?
    Last edited by JosAH; 07-01-2013 at 04:12 PM.

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,647
    Rep Power
    5

    Default Re: Image between 2 points.

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

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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));
    				
    					buffer.add(new Line2D.Double(start, mid));
    					buffer.add(new Line2D.Double(mid, end));
    				
    				}
    				lightning.clear();
    				for(int j = 0; j < buffer.size(); j++){
    					lightning.add(new Line2D.Double(buffer.get(j).getX1(), buffer.get(j).getY1(),
    										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. #16
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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. #17
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default 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++){
    			End.add(new Point2D.Double(random.nextInt(300), random.nextInt(300)));
    			}
    		
        }
    
        public void addNotify() {
            super.addNotify();
            B_WIDTH = getWidth();
            B_HEIGHT = getHeight();
    		animator = new Thread(this);
    		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());
    			buffer2.add(new Line2D.Double(bstart, bend));
    			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));
    					
    						buffer.add(new Line2D.Double(start, mid));
    						buffer.add(new Line2D.Double(mid, end));
    					
    					}
    					buffer2.clear();
    					for(int j = 0; j < buffer.size(); j++){
    						buffer2.add(new Line2D.Double(buffer.get(j).getX1(), buffer.get(j).getY1(),
    											buffer.get(j).getX2(), buffer.get(j).getY2()));
    						}
    					buffer.clear();
    					offset /= 1.8;
    				}
    				for(int j = 0; j < buffer2.size(); j++){
    						lightning.add(new Line2D.Double(buffer2.get(j).getX1(), buffer2.get(j).getY1(),
    											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.

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  2. Java Points help
    By Jcbconway in forum Advanced Java
    Replies: 30
    Last Post: 10-27-2010, 12:25 AM
  3. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

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