Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. /**
  4. * Makes a fish object, sets its swim method and makes it
  5. * flip when it hits the side
  6. * Nick Challoner
  7. * 2/12/11
  8. */
  9. public class Fish extends JComponent
  10. {
  11. private Fishtank tank;
  12. protected Image image;
  13. protected Clicker clicker;
  14. protected int speed;
  15. protected boolean left;
  16. /**
  17. * Constructor for class Fish
  18. */
  19. public Fish(int x,int y,int w,int h,String s, int speed)
  20. {
  21. super();
  22. setBounds(x,y,w,h);
  23. image = new ImageIcon(s).getImage();
  24. this.speed = speed;
  25. }
  26. /**
  27. * Fills in fish
  28. */
  29. public void paint(Graphics a)
  30. {
  31. a.drawImage(image,0,0,getWidth(),getHeight(),this);
  32. paintChildren(a);
  33. }
  34. /**
  35. * Sets the fish to swim at a certain speed, flips image when
  36. * it hits the side of the tank
  37. */
  38. public void swim()
  39. {
  40. tank = new Fishtank();
  41. setLocation(this.getX()-speed,this.getY());
  42. if(getX() <= 0)
  43. {
  44. left = false;
  45. speed = speed * -1;
  46. image = new ImageIcon("fish/starfish-right.png").getImage();
  47. }
  48. if(getX() >= (tank.getWidth()-getWidth()))
  49. {
  50. left = true;
  51. speed = speed * -1;
  52. image = new ImageIcon("fish/starfish-left.png").getImage();
  53. }
  54. }
  55. /**
  56. * moves a fish up when it is clicked
  57. */
  58. public void moveUp()
  59. {
  60. setLocation(this.getX(), this.getY() - 10);
  61. }
  62. /**
  63. * moves a fish down when it is clicked
  64. */
  65. public void moveDown()
  66. {
  67. setLocation(this.getX(), this.getY() + 10);
  68. }
  69. /**
  70. * flips the fish when it is clicked
  71. */
  72. public void flip()
  73. {
  74. if(left = true)
  75. {
  76. speed = speed * -1;
  77. image = new ImageIcon("fish/starfish-right.png").getImage();
  78. left = false;
  79. }
  80. else if(left = false)
  81. {
  82. speed = speed * -1;
  83. image = new ImageIcon("fish/starfish-left.png").getImage();
  84. left = true;
  85. }
  86. setLocation(this.getX(), this.getY());
  87. this.repaint();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement