Advertisement
Crenox

Virtual Pet Java Applet Program

May 12th, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. // Sammy Samkough
  2. // Virtual Pet Object
  3. // Spec: This class encapsulates all of the things we wish our Virtual Pet to be able to do.
  4. // We want to model the behavior of an actual pet.
  5.  
  6. import java.util.Random;
  7. import java.awt.*;
  8. import javax.swing.*;
  9.  
  10. public class VirtualPet
  11. {
  12. // You can keep these categories and icons, or make your own:
  13. private int step, happiness, health, food;
  14. private double age;
  15. private ImageIcon icoHappy, icoSad, icoHungry, icoUnhealthy, icoDead;
  16. private boolean notDead;
  17.  
  18. /** initialize data to zero to start with
  19. * instantiate ImageIcons to the graphics files that you saved
  20. * ie icoHappy = new ImageIcon("HappyPet.gif") etc.
  21. */
  22. public VirtualPet()
  23. {
  24. // Categories
  25. age = 0;
  26. step = 5;
  27. happiness = 5;
  28. health = 5;
  29. food = 5;
  30. notDead = true;
  31.  
  32. // ImageIcons
  33. icoHappy = new ImageIcon("HappyPet.jpg");
  34. icoSad = new ImageIcon("SadPet.jpg");
  35. icoHungry = new ImageIcon("HungryPet.jpg");
  36. icoUnhealthy = new ImageIcon("UnhealthyPet.jpg");
  37. icoDead = new ImageIcon("DeadPet.jpg");
  38. }
  39.  
  40. /** Post Condition: health and happiness are increased
  41. * food is decreased
  42. * These should be random amounts in a range that you create
  43. */
  44. public void walkPet()
  45. {
  46. if (notDead == true)
  47. {
  48. health += (int) (Math.random() * 5) + 1;
  49. happiness += (int) (Math.random() * 5) + 1;
  50. food -= (int) (Math.random() * 3) + 1;
  51. }
  52. }
  53.  
  54. /** Post Condition: food is increased by a small random amount
  55. */
  56. public void feedPet()
  57. {
  58. if (notDead == true)
  59. {
  60. food += (int) (Math.random() * 3) + 1;
  61. }
  62. }
  63.  
  64. /** Post Condition: Happiness is increased and health is increased slightly as well
  65. * food is decreased slightly... playing burns calories!
  66. */
  67. public void playWithPet()
  68. {
  69. if (notDead == true)
  70. {
  71. happiness += (int) (Math.random() * 5) + 1;
  72. health += (int) (Math.random() * 2) + 1;
  73. food -= (int) (Math.random() * 2) + 1;
  74. }
  75. }
  76.  
  77. /** Post Condition: Health is increased; Happiness is decreased
  78. * food is decreased
  79. */
  80. public void takeToVet()
  81. {
  82. if (notDead == true)
  83. {
  84. health += (int) (Math.random() * 5) + 1;
  85. happiness -= (int) (Math.random() * 4) + 1;
  86. food -= (int) (Math.random() * 3) + 1;
  87. }
  88. }
  89.  
  90. /** Post Condition: Health is increased and happiness slightly as well
  91. * food is slightly decreased
  92. */
  93. public void sleep()
  94. {
  95. if (notDead == true)
  96. {
  97. health += (int) (Math.random() * 7) + 1;
  98. happiness += (int) (Math.random() * 4) + 1;
  99. food -= (int) (Math.random() * 3) + 1;
  100. }
  101. }
  102.  
  103. /** Everything that should happen in any given time interval
  104. * each category should be affected in some way
  105. */
  106. public void takeStep()
  107. {
  108. if(notDead == true)
  109. {
  110. age += .3;
  111. health -= age;
  112. happiness -= age;
  113. food--;
  114. }
  115. }
  116.  
  117. /** @return the appropriate ImageIcon based on the current state of this pet
  118. */
  119. public ImageIcon getIcon()
  120. {
  121. if(food < 0 && health < 0 && happiness < 0)
  122. {
  123. notDead = false;
  124. return icoDead;
  125. }
  126. else if(happiness < 0 && notDead == true)
  127. {
  128. return icoSad;
  129. }
  130. else if(food < 0 && notDead == true)
  131. {
  132. return icoHungry;
  133. }
  134. else if(health < 0 && notDead == true)
  135. {
  136. return icoUnhealthy;
  137. }
  138. else
  139. {
  140. return icoHappy;
  141. }
  142. }
  143.  
  144. /** @return a String representing the current state of this VirtualPet
  145. * Label each category and show the current value for each
  146. */
  147. public String toString()
  148. {
  149. String result = "";
  150.  
  151. if(!notDead)
  152. {
  153. result += "Wow. Good Job, you killed him.";
  154. }
  155. else
  156. {
  157. result += "age: " + (int)age + " happiness " + happiness + " health " + health + " food " + food;
  158. }
  159.  
  160. return result;
  161. }
  162. }
  163. ------------------------------------------------------------------------------------------------------------------------------
  164. // Sammy Samkough
  165. // Virtual Pet Applet
  166. // Spec: An applet front-end for a VirtualPet
  167. // Utilizes a Timer with it's own ActionListener
  168. // and another ActionListener for the buttons.
  169.  
  170. import java.awt.*;
  171. import java.awt.event.*;
  172. import javax.swing.*;
  173.  
  174. public class VirtualPetApplet extends JApplet
  175. {
  176. private VirtualPet spot;
  177. private Timer tmr;
  178. private TimerListener tmrListener;
  179. private ButtonListener btnListener;
  180. private JButton btnWalk, btnFeed, btnPlay, btnVet, btnSleep;
  181. private JLabel lblPet, lblStats;
  182.  
  183. public void init()
  184. {
  185. Container cp = getContentPane();
  186. Frame c = (Frame)this.getParent().getParent();
  187. cp.setLayout(new FlowLayout());
  188.  
  189. tmrListener = new TimerListener();
  190. btnListener = new ButtonListener();
  191.  
  192. spot = new VirtualPet();
  193. tmr = new Timer(5000, tmrListener);
  194. tmr.start();
  195.  
  196. btnWalk = new JButton("Walk");
  197. btnFeed = new JButton("Feed");
  198. btnPlay = new JButton("Play");
  199. btnVet = new JButton("Vet");
  200. btnSleep = new JButton("Sleep");
  201. lblPet = new JLabel(spot.getIcon());
  202. lblStats = new JLabel("");
  203.  
  204. btnWalk.addActionListener(btnListener);
  205. btnFeed.addActionListener(btnListener);
  206. btnPlay.addActionListener(btnListener);
  207. btnVet.addActionListener(btnListener);
  208. btnSleep.addActionListener(btnListener);
  209.  
  210. cp.add(lblPet);
  211. cp.add(lblStats);
  212. cp.add(btnWalk);
  213. cp.add(btnFeed);
  214. cp.add(btnPlay);
  215. cp.add(btnVet);
  216. cp.add(btnSleep);
  217.  
  218. lblStats.setText(spot.toString());
  219.  
  220. c.setTitle("Virtual Pet");
  221. setSize(900, 450);
  222. setVisible(true);
  223. }
  224.  
  225. private class TimerListener implements ActionListener
  226. {
  227. public void actionPerformed(ActionEvent event)
  228. {
  229. // This will happen at every timer interval as specified above
  230. // At each interval our pet (spot) should:
  231. // - Take a step
  232. // - Update the stats in lblStats
  233. // - Update the icon in lblPet
  234. spot.takeStep();
  235. lblStats.setText(spot.toString());
  236. lblPet.setIcon(spot.getIcon());
  237. }
  238. }
  239.  
  240. private class ButtonListener implements ActionListener
  241. {
  242. public void actionPerformed(ActionEvent event)
  243. {
  244. Object source = event.getSource();
  245.  
  246. // Build an extended-if to account for each of the other buttons in your interface
  247. if(source == btnWalk)
  248. {
  249. spot.walkPet();
  250. }
  251. else if(source == btnFeed)
  252. {
  253. spot.feedPet();
  254. }
  255. else if(source == btnPlay)
  256. {
  257. spot.playWithPet();
  258. }
  259. else if(source == btnVet)
  260. {
  261. spot.takeToVet();
  262. }
  263. else if(source == btnSleep)
  264. {
  265. spot.sleep();
  266. }
  267.  
  268. // Update the screen for each click:
  269. // - Update the stats in lblStats
  270. // - Update the icon in lblPet
  271. lblStats.setText(spot.toString());
  272. lblPet.setIcon(spot.getIcon());
  273. }
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement