Skip to main content

Basic Direct Draw Question - What Function to call for painting

5 replies [Last post]
vinaykagarwal
Offline
Joined: 2006-01-14
Points: 0

This is probably too basic but I am confused about it. If using direct draw, each drawing element may be defined something like this

gui = new Container() {
public void paint(Graphics g) {
g.setFont(font);
g.setColor(new Color(255, 10, 10));
g.fillRect(20, 20, getWidth() - 40, getHeight() - 40);
g.setColor(new Color(245, 245, 245));
int message_width = g.getFontMetrics().stringWidth(message);
g.drawString(message, (getWidth() - message_width) / 2, 500);
}
};

If this component is to be redrawn, should this paint() function be called? I know repaint() has lot of side effects like redrawing background. I would appreciate clarification.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vinaykagarwal
Offline
Joined: 2006-01-14
Points: 0

Jay thanks. My question was more to whether another part of the application should call paint() directly or should call update() or repaint(). Here's what I have figured out so far

- Before calling paint, one needs to know if the component is actually visible. The getGraphics() does return null if not. If the components are not actually added to containers, then this check would need to be implemented.

- Overriding update() method to simply call paint() and then calling repaint() from other parts of the application does fix the background clearing but not independent thread problem. In cases where the painting is acceptable from another thread, this is OK method.

Message was edited by: vinaykagarwal

elbob
Offline
Joined: 2008-10-01
Points: 0

That helped - thank you!
Components have to be added to HScene (directly or indirectly) AND it has to be visible
to get a valid Graphics Object.

jscons
Offline
Joined: 2009-02-01
Points: 0

With Direct Draw, you acquire the Graphics object yourself:

Graphics g = gui.getGraphics();

Unlike Repaint Draw, Direct Draw gives you more control over when the screen is refreshed. Better for games, animation, etc. Your method would be rewritten to something like this:

public void paint()
{
Graphics g = gui.getGraphics();
g.setFont(font);
g.setColor(new Color(255, 10, 10));
g.fillRect(20, 20, getWidth() - 40, getHeight() - 40);
g.setColor(new Color(245, 245, 245));
int message_width = g.getFontMetrics().stringWidth(message);
g.drawString(message, (getWidth() - message_width) / 2, 500);
g.dispose();
}
...and you call paint() any time you want to update your textfield. (Although you may want to consider another method name that doesn't confuse the 2 drawing models... something like 'doScreenDraw' or 'renderToScreen' - just a suggestion.)

This is obvisously a very basic example. You'll ultimately want to implement double buffering and possibly constraining your redraws to the area of the screen that requires updating as your projects get more complex. But hopefully this gets you started.

Best,

- Jay

elbob
Offline
Joined: 2008-10-01
Points: 0

I'm trying to optimize my App by using Direct Draw but I can't aquire the base Graphics Object.

I'm creating a HScene and add a HContainer to it.
Usually I used the paint Method and the Graphics Object I got there for drawing.

Now I tried HContainer.getGraphics to create my own draw Methods...

Unfortunately I always get a java.awt.NullGraphics Object which I can't use...
HScene.getGraphics returns the same.

How to I aquire my "Base Graphics Object" that is connected to the Screen without using the paint method...

Thanks a lot!

vinaykagarwal
Offline
Joined: 2006-01-14
Points: 0

You get null graphics object from getGraphics() call when the component is not connected to HScene (directly or indirectly). Make sure your component is added to the HScene object.