Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- /**
- * Makes a fish object, sets its swim method and makes it
- * flip when it hits the side
- * Nick Challoner
- * 2/12/11
- */
- public class Fish extends JComponent
- {
- private Fishtank tank;
- protected Image image;
- protected Clicker clicker;
- protected int speed;
- protected boolean left;
- /**
- * Constructor for class Fish
- */
- public Fish(int x,int y,int w,int h,String s, int speed)
- {
- super();
- setBounds(x,y,w,h);
- image = new ImageIcon(s).getImage();
- this.speed = speed;
- }
- /**
- * Fills in fish
- */
- public void paint(Graphics a)
- {
- a.drawImage(image,0,0,getWidth(),getHeight(),this);
- paintChildren(a);
- }
- /**
- * Sets the fish to swim at a certain speed, flips image when
- * it hits the side of the tank
- */
- public void swim()
- {
- tank = new Fishtank();
- setLocation(this.getX()-speed,this.getY());
- if(getX() <= 0)
- {
- left = false;
- speed = speed * -1;
- image = new ImageIcon("fish/starfish-right.png").getImage();
- }
- if(getX() >= (tank.getWidth()-getWidth()))
- {
- left = true;
- speed = speed * -1;
- image = new ImageIcon("fish/starfish-left.png").getImage();
- }
- }
- /**
- * moves a fish up when it is clicked
- */
- public void moveUp()
- {
- setLocation(this.getX(), this.getY() - 10);
- }
- /**
- * moves a fish down when it is clicked
- */
- public void moveDown()
- {
- setLocation(this.getX(), this.getY() + 10);
- }
- /**
- * flips the fish when it is clicked
- */
- public void flip()
- {
- if(left = true)
- {
- speed = speed * -1;
- image = new ImageIcon("fish/starfish-right.png").getImage();
- left = false;
- }
- else if(left = false)
- {
- speed = speed * -1;
- image = new ImageIcon("fish/starfish-left.png").getImage();
- left = true;
- }
- setLocation(this.getX(), this.getY());
- this.repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement