Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java object help(should be simple)

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java object help(should be simple)

    For a reason that I cannot fathom,I am required to take a computer science course in college. I took java programming,and have been keeping up pretty well until now.I have been focusing on classes that will actually help me with my future,such as neuroscience and biomedical sciences.I need to write an object by Friday that will be given to another student.The student will then write a "test" program to see if it works.The purpose of the object is to create a "creature"(mine is a rectangle with a circle for an eye)that can turn left 90 degrees, right 90 degrees, and turn around 180 degrees. The size of the creature should be able to be adjusted by a button click. The creature should turn and move a given distance to the left and to the right,so basically when a number is input(depending on if it's negative or positive) the position of the creature on the x axis is adjusted accordingly.I wrote a test program myself to see if the object works,but all I have so far is turn right 90 degrees and size adjustment.I am honestly swamped in other classes and cannot afford to work on this. This is what I have so far.Any help would be much appreciated(plus if you need free consult from a neurologist in a decade or two I can provide it to you free of charge wink wink).

    OBJECT

    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Creature{
    	//definitions
    	private int xCoord;
    	private int yCoord;
    	private int  height;
    	private int width;
     
    	//default constructor 
    	public Creature (){
    	 xCoord=0;
    	 yCoord=0;
    	 height=0;
    	 width=0;
     
     
    	} 	//copy constructor
    	public Creature (Creature b){
    		 xCoord=b.xCoord;
    		 yCoord=b.yCoord;
    		 height=b.width;
    		 width=b.height;
     
     
    		}//normal constructor
    	public Creature(int x,int y, int w, int h){ 
    		xCoord = x;
    		yCoord = y; 
     
     
    	}
     
    	//mutators(set)
    	//accessors(get)
    	public int getXCoord() {
    		return xCoord;
    	}
     
    	public void setXCoord(int coord) {
    		xCoord = coord;
    	}
     
    	public int getYCoord() {
    		return yCoord;
    	}
     
    	public void setYCoord(int coord) {
    		yCoord = coord;
    	}
     
    	public int getHeight() {
    		return height;
    	}
     
    	public void setHeight(int height) {
    		this.height = height;
    	}
     
    	public int getWidth() {
    		return width;
    	}
     
    	public void setWidth(int width) {
    		this.width = width;
    	}
     
     
     
     
     
    	public void grow(int n){
    		width+=n;
    		height=(int)(width/2);
    	}
    	public void rotate(Graphics g,boolean b){
    	if(b){
    		g.setColor(Color.green);
    		g.fillRect(xCoord,yCoord,height, width);
    		g.drawRect(xCoord,yCoord,height,width);	
    		g.setColor(Color.black);
    		g.fillOval(xCoord+width/2,yCoord,height/3,width/6);
     
     
     
    	}else{
    		g.setColor(Color.green);
    		g.fillRect(xCoord,yCoord,width,height);
    		g.drawRect(xCoord,yCoord,width,height);
    		g.setColor(Color.black);
    		g.fillOval(xCoord,yCoord-height/3,width/6,height/3);}
     
     
     
    	}
    	public void move(Graphics g,boolean b){
    		if(b){
     
    			g.setColor(Color.green);
    			g.fillRect(xCoord+20,yCoord,height, width);
    			g.drawRect(xCoord+20,yCoord,height,width);	
    			g.setColor(Color.black);
    			g.fillOval(xCoord+width/2+20,yCoord,height/3,width/6);
     
    		}
    }	
    public void shrink(int n){
    	width-=n;
    	height=(int)(width/2);
    }
    	public void display(Graphics g){
    		g.setColor(Color.green);
    		g.fillRect(xCoord,yCoord,width,height);
    		g.drawRect(xCoord,yCoord,width,height);
    		g.setColor(Color.black);
    		g.fillOval(xCoord,yCoord-height/3,height/3,width/6);
     
    	}
     
    }





    TEST PROGRAM



    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Testcreat extends Applet implements ActionListener {
    Creature myBrick= new Creature(50,50,50,30);
    	private Button right,left;
    	Button Farther=new Button("Farther");
    	Button Closer=new Button("Closer");
    	Button Right =new Button("Rotate Right");
     
     
     
    	boolean rotated = false;
    	boolean bigHeight= false;
    	boolean moved=false;
     
    	public void init(){
    		this.setSize(500,500);
     
    		add(Farther);Farther.addActionListener(this);
    		add(Closer);Closer.addActionListener(this);
    		add(Right);Right.addActionListener(this);
     
    }
     
    	public void paint(Graphics g){
    		if(rotated){
    			myBrick.rotate(g,bigHeight);
    		}else{
    			myBrick.display(g);
     
     
    		}		
     
    	}
     
    public void actionPerformed(ActionEvent e) {
     
    	if(e.getSource()==Right){
    		rotated=true;
    		bigHeight=!bigHeight;
     
    	}
    	if(e.getSource()==Farther){
    		myBrick.shrink(10);
    	}
    	if(e.getSource()==Closer){
    		myBrick.grow(15);
     
    	}
     
     
     
     
     
     
    	repaint();
     
    }}


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Java object help(should be simple)

    So what, exactly, are you stuck on?

    Surely you don't expect people here to finish your code because you are too busy?

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java object help(should be simple)

    Quote Originally Posted by dlorde View Post
    So what, exactly, are you stuck on?

    Surely you don't expect people here to finish your code because you are too busy?
    That would be the optimal outcome,yes.

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java object help(should be simple)

    Although I would like to know how I can rotate the rectangle/circle as a start.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java object help(should be simple)

    Sorry, we don't do homework for you.

    However, we will help you.

    If you're constraining your rectangle/circle to a plane, then rotating on that plane is quite simple.

    I'll start with the easier of the two: the circle.

    If it's a perfect circle, then at any orientation it will always be identical to any other orientation, so there's no work to do.

    The rectangle is a little more difficult, but not very (this is a nice algorithm for any n-gon shape rotation on a plane).

    The algorithm goes like this (for rotating around the center of gravity for that rectangle):
    1. calculate the (x,y) point that is the center of gravity. For a rectangle, (assuming the rectangle is not rotated) this is simply (1/2 base + lower_left.x, 1/2 height + lower_left.y), where lower_left is the point that is the lower-left corner of the rectangle.
    2. For every vertex v in the rectangle:
    3. create a vector from the center of gravity to v. Note: to make things simpler, store the vector as a magnitude and angle direction instead of as an (i,j) pair
    4. Simply add the rotation desired to the rotation of the vector
    5. Convert the magnitude-angle notation of the vector back into rectangular form. This is the location of the point offset from the center of gravity
    6. Add this vector to the (x,y) of the center of gravity. This is where vertex v should be drawn after being rotated

    Some help with vector math:

    magnitude of vector = sqrt(vector.x * vector.x + vector.y * vector.y)
    direction of vector:
    if the vector is pointed up-ish or straight left/right, the angle from the positive x-axis = arccos(vector.x / magnitude)
    otherwise, the angle from the positive x-axis = -arccos(vector.x / magnitude)

    some notes about java painting:

    Java's coordinate system is based off of the top-left corner which increase as they go down/right. However, the above math is based off of a Cartesian coordinate system based off the bottom-left corner and increasing up/right. However, the algorithm will still work with some minor tweaks to finding the center of gravity.

    Also, Java's default graphics painting class does not have any rotated rectangle shapes in then, so you will have to use the Polygon Class with the specified vertices to draw the rotated rectangle.
    Last edited by helloworld922; May 6th, 2010 at 12:21 AM.

  6. #6
    Junior Member
    Join Date
    May 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java object help(should be simple)

    Any way to do it without vectors?What am I doing wrong in my own program in terms of rotation.

Similar Threads

  1. Simple Java SOAP client
    By andy in forum Java Networking
    Replies: 3
    Last Post: December 16th, 2010, 03:05 PM
  2. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  3. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 06:53 PM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM