# [java] Most optimized way to paint this...

This topic is 4714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

First, I must say I have been working with Java for almost 2 (or is it 3?) years, but Im more like the GUI/logic coding guy; I have code very small amounts of graphics-related code, maybe one being a ball-bounce demo for a project at collegue (but thats for about the time I started with Java) and thats it. The problem I have right now is this one: Lets say I have a window, wheter fullscreen of not, I need it to cover the whole screen. The deal is this, I need to paint the background of the screen black. Why? Well, lets just say I have to given some requirements for this app. This is all done in a JFrame (suggestions to change the parent component are welcome) Then, I have an square or rectangle (way smaller than the whole screen) where Ill render some animated graphic, maybe a gif. This rectangle can be anywhere in the screen and can have any color, but the position is calculated before the painting begins, aka, it will stay in the same position all the time, but its position can be changed before starting the program. If the position would be always the same, I was thinking of some stupid way to do things, and that would be filling the screen around the rectangle with 2 or 3 JLabel, and then setting the background to black, so I dont have to worry about paint() or repaint() or anything like that at all, only for the rectangle. If I lost you, heres the recap: A black-background component covering the whole screen (possibly just fullscreen, and so far a JFrame). A rectangle, put in front of the component, where some painting (an image) will occur 24/7. The position of this rectangle is calculated before the painting begins (could be anywhere in the screen), but never changes during execution. So far, I use the paint() method to paint the JFrame background to black, and make the animation in the rectangle by means of a custom method inside a while(true) loop. Whatever I do inside the while() is not important now. The problem is, what is the most optimized way to do this, in terms of: 1. The root component I should use 2. Should the black background by set by the setBackground() method of the root component, or maybe I should use a canvas an some other method (remember the only thing that needs to be done is repaint it black IF and only IF required, because likely there wont be any windows on top of this one, there are not supossed to be any) 3. What double buffering strategy should I use (I read the one posted by capn_midnight in the FAQ) 4. What would be the best component to make the rectangle, a canvas perhaps? (so far, theres none, I just make a call to fillRect() method of the JFrame) 5. This is supossed to run in good machines (probably >=athlon 2600+), but theres no guarantee of a nice videocard, so no OpenGL stuff (LWJGL, JOGL from what I understand), no 3D at all. 6. What methos should I put the while(true) loop in, doing the repainting (paint(), repaint(), update(), custom) So far, I use an offscreen BufferedImage (IIRC, I got the idea from the java tutorial) where I paint the rectangle plus animation and then I paint it in the screen, something among these lines

//Inside Custom Method...

//before while loop
Graphics g=this.getGraphics(); //"this" is the JFrame
Graphics2D g2d=(Graphics2D)g;
BufferedImage bi=(BufferedImage)createImage(width,height);
Graphics2D big=bi.createGraphics();

//inside while loop
big.fillRect(...);
big.[draw lotsa stuff]

g2d.drawImage(bi,0,0,this);

---------------------------------

//Paint method of the JFrame
public void paint(Graphics g){
setBackground(Color.black);
}


Thanks

• 16
• 9
• 13
• 41
• 15