Results 1 to 7 of 7
  1. #1
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Creating a 'turn' method

    I have a project that calls for me to create a moving bug. It only needs to move as its called to in the code, no input is required. We're supposed to create a Bug class, then a turn, move, and getPosition method. I think I've pretty much got everything except for the turn part. I could create a method that defines which direction the "Bug" is set to move, but I don't know how to turn the "Bug" drawing around to face another direction. We're only moving horizontally.

    Here's the problem:
    Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Provide a constructor
    Java Code:
    public Bug(initialPosition)
    and methods
    Java Code:
    public void turn()
    public void move()
    public int getPosition()
    Sample usage:
    Java Code:
    Bug bugsy = new Bug(10);
    bugsy.move();
    bugsy.turn();
    bugsy.move();

    What I have is incomplete (not sure if the parameters are right to draw the bug), but I think I can handle it once I figure out what to do for the "turn" method.
    Last edited by StateofMind; 02-22-2010 at 02:49 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose your bug is at position x; if it moves (horizontally) it moves to position x+dx; if it had moved in the opposite direction it had moved to position x+(-dx) == x-dx. So all you have to do is keep track of a position x and a direction dx; if the bug has to take a (180 degrees) turn simply set dx= -dx.

    kind regarrds,

    Jos

  3. #3
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks for your help, I guess I was overthinking it.

  4. #4
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    I'm still not able to get the 'Bug' to actually make a 180 degree turn and face the opposite direction. All I'm able to do is get it to move left or right (+(-dx) or +(dx)).

  5. #5
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    Right now I only have two files. A Bug.java source for the 'Bug' class, and a BugTester.java for main. I'm thinking that maybe I need a third file to handle the actual drawing of the bug? Whenever I multiply my X positions by negative 1 they disappear. When I subtract negative number from my X positions...they just move left, as if the bug is moving backwards. How do I get him to actually turn?

    Here's my code so far:

    Bug.java
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    //create Bug class
    public class Bug extends JComponent
    {
    	//create Bug constructor
    	public Bug(int initialPosition)
    	{
    		x2 = initialPosition;
    		direction = 1;
    
    	}
    	
    	//methods for Bug movement
    	public void turn() //turn Bug around
    	{
    		switch (direction)
    		{
    		case 1: dx = 1;
    		case 2: dx = -1;
    		}
    		
    		switch (direction)
    		{
    		case 1: direction++;
    		case 2: direction--;
    		}
    		
    	}
    	public void move() //move Bug horizontally
    	{
    		getPosition();
    		getDirection();
    		if(dx > 0)
    		{
    			x2++; //move Bug right
    		}
    		if(dx < 0)
    		{
    			x2--; //move Bug left
    		}
    	}
    	public int getPosition() //return Bug position
    	{
    	
    	return x2;
    	}
    	
    	public int getDirection()
    	{
    		
    	return dx;
    	}
    	
    	public int x2;
    	public int dx;
    	public int direction;
    	
    	//create Bug
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		
    		
    		//create Bug body
    		Ellipse2D.Double body = new Ellipse2D.Double(300+getPosition(),300,80,45);
    		g2.draw(body); //draw Bug body
    		g2.setColor(Color.GREEN); 
    		g2.fill(body); //color Bug body
    		g2.setColor(Color.BLACK); //reset color
    		
    		//create Bug head
    		Ellipse2D.Double head = new Ellipse2D.Double(380+getPosition(),312,25,20);
    		g2.draw(head); //draw Bug head
    		g2.setColor(Color.GREEN);
    		g2.fill(head); //color Bug head
    		g2.setColor(Color.BLACK); //reset color
    		
    		
    		//create Bug legs
    		//first set
    		Line2D.Double leg1_1 = new Line2D.Double(365+getPosition(),295,365+getPosition(),304);
    		g2.draw(leg1_1);
    		Line2D.Double leg1_2 = new Line2D.Double(365+getPosition(),340,365+getPosition(),349);
    		g2.draw(leg1_2);
    		//second set 
    		Line2D.Double leg2_1 = new Line2D.Double(341+getPosition(),292,341+getPosition(),299);
    		g2.draw(leg2_1);
    		Line2D.Double leg2_2 = new Line2D.Double(341+getPosition(),345,341+getPosition(),352);
    		g2.draw(leg2_2);
    		//third set
    		Line2D.Double leg3_1 = new Line2D.Double(320+getPosition(),295,320+getPosition(),303);
    		g2.draw(leg3_1);
    		Line2D.Double leg3_2 = new Line2D.Double(320+getPosition(),342,320+getPosition(),350);
    		g2.draw(leg3_2);
    		
    		//create Bug antennae
    		Line2D.Double antenna1 = new Line2D.Double(403+getPosition(),315,407+getPosition(),315);
    		g2.draw(antenna1);
    		Line2D.Double antenna2 = new Line2D.Double(403+getPosition(),329,407+getPosition(),329);
    		g2.draw(antenna2);
    		
    		
    	}	
    }
    BugTester.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class BugTester 
    {
    
    	public static void main(String[] args) 
    	{
    	      JFrame frame = new JFrame();
    	      frame.setSize(700, 700);
    	      frame.setTitle("P3.9");
    	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	      Bug bobthebug = new Bug(0);
    	      frame.add(bobthebug);
    
    	      frame.setVisible(true);
    		  
    	}
    
    }

  6. #6
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok, I got it to work but I really don't think this is the way it should be done. Here's what I have:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    //create Bug class
    public class Bug extends JComponent
    {
    	//create Bug constructor
    	public Bug(int initialPosition)
    	{
    		x2 = initialPosition;
    		direction = 1;
    
    	}
    	
    	//methods for Bug movement
    	public void turn() //turn Bug around
    	{
    		switch (direction)
    		{
    		case 1: dx = 0;
    		case 2: dx = 1;
    		}
    		
    		switch (direction)
    		{
    		case 1: direction++;
    		case 2: direction--;
    		}
    		
    	}
    	public void move() //move Bug horizontally
    	{
    		getPosition();
    		getDirection();
    		if(dx == 0)
    		{
    			x2++; //move Bug right
    		}
    		if(dx == 1)
    		{
    			x2--; //move Bug left
    		}
    	}
    	public int getPosition() //return Bug position
    	{
    	
    	return x2;
    	}
    	
    	public int getDirection()
    	{
    		
    	return dx;
    	}
    	
    	public int x2;
    	public int dx;
    	public int direction;
    	
    	//create Bug
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		
    		
    		//create Bug body
    		Ellipse2D.Double body = new Ellipse2D.Double(300+getPosition(),300,80,45);
    		g2.draw(body); //draw Bug body
    		g2.setColor(Color.GREEN); 
    		g2.fill(body); //color Bug body
    		g2.setColor(Color.BLACK); //reset color
    		
    		//create Bug head
    		Ellipse2D.Double head = new Ellipse2D.Double(380-(dx*105)+getPosition(),312,25,20);
    		g2.draw(head); //draw Bug head
    		g2.setColor(Color.GREEN);
    		g2.fill(head); //color Bug head
    		g2.setColor(Color.BLACK); //reset color
    		
    		
    		//create Bug legs
    		//first set
    		Line2D.Double leg1_1 = new Line2D.Double(365+getPosition(),295,365+getPosition(),304);
    		g2.draw(leg1_1);
    		Line2D.Double leg1_2 = new Line2D.Double(365+getPosition(),340,365+getPosition(),349);
    		g2.draw(leg1_2);
    		//second set 
    		Line2D.Double leg2_1 = new Line2D.Double(341+getPosition(),292,341+getPosition(),299);
    		g2.draw(leg2_1);
    		Line2D.Double leg2_2 = new Line2D.Double(341+getPosition(),345,341+getPosition(),352);
    		g2.draw(leg2_2);
    		//third set
    		Line2D.Double leg3_1 = new Line2D.Double(320+getPosition(),295,320+getPosition(),303);
    		g2.draw(leg3_1);
    		Line2D.Double leg3_2 = new Line2D.Double(320+getPosition(),342,320+getPosition(),350);
    		g2.draw(leg3_2);
    		
    		//create Bug antennae
    		Line2D.Double antenna1 = new Line2D.Double(403-(dx*130)+getPosition(),315,407-(dx*130)+getPosition(),315);
    		g2.draw(antenna1);
    		Line2D.Double antenna2 = new Line2D.Double(403-(dx*130)+getPosition(),329,407-(dx*130)+getPosition(),329);
    		g2.draw(antenna2);
    		
    		
    	}	
    }
    Last edited by StateofMind; 02-22-2010 at 02:42 AM.

  7. #7
    Rubas is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Which method is best for creating Threads?
    By makpandian in forum Threads and Synchronization
    Replies: 11
    Last Post: 06-08-2009, 08:00 AM
  2. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  3. Creating a GUI - which method
    By matpj in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:40 PM
  4. Can't turn on breadcrumbs in enclipse
    By Jubei1980 in forum Eclipse
    Replies: 0
    Last Post: 11-24-2008, 10:08 PM
  5. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 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
  •