0

I'm developing some kind of videogame, so I am not interested in calling paint, repaint or any sort of those methods on each updating from keylistening, since they call also the update(Graphics g) method which cleans the whole screen. That's why I do want to @Override the update method, not allowing it to cleaning the screen at first. Doing this I can update what I want when I want.

However, sometimes it goes in a loop auto painting the components (such as jButtons) cleaning the screen(I couldn't track anything special happening whenever this occurs, I have already tried overriding some methods in order to catch which one's the troublesome and I couldn't find it, I'm likely missing something). I do not want this happening, because this auto painting cleans the screen which makes me draw everything once again. Moreover I don't feel comfortable with something looping until the player press any key. Do you have any clue? One solution could simply be using a timer with a boolean that each time a screen is completed and the next one is being loaded it calls update(g) from my JFrame (which contains the jPanel). But I would like something better..

Maybe I am doing something wrong, even if I tried to improve my painting methods thousand times surfing throughout the net and netbeans's suggs.

This is how my painting methods looks with some flags written and the ones that are called after running:

@Override public void paint(java.awt.Graphics g){
    paintComponents(g);
    System.out.println("Flag");
    update(g);
}

@Override
public synchronized void update(Graphics g)
{
    g.setColor(java.awt.Color.GREEN);
    w.getDrawer().draw(g);
    g.drawRect (0, 0, w.getActive().getW(), w.getActive().getH());
}`

The overriding on paint(Graphics g) method is not needed at all, just did it in order to see what was going on. I never called repaint() but update(getGraphics()) and it just spam Flags all around. Also thought that maybe I was making it run in a loop with paint and paintComponents, but deleting paintComponents(g) line helps not at all.

Any help would be welcome, since I am trying to make this project "serious". Thanks in advance. Sergi.

By the way, w.getDrawer().draw(g); is just calling some entities (like 100) with something like g.drawImage(image, locationX, locationY, null) inside. I don't think it has anything to do with my prob.

Ssr1369
  • 348
  • 3
  • 16

1 Answers1

2

..this auto painting cleans the screen which makes me draw everything once again.

Draw it to a BufferedImage displayed in a JLabel. When the data (the image) updates call label.repaint().

E.G. as seen in:

  1. This answer to How to draw an image over another image?



  2. This answer to Dynamic Graphics Object Painting.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you, it is definetly useful for another stuff further in my project, but not currently. The painting is working fine, but it cleans up alone for an unknown reason to me. Then I need to paint manually (with a button which I press when the unknown stuff finished cleaning the screen). What you are suggesting would simply change the behaviour of my button, but still I need to make it work after something unknown, so I need to keep doing it manually (or else with a timer). It is a good answer but is not helping on the main question: Why is it cleaning automatically and how can I stop it? – Ssr1369 Feb 25 '14 at 10:55
  • *"after something unknown"* Wait, ..what? Or rather. **Why** is it 'not known'? Is the application making the changes/updates in *your* control? – Andrew Thompson Feb 25 '14 at 11:04
  • Yes, as far as I know! I'm just calling all I already said, but I'm afraid I'm not so familiar with update, paint and so on, since they are almost all given by the api. I'm just using this update, as I was using the paint and repaint before. But I don't know wether the api is calling any method for GUI drawing on a synchronous way and I make it stop by calling my update, or whatever. I just have some controled "events" which change for example a boolean and when this boolean is changed I call the entity.draw(g) method which does something like g.drawImage(image,locationx,locationy,null). – Ssr1369 Feb 25 '14 at 11:09
  • With unknown I meant unknown for me. Is the jPanel being uploaded on its own (or better, should it be?) after some time? Because it is was it seems to happen. Checked like 5 times that I was not making it happen, before posting and even now. So i'm pretty sure it is not something I programmed to happen but it's happening. That's why I assume it could be something with the API. I even deleted all the calls I made and it's still autodoing stuff. – Ssr1369 Feb 25 '14 at 11:16
  • 1
    *"jPanel being uploaded on its own (or better, should it be?).."* Oh right I get you, the updates to the view triggered by the JVM whenever it considers it might need updating. This might occur if it goes behind, or comes in front of, other windows, as well as other times as deemed necessary. But that is where the beauty is in using an image in a label. The JVM can refresh the image almost instantly. The calculations to *change the image* is what I was talking about as being in your control. Then when your code changes the image, call repaint and voila, all taken care of. – Andrew Thompson Feb 25 '14 at 11:22