High CPU usage with simple Java game

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 11
Reputation: cherryduck is an unknown quantity at this point 
Solved Threads: 0
cherryduck cherryduck is offline Offline
Newbie Poster

High CPU usage with simple Java game

 
0
  #1
Apr 12th, 2009
Hi everyone, I'm trying to create a simple Java game, The Last Space Invader, where you control the last space invader and the tank automatically tracks and shoots at you. The code for my invader is as follows:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import javax.imageio.*;
  5. import java.awt.event.*;
  6.  
  7. public class Invader extends JPanel{
  8.  
  9. private Image sprite;
  10. private int spriteW;
  11. private int spriteH;
  12. private int speedX = 5;
  13. private int speedY = 8;
  14. private Point invPos;
  15. private boolean alive = true;
  16. private boolean hitright = false;
  17. private boolean hitleft = false;
  18. private boolean landed = false;
  19. static final int RIGHT = 1;
  20. static final int LEFT = 0;
  21. private int rightEdge;
  22. private int leftEdge;
  23. private int top;
  24. private int bottom;
  25. private int lives = 5;
  26. private int spriteCount = 1;
  27.  
  28. public Invader()
  29. {
  30. this.addKeyListener(new KeyboardListener());
  31. this.setFocusable(true);
  32. invPos = new Point(110, 20);
  33. rightEdge = 256 - getSpriteW();
  34. setBoundaries(0, 208, 224, 395);
  35. start();
  36. }
  37.  
  38. public void paintInv()
  39. {
  40. try{
  41. sprite = ImageIO.read(new File("Invader" + spriteCount + ".png"));
  42. spriteW = sprite.getWidth(this);
  43. spriteH = sprite.getHeight(this);
  44. }
  45. catch(Exception e){
  46. System.out.println("File not found");
  47. }
  48. }
  49.  
  50. public boolean dead()
  51. {
  52. return !alive;
  53. }
  54.  
  55. public Point where()
  56. {
  57. return invPos;
  58. }
  59.  
  60. public void checkPosition()
  61. {
  62. if (invPos.x < leftEdge )
  63. {
  64. invPos.x = leftEdge;
  65. if (!hitleft)
  66. {
  67. hitleft = true;
  68. hitright = false;
  69. invPos.y += speedY;
  70. }
  71. }
  72.  
  73. if (invPos.x > rightEdge )
  74. {
  75. invPos.x = rightEdge;
  76. if (!hitright)
  77. {
  78. hitright = true;
  79. hitleft = false;
  80. invPos.y += speedY;
  81. }
  82. }
  83.  
  84. if (invPos.y > bottom)
  85. {
  86. invPos.y = bottom;
  87. landed = true;
  88. }
  89. }
  90.  
  91. public void moveInvader(int direction)
  92. {
  93. if (alive && !landed)
  94. {
  95. if (direction == LEFT)
  96. {
  97. invPos.x -= speedX;
  98. }
  99.  
  100. else
  101. {
  102. invPos.x += speedX;
  103. }
  104.  
  105. checkPosition();
  106. repaint();
  107. }
  108. }
  109.  
  110. public void setBoundaries(int l, int r, int t, int b)
  111. {
  112. leftEdge = l;
  113. rightEdge = r;
  114. top = t;
  115. bottom = b;
  116. }
  117.  
  118. public void paintComponent(Graphics g)//Paint the panel component
  119. {
  120. Graphics2D g2 = (Graphics2D) g;
  121. if(alive)
  122. {
  123. g.drawImage(sprite,invPos.x,invPos.y, this);
  124. spriteCount++;
  125.  
  126. if(spriteCount == 4)
  127. {
  128. spriteCount = 1;
  129. }
  130.  
  131. start();
  132. }
  133.  
  134. else
  135. {
  136. g.setColor(Color.white);
  137. g.drawString("You Are Dead", 100, 100);
  138. }
  139. }
  140.  
  141. public void setSpriteW(int spriteW)
  142. {
  143. this.spriteW = spriteW;
  144. }
  145.  
  146. public int getSpriteW()
  147. {
  148. return spriteW;
  149. }
  150.  
  151. public class KeyboardListener implements KeyListener
  152. {
  153. public void keyReleased(KeyEvent k)
  154. {
  155. }
  156.  
  157. public void keyPressed(KeyEvent k)
  158. {
  159. int code = k.getKeyCode();
  160. if (code == KeyEvent.VK_LEFT)
  161. moveInvader(LEFT);
  162. if (code == KeyEvent.VK_RIGHT)
  163. moveInvader(RIGHT);
  164. }
  165.  
  166. public void keyTyped(KeyEvent k)
  167. {\
  168. }
  169. }
  170.  
  171. public void start()
  172. {
  173. for(int i=0; i<20; i++)
  174. {
  175. repaint();
  176. paintInv();
  177. }
  178. }
  179.  
  180. }

However my CPU usage is at 60-70% when running the complete program. I realise this is probably something to do with my for loop in the start method, but I'm not sure how to make Java wait before updating the sprite image. I know I should probably be using threads but again I'm something of a beginner and haven't wrapped my head around threads yet, however much I try.

EDIT

I'd just like to point out that by code isn't actually that badly set out but it seems to have been changed in pasting it here.

SECOND EDIT

Please ignore this, unless you have any improvements on my code. I've managed to implement it using a thread instead, simpler than I thought.
Last edited by cherryduck; Apr 12th, 2009 at 11:40 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,597
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: High CPU usage with simple Java game

 
0
  #2
Apr 12th, 2009
The for loop with only 20 iterations is not going to cause you trouble. One of the methods called from inside the for loop might, however, or calling the method that contains the for loop repeatedly. Have you actually checked to see how much your .exe is using? And where is your main method?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: cherryduck is an unknown quantity at this point 
Solved Threads: 0
cherryduck cherryduck is offline Offline
Newbie Poster

Re: High CPU usage with simple Java game

 
0
  #3
Apr 12th, 2009
This is just one class file of many, so the main is contained elsewhere.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,597
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: High CPU usage with simple Java game

 
0
  #4
Apr 12th, 2009
Right, but I wanted to see it so that I could follow your logic, perhaps see if there is a loop somewhere or some other reason that paintComponent might be getting called repeatedly even if it doesn't need to be, etc

edit:

repaint calls paint which calls paintComponent, just FYI
Last edited by BestJewSinceJC; Apr 12th, 2009 at 1:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: cherryduck is an unknown quantity at this point 
Solved Threads: 0
cherryduck cherryduck is offline Offline
Newbie Poster

Re: High CPU usage with simple Java game

 
0
  #5
Apr 12th, 2009
Ok fair enough, my main is as follows:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import javax.imageio.*;
  5. import java.awt.event.*;
  6.  
  7. public class SpaceFrame{
  8.  
  9. public static void main(String[] args){
  10. JFrame jf = new JFrame("The Last Space Invader");
  11. AlienLandingPanel alp = new AlienLandingPanel();
  12. jf.add(alp);
  13. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. jf.setSize(256, 512);
  15. jf.setVisible(true);
  16. }
  17. }

The actual code which initiates everything else is in AlienLandingPanel, as below:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.*;
  4. import javax.imageio.*;
  5. import java.awt.event.*;
  6.  
  7. public class AlienLandingPanel extends JPanel implements Runnable{
  8.  
  9. private Thread t;
  10. private Invader inv;
  11. private Tank tank;
  12. //private Missile miss;
  13. private Image backImg;
  14. boolean changed = true;
  15. String score;
  16. String lives;
  17. int livesRem = 10;
  18.  
  19. public AlienLandingPanel() {
  20.  
  21. inv = new Invader();
  22. add(inv);
  23. tank = new Tank();
  24. try{
  25. backImg = ImageIO.read(new File("back.jpg"));
  26. }
  27. catch(Exception e)
  28. {
  29. System.out.println("Not found");
  30. }
  31. t = new Thread(this);
  32. t.start();
  33. }
  34.  
  35. public void paintComponent(Graphics g) {
  36. super.paintComponent(g);
  37. g.drawImage(backImg, 0, 0, this);
  38. inv.paintComponent(g);
  39. tank.paintComponent(g);
  40. } public void run()
  41. {
  42. try{
  43. while(!inv.dead())
  44. {
  45. Thread.sleep(50);
  46. Point p = inv.where();
  47. tank.moveTank(p.x);
  48. repaint();
  49. }
  50. }
  51.  
  52.  
  53. catch(InterruptedException inter)
  54. {
  55.  
  56. }
  57. }
  58.  
  59. }

The program as a whole is by no means complete and most likely needs bits adding to it. My other main problem at the moment is that I am entirely unable to get my missiles to work, if I use miss.paintComponent(g); in the AlienLandingPanel the whole thing crashes :-S but I guess that's another problem altogether for another day.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC