new to java2d (newbie questions)

I am sorry if most of the answers seem to be very newbie-like but this is actually the first thing I ever programmed in java2d with some graphic stuff.
I need those answers only for understanding purpose, I know I could write the code with my current knowledge but I want to improve and not remain on my current level.

So, here we go:

  1. What is the difference (if any) in using a Graphics object and directly painting on the java window ?

Let’s suppose I redeclare the paint(Graphics g) function and in this function I start drawing images with g.drawImage(my_img)
Well, as a result it will draw the image and it will be displayed.

Mostly I find always this approach:

We declare an Image and a Graphics Object.


      private Image bufferImage; 
      private Graphics bufferGraphics; 

then we are using this:


            System.gc();
              bufferImage=createImage(bufferWidth,bufferHeight);        bufferGraphics=bufferImage.getGraphics(); 

and instead of using drawImage on the Graphic object g of paint(Graphics g), we are actually painting on the Graphics object bufferGraphics we created (which seems to draw on bufferImage and then finally only draw the bufferImage.

Why are we using this approach ?
Why are we specifiing a second Graphics Object linked to an Image and finally only displaying this single image ?

  1. What is a good method to actually count FPS ?

I found this approach:


      private int fps;
      static int  count = 59;
      static long time2  = System.currentTimeMillis();

      public void paint(Graphics g){ 
                    // some stuff here
          FrameRate(g);
                }

      public void FrameRate(Graphics g)
      {
            if (++count == 60){
                  fps   = (int)(1000 / (float)((System.currentTimeMillis() - time2) / 60));
                  time2  = System.currentTimeMillis();
                  count = 0;
            }
            g.setColor(Color.blue);
            g.drawString("FPS: " + fps, 0, 16);
      }


I understand that each time we call paint(), we increment count and then, once 60 reached, are calculating the fps value. But is this acutally a good method or isn’t there another one ?

Thanks for your answers.

[quote][…]
Why are we using this approach ?
Why are we specifiing a second Graphics Object linked to an Image and finally only displaying this single image ?
[…]
[/quote]
That’s doublebuffering. We draw everything in a buffer and blit (copy) it over to the frontbuffer once we are done with drawing. This way you don’t see the drawing action itself… the clearing… drawing of individual sprites, lines, rectangeles… whatsoever. That is necessary in order to prevent flickering. (It works because we only draw one finished image over the old frame - the worst kind of artifacts you might be able to see is tearing)

But is this acutally a good method or isn’t there another one ?

It’s ok. Not that accurate if the framerate is unlikely high (3000+ fps with win9x) but that doesnt really matter does it? :slight_smile:

There are other ways like checking the time each frame… if the difference is >1000 msecs you calculate the framerate… but that needs more CPU power per frame than “your” method.

Btw it’s better to use BufferedImage instead of Image and Graphics2D instead of Graphics.

Btw² search the board for “Balls.jar” from Abuse. It’s a nice testing application, wich allows you to compare the speed of the different image types (source is included).

Oh and welcome to the board :slight_smile:

Hm maybe you should post your question in the

Newless Clubies Board ::slight_smile:

Sorry, won’t happen again. I thaught the newless clubies board was for general java questions and as my problems were all Java2D specific…

sorry again :-/

Hey… imo it was absolutly ok to post it in this board :slight_smile:

Btw if you still have any questions feel free to ask (here).

In addition… check the othere threads… there is alot of useful information out there :slight_smile:

Its a hard call in cases like this…

I’d say my rule of thumb is “if its so dirt simple ANY expereinced Java programmer would know the answer, post it in Clueless Newbies, if it requires someone with specific area knowledge to answer it then post it in that area.”

Its worth noting that the title of the forum is a bit deceptive. This isn’t a “Java2D” forum, but rather a “2D techniques in Java” which is a broader category that certainly includes Java2D. (Same really is true for Java3D though we have a Xith specific forum as well.)