Repainting at a specific rectangle on an image

Has anyone tried using the method repaint(int, int, width, height) method to only paint at the specific place? I tried doing that but the image smudge when i moved my player. I have to minimize it and maximize it back for it to show a clean image at an updated position.

Can anyone help me with some hints?

[319 byte] By [Hanz_05a] at [2007-11-27 10:51:09]
# 1

repaint() of which class? Component?

CeciNEstPasUnProgrammeura at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 2

repaint() of the paint() method. My class extends a Canvas.

Hanz_05a at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 3

> repaint() of the paint() method. My class extends a Canvas.

So it's AWT code, not Swing?

BigDaddyLoveHandlesa at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes it a AWT component....

Hanz_05a at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 5

> Yes it a AWT component....

The graphics may be better in Swing. Do you have to do it in the old AWT?

BigDaddyLoveHandlesa at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 6

The question to me is: do you mix Swing and AWT components?

CeciNEstPasUnProgrammeura at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 7

Either one if can work...So whats the different between Swing and AWT? Does the swing uses paintComponent() rather than paint()?

Hanz_05a at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 8

> The question to me is: do you mix Swing and AWT components?

Yes, if you're doing that, stop!

BigDaddyLoveHandlesa at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 9

I am not sure if am doing the right thing......

public void paint(Graphics g)

{

//codes

}

Then i just called the repaint method somewhere in my code

repaint(int , int, width, height);

Hanz_05a at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 10

> Either one if can work...So whats the different

> between Swing and AWT? Does the swing uses

> paintComponent() rather than paint()?

Swing double-buffers by default which may fix your repainting woes.

I also think it is better supported and optimized in the later versions,

while AWT...?

Method paint still exists, after all, JComponent is in the Component hierarchy.

However, in JComponent, paint calls paintComponent, paintBorder and paintChildren.

Typically, if you want a custom component you extend JPanel (not Canvas)

and override paintComponent, not paint:

protected void paintComponent(Graphics g) {

super.paintComponent(g);

//do you rendering here

}

BigDaddyLoveHandlesa at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 11

Ok... I will try that.. thanks.. is the super.paintComponent(g) necessary?

Hanz_05a at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...
# 12

> Ok... I will try that.. thanks.. is the

> super.paintComponent(g) necessary?

You could comment it out and see what happens :-)

In JPanel, it will fill the component's bounds with the background color.

BigDaddyLoveHandlesa at 2007-7-29 11:29:29 > top of Java-index,Java Essentials,Java Programming...