Posted By

obrienm on 08/29/14


Tagged


Versions (?)

Burger Time - Drawable Component


 / Published in: Java
 

OO Software Development Project

  1. /**
  2.  *
  3.  */
  4. package burgertime;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.Shape;
  10. import java.awt.geom.Ellipse2D;
  11. import java.awt.geom.Point2D;
  12. import java.awt.geom.Rectangle2D;
  13. import java.awt.geom.RectangularShape;
  14. import java.awt.geom.Rectangle2D.Double;
  15. import java.awt.image.BufferedImage;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Timer;
  20. import java.util.TimerTask;
  21.  
  22. import javax.imageio.ImageIO;
  23. import javax.swing.JComponent;
  24.  
  25. /**
  26.  * DrawableComponent class draws everything in the game, handles pausing /
  27.  * resuming
  28.  *
  29.  * @author xiaox. trowbrct. obrienm.
  30.  */
  31. public class DrawableComponent extends JComponent implements Temporal {
  32. private static final int FRAMES_PER_SECOND = 30;
  33. private static final long REPAINT_INTERVAL_MS = 1000 / FRAMES_PER_SECOND;
  34.  
  35. public boolean displayPowerup;
  36. public long timePassed;
  37. private double powerupTime;
  38. public Ellipse2D powerUp;
  39. public BufferedImage powerup;
  40.  
  41. public boolean[] isLevelThreading;
  42. private boolean leveledUp;
  43.  
  44. private Rectangle2D heroRect;
  45. @SuppressWarnings("javadoc")
  46. public Hero hero;
  47. public Burger[] burger;
  48. private Runnable[] guard;
  49. private ArrayList<Drawable> drawableParts;
  50. private ArrayList<Rectangle2D> pathList;
  51. private Score score;
  52. private boolean isPaused;
  53.  
  54. @SuppressWarnings("javadoc")
  55. public Level level;
  56.  
  57. private Thread guardThread1;
  58. private Thread guardThread2;
  59. private Thread guardThread3;
  60. private Thread guardThread4;
  61.  
  62. public Game game;
  63. /**
  64. * loads the level, starts the threads, initializes all of the variables
  65. * @throws IOException
  66. *
  67. */
  68. public DrawableComponent(Game game,Score score) throws IOException {
  69. this.isLevelThreading = new boolean[4];
  70. this.isLevelThreading[0] = true;
  71. this.isLevelThreading[1] = false;
  72. this.isLevelThreading[2] = false;
  73. this.isLevelThreading[3] = false;
  74. this.score=score;
  75. this.game = game;
  76. this.level = new Level(this,this.score);
  77. this.level.loadLevel();
  78.  
  79. this.initialize();
  80. this.startThread();
  81. this.powerup = ImageIO.read(new File("shroom.png"));
  82. }
  83.  
  84. @SuppressWarnings("deprecation")
  85. private void startThread() {
  86. // Creates a separate "thread of execution" to trigger periodic
  87. // repainting of this component.
  88. Runnable repainter = new Runnable() {
  89. @Override
  90. public void run() {
  91. // Periodically asks Java to repaint this component
  92. try {
  93. while (true) {
  94. Thread.sleep(REPAINT_INTERVAL_MS);
  95. repaint();
  96. timePassed();
  97. }
  98. } catch (InterruptedException exception) {
  99. // Stop when interrupted
  100. }
  101. }
  102. };
  103. new Thread(repainter).start();
  104.  
  105. this.guardThread1.resume();
  106. this.guardThread2.resume();
  107. this.guardThread3.resume();
  108. this.guardThread4.resume();
  109. for (int i = 0; i < 4; i++) {
  110. this.burger[i].fallThread.resume();
  111. }
  112. }
  113.  
  114. /**
  115. * Initialize all of the variables from the level class
  116. *
  117. */
  118. public void initialize() {
  119. //random number between 10 and 20 for powerup spawn time
  120. this.powerupTime = 10 + (int)(Math.random() * ((10) + 1));
  121. this.displayPowerup = false;
  122. this.timePassed = System.currentTimeMillis();
  123.  
  124. if (this.level.currentLevel == 1){
  125. this.powerUp = new Ellipse2D.Double(330, 200, 20, 20);
  126. } else if (this.level.currentLevel == 2){
  127. this.powerUp = new Ellipse2D.Double(315, 200, 20, 20);
  128. } else if (this.level.currentLevel == 3){
  129. this.powerUp = new Ellipse2D.Double(315, 180, 20, 20);
  130. } else {
  131. this.powerUp = new Ellipse2D.Double(315, 180, 20, 20);
  132. }
  133.  
  134. this.leveledUp = false;
  135. this.isLevelThreading[this.level.currentLevel - 1] = true;
  136.  
  137. this.pathList = new ArrayList<Rectangle2D>();
  138. this.pathList = this.level.getPath();
  139. this.pathList.addAll(this.level.getPlates());
  140.  
  141. this.guard = new Guard[4];
  142. this.guard[0] = this.level.getGuard(0);
  143. this.guard[1] = this.level.getGuard(1);
  144. this.guard[2] = this.level.getGuard(2);
  145. this.guard[3] = this.level.getGuard(3);
  146.  
  147. this.burger = new Burger[4];
  148. this.burger[0] = this.level.getBurger(0);
  149. this.burger[1] = this.level.getBurger(1);
  150. this.burger[2] = this.level.getBurger(2);
  151. this.burger[3] = this.level.getBurger(3);
  152. for (int i=0;i<this.burger.length;i++){
  153. this.burger[i].readImage();
  154. }
  155.  
  156. this.heroRect = this.level.getHero();
  157. this.hero = new Hero(new Point2D.Double(this.heroRect.getX(),
  158. this.heroRect.getY()), this.heroRect.getWidth(),
  159. this.heroRect.getHeight(), this.pathList, this.burger,
  160. (Guard[]) this.guard,this.score);
  161. this.hero.lives = 3;
  162.  
  163. this.hero.guards = (Guard[]) this.guard;
  164. this.hero.burgers = this.burger;
  165.  
  166. this.drawableParts = new ArrayList<Drawable>();
  167. for (int i = 0; i < this.guard.length; i++) {
  168. this.drawableParts.add((Guard) this.guard[i]);
  169. }
  170. this.drawableParts.add(this.hero);
  171.  
  172. this.guardThread1 = new Thread(this.guard[0]);
  173. this.guardThread2 = new Thread(this.guard[1]);
  174. this.guardThread3 = new Thread(this.guard[2]);
  175. this.guardThread4 = new Thread(this.guard[3]);
  176.  
  177. this.guardThread1.start();
  178. this.guardThread2.start();
  179. this.guardThread3.start();
  180. this.guardThread4.start();
  181. }
  182.  
  183. @Override
  184. protected void paintComponent(Graphics g) {
  185. super.paintComponent(g);
  186.  
  187. for (int sprays = 1; sprays <= this.hero.pepperSprays; sprays++) {
  188. g2.setColor(new Color(150, 130, 10, 155));
  189. Ellipse2D.Double spray = new Ellipse2D.Double(35 * sprays + 120,
  190. 505, 30, 30);
  191. g2.fill(spray);
  192. }
  193.  
  194. for (int lives = 1; lives <= this.hero.lives; lives++) {
  195. try {
  196. BufferedImage image=ImageIO.read(new File("image/chef.png"));
  197. g2.drawImage(image,null,30*lives+10,500);
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. }
  201. }
  202.  
  203. for (int i = 0; i < this.pathList.size(); i++) {
  204. this.drawPathway(g2, this.pathList.get(i));
  205. }
  206.  
  207. for (Drawable d : this.drawableParts) {
  208. this.drawDrawable(g2, d);
  209. }
  210.  
  211. for (int i = 0; i < this.burger.length; i++) {
  212. for (Burger d : this.burger) {
  213. this.drawBurger(g2, d);
  214. }
  215. }
  216.  
  217. if (this.hero.pepperDeployed) {
  218. g2.setColor(new Color(150, 130, 10, 155));
  219. g2.fill(this.hero.deployPepperSpray());
  220. }
  221.  
  222. if (this.displayPowerup) {
  223. if (this.isPaused) {
  224. this.displayPowerup = false;
  225. } else {
  226. g2.draw(this.powerUp);
  227. int x = (int) this.powerUp.getX();
  228. int y = (int) this.powerUp.getY();
  229. g2.drawImage(this.powerup, null, x, y);
  230. Timer timer = new Timer();
  231. timer.schedule(new TimerTask() {
  232.  
  233. @Override
  234. public void run() {
  235. DrawableComponent.this.displayPowerup = false;
  236. DrawableComponent.this.timePassed = System
  237. .currentTimeMillis();
  238. }
  239. }, 4500);
  240. }
  241. }
  242. }
  243.  
  244. /**
  245. * This helper method draws the given drawable object on the given graphics
  246. * area.
  247. *
  248. * @param g2
  249. * the graphics area on which to draw
  250. * @param d
  251. * the drawable object
  252. */
  253. private void drawDrawable(Graphics2D g2, Drawable d) {
  254. Shape shape = d.getShape();
  255. g2.drawImage(d.getImage(), null,
  256. (int) ((RectangularShape) shape).getX(),
  257. (int) ((RectangularShape) shape).getY());
  258. }
  259.  
  260. private void drawPathway(Graphics2D g2, Rectangle2D pathSegment) {
  261. if (pathSegment.getHeight() > pathSegment.getWidth()) {
  262. for (int i = 0; i < pathSegment.getHeight(); i += 4) {
  263. g2.setColor(Color.DARK_GRAY);
  264. g2.fill(new Rectangle2D.Double(pathSegment.getX(), pathSegment
  265. .getY() + i, pathSegment.getWidth(), 2));
  266. }
  267. } else {
  268. g2.setColor(Color.DARK_GRAY);
  269. g2.fill(pathSegment);
  270. }
  271. }
  272.  
  273. private void drawBurger(Graphics2D g2, Burger burger) {
  274. Rectangle2D[][] shape = burger.getShape();
  275. BufferedImage[][] image = burger.getColor();
  276.  
  277. for (int i = 0; i < shape.length; i++) {
  278. for (int j = 0; j < shape[i].length; j++) {
  279. g2.drawImage(image[i][j], null, (int) shape[i][j].getX(),
  280. (int) shape[i][j].getY());
  281. }
  282. }
  283. }
  284.  
  285. /**
  286. * When the hero dies, reset position of the characters and burgers
  287. *
  288. */
  289. public void resetCharacters() {
  290. this.hero.resetPosition();
  291. this.timePassed = System.currentTimeMillis();
  292. for (int i = 0; i < 4; i++) {
  293. ((Guard) this.guard[i]).resetPosition();
  294. this.burger[i].resetPosition();
  295. }
  296. }
  297.  
  298. @Override
  299. public void timePassed() {
  300. if (System.currentTimeMillis() - this.timePassed >= this.powerupTime * 1000){
  301. this.displayPowerup = true;
  302. this.timePassed = System.currentTimeMillis();
  303. }
  304.  
  305. if (this.displayPowerup){
  306. if (this.powerUp.intersects(this.hero.character)){
  307. this.score.addScore(500);
  308. this.hero.pepperSprays++;
  309. this.displayPowerup = false;
  310. }
  311. }
  312.  
  313. if (!this.hero.pepperDeployed) {
  314. if (this.hero.isDead()) {
  315. this.resetCharacters();
  316. this.hero.heroState = false;
  317. }
  318. if (this.hero.contactWithGuard(this.hero.getHeroCorners(0, 0))) {
  319. this.hero.die();
  320.  
  321. }
  322. }
  323. if (this.hero.lives == 0) {
  324. this.isLevelThreading[this.level.currentLevel - 1] = false;
  325. this.game.gameOver(true);
  326. this.hero.lives = -1;
  327. }
  328.  
  329.  
  330. int plateCount = 0;
  331. if (!this.leveledUp) {
  332. for (int i = 0; i < 4; i++) {
  333. if (this.burger[i].isPlated()) {
  334. plateCount++;
  335. }
  336. }
  337. }
  338. if (plateCount == 4) {
  339. this.leveledUp = true;
  340. Timer timer = new Timer();
  341. timer.schedule(new TimerTask() {
  342.  
  343. @Override
  344. public void run() {
  345. int level = DrawableComponent.this.level.currentLevel;
  346. DrawableComponent.this.level.upLevel();
  347. if (level != DrawableComponent.this.level.currentLevel) {
  348. DrawableComponent.this.leveledUp = false;
  349. }
  350. }
  351. }, 1500);
  352. }
  353.  
  354. }
  355.  
  356. /*
  357. * (non-Javadoc)
  358. *
  359. * @see burgertime.Temporal#setIsPaused(boolean)
  360. */
  361. @SuppressWarnings("deprecation")
  362. @Override
  363. public void setIsPaused(boolean isPaused) {
  364. this.isPaused = isPaused;
  365. for (int i = 0; i < 4; i++) {
  366. this.burger[i].setIsPaused(this.isPaused);
  367. }
  368. if (!this.isPaused) {
  369. startThread();
  370. } else {
  371. this.guardThread1.suspend();
  372. this.guardThread2.suspend();
  373. this.guardThread3.suspend();
  374. this.guardThread4.suspend();
  375. for (int i = 0; i < 4; i++) {
  376. this.burger[i].fallThread.suspend();
  377. }
  378. }
  379. }
  380.  
  381. /*
  382. * (non-Javadoc)
  383. *
  384. * @see burgertime.Temporal#getIsPaused()
  385. */
  386. @Override
  387. public boolean getIsPaused() {
  388. return this.isPaused;
  389. }
  390.  
  391. }

Report this snippet  

You need to login to post a comment.