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(); }}