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:
- 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 ?
- 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.