1 Reply Latest reply: Mar 11, 2010 8:25 AM by 800560 RSS

    Big Problem with keyPressed  & Repaint TOGETHER

    843804
      hi everyone, i have a problem with the paint component.
      i try to draw an image every move but it doesnt paint it, im exhausted from findin it out, it draws the pic only at the beginning point and at the end point
      this is my code:

      public class Game extends Canvas implements Runnable{
      static int width; // screen width
      static int height; // screen heigh
      Thread GameThread; // Menu Thread
      Image bgImage; // BG Image
      Image playerImage;
      int player_x=0,player_y=0;
      int player_dif_x=0,player_dif_y=0;
      private int level=0;
      private boolean first;

      public Game(int n){
      level=n;
      width = getWidth();
      height = getHeight();
      // Set BG Image
      try {
      bgImage = Image.createImage("/Pics/gamebg.png");
      bgImage=MyMain.scale(bgImage, getWidth(), getHeight());
      } catch (Exception e) {
      System.out.println("Error creating Background Image :: " + e);
      }
      //Set Player Image
      try{
      playerImage = Image.createImage("/Pics/gameplayerpix.png");
      playerImage=MyMain.scale(playerImage, (getWidth()/15), (getHeight()/18));
      } catch (Exception e) {
      System.out.println("Error creating Player Image :: " + e);
      }
      GameThread = new Thread(this);
      GameThread.start();

      }
      protected void paint(Graphics g) {
      if(first){
      g.setColor(0x00000000);
      g.fillRect(0,0,width,height);
      g.drawImage(bgImage, 0, 0, 0);
      }
      g.drawImage(playerImage, player_x, player_y, 0);
      }

      protected void keyPressed(int keyCode){
      int gameAction=getGameAction(keyCode);
      int flag=1;
      if(first){
      first=false;
      }
      if(gameAction==RIGHT){
      System.out.println("Right");
      while(flag==1){
      if(player_x<80){
      player_x++;
      repaint();
      }
      else{
      flag=0;
      }
      }
      }
      }

      public void run() {
      first=true;
      while(true){
      repaint();
      }
      }

      }

      Edited by: Shayhibah on Mar 11, 2010 1:44 AM

      Edited by: Shayhibah on Mar 11, 2010 1:45 AM

      Edited by: Shayhibah on Mar 11, 2010 1:49 AM
        • 1. Re: Big Problem with keyPressed  & Repaint TOGETHER
          800560
          What does this have to do with Jar files? I think your post belongs in the AWT forum.

          Please use code tags when you post code. The forum software munges code up otherwise. There's a label "CODE" you can click and it generates the tags. Put your formatted code between them.

          I think you are trying to have something happen when a key is pressed. You have a method called keyPressed(). But it is not getting called by anything. Google "java keyboard tutorial"

          I suggest switching over to Swing. Go through some Swing tutorials.