Member Avatar for champmanking

The solution you provided causes the method not to work anymore. . . if I undo it partially works (prints but in wrong place).

Missile.java paint method:

        // paints image
      public void paint(Graphics g, Main main){
          if(visible){
          g.drawImage(missileimage, sprite.getX(), sprite.getY(), main);
          String variable = String.valueOf(sprite.getX()); // get x
          g.drawString(variable, 10, 10); // print current x of sprite
          }

}

Main.java:

       // Paints
    public void paint(Graphics g){
        sprite.paint(g, this);
        missile.paint(g, this);

Missile.java press space:

             // Key detector
          public void keyReleased(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_SPACE:{
               //velshotx  = 0;
               break;
          }

     }
}

          public void keyPressed(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_SPACE:{
               visible = true;
               break;
          }

     }
}

          // update emthod
      public void update(Main main){
        main.addKeyListener(this);


} 

Use lots of print statements to check that methods are being called and key variables/returned values (eg sprite.getX()) have the values you expected. The change we made will not, of itself, break the code so there's somethig else lurking in there that we just exposed.

Member Avatar for champmanking

The methods are being called. Is it to do with these in Missile.java that the missile image doesn't print on SPACE press?

         // Missile contrustor loads image
       public Missile(Main main, Sprite sprite){
         this.sprite = sprite;
         url = main.getDocumentBase();
         missileimage = main.getImage(url, "missile.png");

} 

and

          // update emthod
      public void update(Main main){
        main.addKeyListener(this);

} 

... and key variables/returned values (eg sprite.getX()) have the values you expected

Member Avatar for champmanking

Yes, that works, also.

If every method is being called as expected, and every variable amd method return has the correct value then your program must be perfect. You're just not looking hard enough. Don't rely on your assumptions about what your code must be doing. If it were following your assumtions then it would work.

Member Avatar for champmanking

Changed a few things, here is my full code, maybe I'm missing something and haven't given you enough code/information to help me.

Main.java:

   // Includes
import java.awt.*;    
import java.awt.event.*;
import javax.swing.*; 
import java.applet.*; 
import java.util.*;

       //Main class runs everything
    public class Main extends Applet implements Runnable { 

      // Double buffering
    private Image dbi;
    private Graphics dbg;
    Player player;
    Shot shot;


       // Auto called on start
    public void init() { 

              // Create thread
          Thread thread = new Thread(this);
          player = new Player(this);
          shot = new Shot(this, player);
              // Start thread
          thread.start();
            // Set screen size 600*400 pixels
         setSize(600, 400);

}

       // Called when user returns to page
    public void start() {}

       // Called when user leaves page
    public void stop() {}

       // Called when browser closes
    public void destroy() {}


       // Runs
    public void run(){

           // While app running
        while(true){

              // Update screen
            repaint();
            player.update(this);
            shot.update(this);


              // Refresh 
            try {
                 Thread.sleep(1000/30);
                } catch (InterruptedException e){
                   ; 
                                                 }
                   }

}

        // Updates
    public void update(Graphics g){
        dbi = createImage(600, 400);
        dbg = dbi.getGraphics();
        paint(dbg);
        g.drawImage(dbi, 0, 0, this);

}

       // Paints
    public void paint(Graphics g){
        player.paint(g, this);
        shot.paint(g, this);


}

}

Player.java

 // Includes
import java.awt.*;
import java.awt.event.*;
import java.net.URL;


        // Player class
     public class Player implements KeyListener{

        // Player class variables
     private int x = 300;
     private int y = 200;
     private int radius = 20;
     private int velX = 0;
     private int velY = 0;
     private URL url;
     private Image playerimage;


       //Picture loader constructor
     public Player(Main main){
     url = main.getDocumentBase();
     playerimage = main.getImage(url, "player.jpg");
}




          // Player updater 
        public void update(Main main){
        main.addKeyListener(this);    
        x += velX;
        y += velY;

       if(this.getX() >= main.getWidth() || this.getX() <= 0 || this.getY() >= main.getHeight() || this.getY() <= 0){
           setX(300);
           setY(200);
        }


}

            // Paints image
         public void paint(Graphics g, Main main){

         g.drawImage(playerimage, x, y, main);

}

             // Key detector
          public void keyReleased(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_RIGHT:{
               velX = 0;
               break;
          }

          case KeyEvent.VK_LEFT:{
              velX = 0;
              break;
          }

          case KeyEvent.VK_UP:{
              velY = 0;
              break;
          }

          case KeyEvent.VK_DOWN:{
              velY = 0;
          }



     }
     }

          public void keyPressed(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_RIGHT:{
               velX = 5;
               break;
          }

          case KeyEvent.VK_LEFT:{
              velX = -5;
              break;
          }

          case KeyEvent.VK_UP:{
              velY = -5;
              break;
          }

          case KeyEvent.VK_DOWN:{
              velY = 5;
          }


     }
}

         public void keyTyped(KeyEvent e) {}
         public void actionPerformed(ActionEvent e) {}


            // Gets x
         public int getX() {
         return x;
}

           // Gets y
         public int getY() {
         return y;
}
           // Sest x
         public void setX(int x) {
         this.x = x;
}

          // Sets y
        public void setY(int y) {
        this.y = y;
}


}

Shot.java:

  // Includes
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

          // Shot class
       public class Shot implements KeyListener{

        // Shot variables
       private Image shotimage;
       private URL url;
         // Shot speed
       //private int velshotx = 0;
      // private int velshoty = 0;
        // Player position
       private int x;
       private int y;


        // Visible or not
       private boolean visible = false;

Player player;



         // Shot contrustor loads image
       public Shot(Main main, Player player){
         this.player = player;
         url = main.getDocumentBase();
         shotimage = main.getImage(url, "shot.png");

} 


          // update emthod
      public void update(Main main){
        main.addKeyListener(this);

}      
        // paints image
      public void paint(Graphics g, Main main){
          if(visible){
          g.drawImage(shotimage, player.getX(), player.getY(), main);
          String variable = String.valueOf(player.getX());
          g.drawString(variable, 10, 10);
          }

}

            // Gets x
         public int getX() {
         return x;
}

           // Gets y
         public int getY() {
         return y;
}
           // Sest x
         public void setX(int x) {
         this.x = x;
}

          // Sets y
        public void setY(int y) {
        this.y = y;
}



             // Key detector
          public void keyReleased(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_SPACE:{
               //velshotx  = 0;
               break;
          }

     }
}

          public void keyPressed(KeyEvent e){
          switch(e.getKeyCode()){
          case KeyEvent.VK_SPACE:{
               visible = true;
               break;
          }

     }
}

         public void keyTyped(KeyEvent e) {}
         public void actionPerformed(ActionEvent e) {}



}
Member Avatar for champmanking

Can anyone see anything wrong with the code?

Member Avatar for champmanking

Guess not, hmmmm ...

It's not that. You ignored my post 5 days ago about your serious problem with adding your listeners. You obviously did not follow my advice about debugging 3 days ago - despite trying to fob me off with vague claims that you did. That's why I stopped wasting my time. I suspect many others saw how this thread was going, and came to the same conclusion.

Member Avatar for champmanking

I did debug as best as I knew (new to java), and they were being called, unless I missed something ... so I posted my code for others to check if they wanted to help.

OK, one last try.
Your symptom is that when you press Space you don't get the results on the screen that you expect.

Put a print statement in your key listener to confirm that it is being called when you expect.
Put a print statement inside the part that tests for the space key to confirm that you are detecting the right key correctly.
Put a print statement in the Shot paint method to confirm that that is being called (print the "visible" variable to see if that has the expected value)

See what those tell you, then try some more of your own. Confirm every step of your code by printing the vraiables.

Member Avatar for champmanking

Ok, so, when I press SPACE "space" text appears (so it works, partially), but not the shot image.

The boolean visible returns correctly, also.

It's hard to print in an applet

My conclusion is that this line isn't working (most important):

g.drawImage(shotimage, player.getX(), player.getY(), main);

The rest of the code in that method does:

        // paints image
      public void paint(Graphics g, Main main){
          if(visible){
          g.drawImage(shotimage, player.getX(), player.getY(), main);
          String variable = String.valueOf(player.getX());
          g.drawString(variable, 10, 10);
          g.drawString("space", 10, 20);
          }

}

Good luck
J

Member Avatar for champmanking

No thanks,
C.

Oh, man! This place is very helpful.

Member Avatar for champmanking

Bump. Anyone know a solution?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.