Link to home
Start Free TrialLog in
Avatar of TheFred
TheFred

asked on

Emergency!

Hi folks, i'm in a hurry
I'm writting kind of a game in java , my first. Everything went ok with the swing components (Y), but here is my problem, my consists on a robot who moves inside of an array. i do have a JPanel working with a GridBagLayout Manager to represent the array, then I use this panel as a component of a bigger panel together with some other components, that big panel is going to be the container of my JFrame.
    That's working good, but know y listen to the keyboard and receive a movement, (ok) i make the changes inside of the array (ok) but when i call to my paintarray method that first created the pane it seems to work ok , but I keep looking at the same thing , it does not refresh, i tried using container.repaint() panel.repaint() but doesn't work the code i use to PaintArray looks like :
public void Dibujar (JPanel pane, CCasilla[][] ma){
   int i, j;
   pane.removeAll();
   GridBagConstraints Rejillas = new GridBagConstraints();
   Rejillas.gridwidth=Rejillas.gridheight=1;
   for (i = 0 ; i < _fi; i ++){
           for ( j = 0 ; j <_co;j++){
         Rejillas.gridx = j;
         Rejillas.gridy = i;
         if (ma[j][i] == null){
            pane.add(new JLabel(cas),Rejillas);
         }
         else{
            pane.add(new JLabel(ma[j][i].getImagen()),Rejillas);
            if ((ma[j][i] instanceof CBeeper)||(ma[j][i] instanceof CRobot)){
                     pane.add(new JLabel(cas),Rejillas);
                                }
         }
           }
}
            
Need help !! how can I force my JFrame to show the changes in my Jpanel . And another question Im Using public class CGame extends JFrame implements KeyListener as the clase in which i listen the keyboard, i don't want to use the word implements ..... any other way ?? . ANYWAY the first question is the BIG ONE please HELP            
            
Avatar of RuadRauFlessa
RuadRauFlessa

Have you tried looking at overriding the paint method and manually painting your sprite since it will be much faster and smoother and you can incorporate alot more from there.

If you still want to keep it like it is at the moment rather add a JLabel to every grid component and simply set and remove the icon's of the labels to ma[j][i].getImagen().

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TheFred

ASKER

objects, first one... worked fine thx, second one... how can i do that? When I compile this class "public class CGame extends JFrame implements KeyListener"  works fine when using sdk 1.3 but not as good when using 1.4 , the second one captures no keystroke. I think it's a problem of focus maybe to the frame ... how can i fix that?
you need to add your keylistener to whatever component has focus at the time you are interested.