2

I'm developing in BlueJ a simple game, it's like a "Space invaders". My problem is that, when I move an object, like the UFOs with a KeyListener, the old UFOs don't disappear!

To update the frame I do:

public static void updateJframe()
{
  SwingUtilities.updateComponentTreeUI(canvas);
}

canvas is my variable from my class Canvas that extends JFrame.

If more code is needed I have no problem to post it.

Ok sorry for post the code in the comments, I'm a noob here too :$

public class Inout 
{

    private static Canvas canvas;
    private Motor motor;

    public Inout()
    {
        motor = new Motor(); //in class "Motor" is the logic of the game
       canvas = new Canvas();
    }

    public static void actualizar() //this is the method where i'm trying to update the                     frame
    {
        canvas.removeAll();
        SwingUtilities.updateComponentTreeUI(canvas);
    }

    public class Canvas extends JFrame implements KeyListener 
    {


        public Canvas ()
        {
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
            setTitle("Space Invaders");
            setSize(1200,600);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        public void paint(Graphics g)
        {
            int i = 0;
            int j = 0;
            for (i = 0; i < 30 ; i++)
            {
                for (j = 0; j < 50; j++)
                {
                    if (Motor.cuadricula[i][j].esUfo())    
                    {
                        g.setColor (Color.blue);
                        g.fillRect(i*40,j*20,20,15);   
                    }
                    else if (Motor.cuadricula[i][j].esDef())    
                    {
                        g.setColor (Color.red);
                        g.fillRect(i*40,j*20,20,15);}
                    }
                }
            }
        }   
        public void keyPressed(KeyEvent e)
        {}

        public void keyReleased(KeyEvent e)
        {
            int keyCode = e.getKeyCode();
            if(keyCode == KeyEvent.VK_P){
                Motor.moverDef(1);//mover una casilla a la derecha

            }
            else if(keyCode == KeyEvent.VK_O){
                Motor.moverDef(0);//mover una casilla a la izquierda
                Inout.actualizar();
            } 
        }

        public void keyTyped (KeyEvent e){}
    }
}

The topic that i can't understand is: if I use the "actualizar);" method from any other method it works fine, but if do it from the KeyListener the bug appear.

Many thanks for your help!!

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Yes post some code. How do you draw your objects? By overriding the paint method? Anyway, you should rather use repaint() to refresh your UI. – Guillaume Polet May 05 '12 at 11:18
  • 5
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Override `paintComponent()` in a `JPanel` and add it to the frame. 3) Call `super.paintComponent(Graphics);` before painting, to erase earlier drawings. – Andrew Thompson May 05 '12 at 11:27
  • Sorry, i don't know how to pack my code for you to read it. I can upload it to dropbox for example? – user1376672 May 05 '12 at 11:44
  • 3
    It's unlikely anyone will want to wade though such a large example. An [sscce](http://sscce.org/) is typically less than a hundred lines. Your current code is an inappropriate use of `updateComponentTreeUI()`. – trashgod May 05 '12 at 11:49
  • 1
    *"pack my code for you to read it."* If you want **me** to read it, make an SSCCE and edit it into the question. Note that very few people will follow links or look at code that is longer than can be posted on SO. – Andrew Thompson May 05 '12 at 11:50
  • 2
    See also this [answer](http://stackoverflow.com/a/10440350/230513) on `KeyListener` and this animation [Q&A](http://stackoverflow.com/q/9849950/230513). – trashgod May 05 '12 at 11:56
  • Yes i override the paint Method. SSCCE: public class Inout { private static Canvas canvas; public Inout() { canvas = new Canvas();} public static void actualizar() { canvas.removeAll(); SwingUtilities.updateComponentTreeUI(canvas); } public class Canvas extends JFrame implements KeyListener { public Canvas () { addKeyListener(this); setFocusable(true); setSize(1200,600); setVisible(true); } public void paint(Graphics g) { *HERE I USE A ITERATOR TO DRAW ALL THE UFO } public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_P){ Motor.moverDef(1); – user1376672 May 05 '12 at 12:07
  • 1
    @user1376672: please don't post code in a comment section (the code loses formatting and is unreadable as you can see) but instead delete the comment above, edit your original post, and then post the code in the original post. Then post a comment notifying us of your edit. – Hovercraft Full Of Eels May 05 '12 at 13:02
  • 1
    To follow on what @HovercraftFullOfEels already said those are just bits and pieces of code which doesn't really tell us anything useful. For example, let us have a sample of a method or something where you expect the problem is (by editing it into your question). BTW I gave you (+1) as an encouragement, I know how hard it is to start. :) – Boro May 05 '12 at 13:06
  • Sorry, i'm new posting here too. There is my SSCCE code but it is pretty much my raw code. Thanks!! – user1376672 May 05 '12 at 13:43
  • 1
    Please re-read or actually read the [sscce](http://sscce.org) link. Don't post this stuff and call it an sscce, when clearly it is not. Yes you will need to put in some effort to create a true sscce, and it's totally up to you whether you want to do it or not, since it's not required. But the question to ask is how much do you need our help? If you need it badly, then a true compliant SSCCE is one of the best ways to allow us to understand your problem and be able to help you fix it. – Hovercraft Full Of Eels May 05 '12 at 13:55
  • I should not really be doing this, since you are not paying close attention to the help that is being offered, but change `public void paint(Graphics g) {..` to `public void paint(Graphics g) { super.paint(g); ..` And if that fails to 'fix the problem' (there are many more shown even in the single unindented class so far shown), post an ***SSCCE*** as linked from a comment, and referred to by most of the people who have commented. – Andrew Thompson May 05 '12 at 15:32
  • OK I understand, this is not a place for a noob person. Looks like I didn't understand what is a SSCCE. I've read the link about this and I thought a SSCCE was an example about your code without all the things that are irrelevant. Well, here it is. I don't know where is the problem. Thanks anyway for your help even when you "shouldn't help". I'm trying to learn and I'm trying to do my best to explain my problem according to the rules of this site and I didn't understand so far de SSCCE link. – user1376672 May 05 '12 at 15:55
  • Andrew, many thanks for your comment. There was the problem, with your tip the bug is solved. My apologises if I didn't explain my code in the right way. Where was the mistake? Where can I find more examples about SSCCE – user1376672 May 05 '12 at 15:58
  • Do check the edit, and if wrong revert it back :(. – nIcE cOw May 05 '12 at 16:39
  • The key to an SSCCE is it is small code that we can run and compile unmodified and without need of outside resources (databases, files, images) on our systems at home. Your code fulfilled none of this. If you need to do this again, and you still are unsure about the SSCCE requirements, go through them again, step by step, and *ask* for clarification of any portion of the requirement. – Hovercraft Full Of Eels May 05 '12 at 17:10

1 Answers1

1

Good way: Use paintComponent method instead of paint and you won't have problems with old objects not disappearing @ repaint.

Bad way: Fill background every time paint is called (at the method beginning) - it will cover everything you have painted before.

Mikle Garin
  • 10,083
  • 37
  • 59