swing - Game in java: rotating selected object -


i'm developing java game study purposes. want player select object. selected object can move, others (non-selected objects) must not move.

game.java:

import javax.swing.jframe;  public class game {     public static void main(string[] args) {         jframe frame = new jframe("kibe");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setcontentpane(new gamepanel());          frame.pack();         frame.setvisible(true);     } } 

gamepanel.java:

import javax.swing.jpanel; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.*;  public class gamepanel extends jpanel implements runnable, keylistener {     // fields     public static final int width = 640, height = 480;      private thread thread;     private boolean running;      private int fps = 30;     private double averagefps;      private bufferedimage image;     private graphics2d g;      public arraylist<circle> circles;     private int selectedcircle;      // constructor     public gamepanel(){         super();         setpreferredsize(new dimension(width, height));         setfocusable(true);         requestfocus();     }     // functions      public void addnotify(){         super.addnotify();         if(thread == null){             thread = new thread(this);             thread.start();         }         addkeylistener(this);     }      public void run(){         running = true;          image = new bufferedimage(width, height, bufferedimage.type_int_rgb);         g = (graphics2d) image.getgraphics();          circles = new arraylist<circle>();          long starttime;          gameupdate();         gamerender();         gamedraw();          long urdtimemillis;         long waittime;         long totaltime = 0;          int framecount = 0;         int maxframecount = 30;          long targettime = 1000 / fps;          while(running){ // game loop             starttime = system.nanotime();              gameupdate();             gamerender();             gamedraw();              urdtimemillis = (system.nanotime() - starttime) / 1000000;             waittime = targettime - urdtimemillis;              try{                 thread.sleep(waittime);             }catch(exception e){                 e.printstacktrace();             }              framecount++;             if(framecount == maxframecount){                 averagefps = 1000.0 / ((totaltime / framecount) / 1000000);                 framecount = 0;                 totaltime = 0;             }         }     }      private void gameupdate(){         circles.get(selectedcircle).update();     }      private void gamerender(){         g.setcolor(color.black);         g.fillrect(0, 0, width, height);          for(int = 0; < circles.size(); i++){             circles.get(i).draw(g);         }     }      private void gamedraw(){         graphics g2 = this.getgraphics();         g2.drawimage(image, 0, 0, null);         g2.dispose();     }      // key functions     public void keytyped(keyevent e){         int keycode = e.getkeycode();     }     public void keypressed(keyevent e){         int keycode = e.getkeycode();         if(keycode == keyevent.vk_space){             circles.add(new circle());         }         else if(keycode == keyevent.vk_z){             selectedcircle = (selectedcircle + 1) % circles.size();         }     }     public void keyreleased(keyevent e){         int keycode = e.getkeycode();     } } 

circle.java:

import java.awt.*;  public class circle {     // fields     private double x;     private double y;     private int speed;      private int dx;     private int dy;     private int r;      private boolean up;     private boolean down;     private boolean left;     private boolean right;      private color color;      // constructor     public circle(){         x = math.random() * gamepanel.width / 2 + gamepanel.height / 4;         y = -r;         speed = 5;          dx = 0;         dy = 0;         r = 5;          color = color.white;     }      // functions     public void setup(boolean b) { = b; }     public void setdown(boolean b) { down = b; }     public void setleft(boolean b) { left = b; }     public void setright(boolean b ) { right = b; }      public void update(){         if(up)             dy = -speed;         else             dy = 0;          if(down)             dy = speed;          if(left)             dx = -speed;         else             dx = 0;          if(right)             dx = speed;          color = color.red;     }      public void draw(graphics2d g){         g.setcolor(color.white);         g.filloval((int) x - r, (int) y - r, 2 * r, 2 * r);     } } 

the error when try run:

exception in thread "thread-2" java.lang.indexoutofboundsexception: index: 0, si ze: 0 @ java.util.arraylist.rangecheck(unknown source) @ java.util.arraylist.get(unknown source) @ gamepanel.gameupdate(gamepanel.java:102) @ gamepanel.run(gamepanel.java:51) @ java.lang.thread.run(unknown source)

the error message clear:

indexoutofboundsexception: index: 0, size: 0 

you're trying 0th item out of arraylist size 0, meaning there is no 0th item (first item).

this line:

private void gameupdate(){     circles.get(selectedcircle).update();  // here **** } 

this happening @ game start before circles arraylist holds circle objects.

one solution validity check before trying extract doesn't exist, e.g.,

private void gameupdate() {     if (selectedcircle < circles.size()) {         circles.get(selectedcircle).update();     } }     

of course won't prevent other problems encounter code including

  • negative thread.sleep times
  • drawing graphics object obtained calling getgraphics() on swing component
  • making swing calls directly background thread

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -