Results 1 to 2 of 2
  1. #1
    burakesme is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default How To Move An Object On A Random Path

    Hi all. I am newbie on java and these forums. Here is the deal; i should move an object (a camel) on a random line. The code is above. The camel is walking but i want it to move on the line. When i change the line, camel should follow the line.

    Gif image is attached. If you specify the pat of this gif, you can see.

    LineUnder.java ------------------------------------------------------------
    Java Code:
    /**
     *
     * @author burak.esme
     */
    import java.awt.*;
    import java.awt.geom.Line2D;
    import java.awt.geom.PathIterator;
    import javax.swing.*;
    
    public class LineUnder extends JFrame {
    	LinePanel linePanel;
    	final static Camel camel = new Camel();
    	Line2D line;
    
    	LineUnder() {
    		// new Thread(new RunAnimate(camel)).start();
    		// new Thread(new RunMove(camel)).start();
    		
    		linePanel = new LinePanel();
    		linePanel.line.setLine(0d, 0d, 300d, 300d);
    		initContentPane();
    		pack();
    		setLocationRelativeTo(null);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	}
    	private static String floatStr(float[] coords) {
            String s = "";
            for (int i = 0; i < coords.length; i++) {
                s += coords[i] + " ";
    
            }
            return s;
    
        }
    	
    	private void initContentPane() {
    		
    		getContentPane().add(linePanel);
    		linePanel.add(camel);
    
    		camel.flag = true;
    		camel.flag2 = true;
    	}
    
    	public static void main(String[] args) throws InterruptedException {
    		
    		float[] coords = new float[6];
    		//int type = pathiterator.currentSegment(coordinates);
    		LineUnder lu=new LineUnder();
    		
    		//PathIterator pi=lu.linePanel.line.getPathIterator(null);
    		camel.setLocation(0, 0);
    		//lu.linePanel.line.
    		/* while (!pi.isDone()) { 
    			 int type = pi.currentSegment(coords); 
    			 if (type== PathIterator.SEG_LINETO) 
    			 { 			 
    				 System.err.println("type :"+type+" "+floatStr(coords));
    				 new Thread(new RunMove(camel,coords[0], coords[1])).start();
    		  
    		  
    		  
    		  } pi.next(); 
    		  System.err.println("type :"+type+" "+floatStr(coords));
    		 }*/
    		double a=0,b=0;
    		
    		new Thread(new RunMove(camel)).start();
    		
    		
    		 
    		
    		//new Thread(new RunMove(camel)).start();
    
    	}
    }
    
    class LinePanel extends JPanel {
    	Point lineStart = new Point(0, 0);
    	Point lineEnd = new Point(0, 0);
    	public Line2D line = new Line2D.Double(lineStart, lineEnd);
    
    	//boolean a = false;
    
    	// width of line
    	private Stroke stroke = new BasicStroke(2.0f);
    
    	LinePanel() {
    		super();
    
    	}
    
    	// draw and delete line
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setStroke(stroke);
    
    		g2.setColor(Color.RED);
    		// draw shape of line
    		g2.draw(line);
    
    	}
    
    	// define panel's size
    
    	public Dimension getPreferredSize() {
    		return new Dimension(400, 350);
    	}
    
    	public Line2D getLine() {
    		return line;
    	}
    
    	public void setLine(Line2D line) {
    		this.line = line;
    	}
    	
    }
    
    class RunAnimate implements Runnable {
    	public Camel c;
    
    	public RunAnimate(Camel c) {
    		this.c = c;
    	}
    
    	@Override
    	public void run() {
    		c.animate();
    
    	}
    }
    
    class RunMove implements Runnable {
    	public Camel c;
    	
    
    	public RunMove(Camel c) {
    		this.c = c;
    		
    	}
    
    	@Override
    	public void run() {
    
    		c.move();
    
    	}
    }


    Camel.java----------------------------------------------------------------
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.geom.Line2D;
    import java.awt.geom.PathIterator;
    
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Camel extends JLabel {
    	private Icon icon, icon2;
    	public boolean flag = false;
    	public boolean flag2 = false;
            int type;
            PathIterator pi;
    
    	public Camel() {
    		// TODO Auto-generated constructor stub
    
    		icon = new ImageIcon("C://Camelia.gif");
    		//icon2 = new ImageIcon("C://Adam2.gif");
    		setIcon(icon);
    	}
    
    	public void animate() {
    		while (flag) {
    			
    				setIcon(icon);
    
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
    	}
    
    
    
            public void move() {
    		while (flag2) {
    			Point pt = getLocation();
    
    			pt.x+=5;
    			setLocation(pt);
    			try {
    				Thread.sleep(750);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
                //FlatteningPathIterator fp= new FlatteningPathIterator(null, WIDTH);
    	}
    
            public void stop(){
                Point pt = getLocation();
                setLocation(pt);
    
                
            }
    
    
    }

    Moderator Edit: Quote tags changed into Code tags
    Attached Thumbnails Attached Thumbnails How To Move An Object On A Random Path-camelia.gif  
    Last edited by Fubarable; 05-23-2010 at 05:21 PM. Reason: Moderator Edit: Quote tags changed into Code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and changed your quote tags into code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

Similar Threads

  1. Help Using KeyListener to Move an Object
    By jjonesmu in forum New To Java
    Replies: 3
    Last Post: 04-20-2010, 10:35 PM
  2. Replies: 1
    Last Post: 04-20-2010, 10:31 PM
  3. setting class-path & Library Path in ubantu
    By programmer_007 in forum Eclipse
    Replies: 18
    Last Post: 02-22-2010, 12:31 PM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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