0

What I want is that press the " A", " S ", "D " and " W " the character move in their respective directions keys.

The problem is that every time I change direction , I change the image of JLabel ( it is an arrow ) .

It really works well but every time the image is changed, the JLabel back for a second to its default position . Then the JLabel continues from where it was.

Here is the code.

private int pX;
private int pY;

public MovePj() {
     initComponents();
     pX=labelPj.getX();
     pY=labelPj.getY();  }

private void formKeyPressed(KeyEvent evt) {            

    switch(evt.getKeyCode()){
        case 87:                                                                                    //Norte
            pY=pY-movimiento;
            labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\07-north.png"));
            break;
        case 83:                                                                                    //Sur
            pY=pY+movimiento;
            labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\03-sur.png"));
            break;
        case 68:                                                                                    //Este
            pX=pX+movimiento;
            labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\01-east.png"));
            break;
        case 65:                                                                                    //Oeste
            pX=pX-movimiento;
            labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\05-west.png"));
            break;
        default:
            break;
    }
    labelPersonaje.setLocation(pX, pY);
   //labelPj.setBounds(pX, pY, labelPj.getWidth(), labelPj.getHeight());

}

The character moves correctly, but to change the image of JLabel , this returns to the preset point.

Thanks.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Thratos
  • 3
  • 2
  • Try providing a runnable example of your problem – MadProgrammer Feb 18 '15 at 08:49
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 18 '15 at 09:52
  • It would be useful to see the code relating to the label's container. – Andrei Vajna II Feb 18 '15 at 09:53
  • @AndreiVajnaII It would be more useful to see a runnable example (as suggested in the first and second comments) that uses images we can all see (as suggested in the 2nd comment). – Andrew Thompson Feb 18 '15 at 09:55

1 Answers1

0

The setIcon(...) method will invoke:

revalidate();
repant();

on the label which causes the layout manager to be invoked. So I would guess you need to use:

panel.setLayout( null );

to prevent the size/location of the label from being recalculated. You would do this when you create the panel.

Also, it is not a good idea to read the images in response to the KeyEvent. You should read the images when the class is loaded for better performance.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for all, and thank you camickr, cause your answer solved my issue. That's all, just calling "this.getContentPane().setLayour(null);" i fixed it. Thanks again – Thratos Feb 23 '15 at 08:08