java - Repaint() not repainting images -


i'm making pong game in java play around language , wondering why repaint method not repainting images on screen. want when ball hits either side of screen initiate player has scored, want repaint in starting place emulate reset. i've tried putting repaint on place, either leads locking or game continuing on if nothing happened.

import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.rectangle;  import javax.swing.jpanel; import javax.swing.timer;  public class board extends jpanel implements actionlistener {     private timer timer;     private playerone paddle;     private playertwo paddle2;     private ball ball;     private boolean ingame = true;     int playeronescore = 0;     int playertwoscore = 0;      public board()     {          addkeylistener(new tadapter());         setfocusable(true);         setdoublebuffered(true);          paddle = new playerone();         paddle2 = new playertwo();         ball = new ball();          timer = new timer(5, this);         timer.start();     }  public void paint(graphics g) {     if(ingame)     {         super.paint(g);         graphics2d g2d = (graphics2d) g;         g2d.drawimage(paddle.getimage(), paddle.getx(), paddle.gety(), this);         g2d.drawimage(paddle2.getimage(), paddle2.getx(), paddle2.gety(), this);         g2d.drawimage(ball.getimage(), ball.getx(), ball.gety(),this);         toolkit.getdefaulttoolkit().sync();         g.dispose();     }     else     {         restartgame();         repaint();     }  }  public void actionperformed(actionevent e) {     paddle.move();     paddle2.move();     ball.move();     checkcollision();     repaint(); }  private class tadapter extends keyadapter {     public void keypressed(keyevent e)     {         paddle.keypressed(e);         paddle2.keypressed(e);     }      public void keyreleased(keyevent e)     {         paddle.keyreleased(e);         paddle2.keyreleased(e);     } } public void restartgame()//resetting paddles , ball initial positions {     paddle.setx(0);     paddle.sety(60);     paddle2.setx(280);     paddle2.sety(60);     ball.setx(140);     ball.sety(60);  } public void checkcollision() {     rectangle r1 = paddle.getbounds();     rectangle r2 = paddle2.getbounds();     rectangle r3 = ball.getbounds();      if(ball.getx() == 0)     {         playeronescore++;         ingame = false;     }      if(ball.getx() == 280)     {         playertwoscore++;         ingame = false;     }      if(r3.intersects(r1))     {         int paddlelpos = (int)paddle.getbounds().getminx();         int balllpos = (int)ball.getbounds().getminx();          int first = paddlelpos + 8;         int second = paddlelpos + 16;         int third = paddlelpos + 24;         int fourth = paddlelpos + 32;          if (balllpos < first) {             ball.setxdir(-1);             ball.setydir(-1);         }          if (balllpos >= first && balllpos < second) {             ball.setxdir(-1);             ball.setydir(-1 * ball.getydir());         }          if (balllpos >= second && balllpos < third) {             ball.setxdir(0);             ball.setydir(-1);         }          if (balllpos >= third && balllpos < fourth) {             ball.setxdir(1);             ball.setydir(-1 * ball.getydir());         }          if (balllpos > fourth) {             ball.setxdir(1);             ball.setydir(-1);         }                }     else if(r3.intersects(r2))     {         int paddlelpos = (int)paddle2.getbounds().getminx();         int balllpos = (int)ball.getbounds().getminx();          int first = paddlelpos + 8;         int second = paddlelpos + 16;         int third = paddlelpos + 24;         int fourth = paddlelpos + 32;          if (balllpos < first) {             ball.setxdir(-1);             ball.setydir(-1);         }          if (balllpos >= first && balllpos < second) {             ball.setxdir(-1);             ball.setydir(-1 * ball.getydir());         }          if (balllpos >= second && balllpos < third) {             ball.setxdir(0);             ball.setydir(-1);         }          if (balllpos >= third && balllpos < fourth) {             ball.setxdir(1);             ball.setydir(-1 * ball.getydir());         }          if (balllpos > fourth) {             ball.setxdir(1);             ball.setydir(-1);         }                } }  } 

any appreciated.

ok, have few issues here.

first should override paintcomponents not paint.

second, repaint() calls paint. reason game locking you're forcing never ending loop. paint called, calls repaint calls paint etc etc etc.

finally, carmickr said in comment, shouldn't changing component properties in paint method.

so. putting together, i'd suggest relook @ actionperformed logic. you're learning i'm bit loathe hand pile of code, few hints:

you determine in checkcollision whether point has been scored. returning method actionperformed method - if point has been scored this, if not that. @ end of you'll need call paintcomponent you'll need flag - boolean variable - telling paintcomponent whether draw updated positions of bats or initial position @ least, carmickr's suggestion of resetting positions , having 1 route through paintcomponent better 1 though.

i hope helps.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -