Link to home
Create AccountLog in
Avatar of desiboy1974
desiboy1974

asked on

applet looks blurry or flickers

has anyone seen this?...when i lauch my applet, the iamges appear over another etc..as if its out of place..if i then minimise the applet and then maximise it, its all fine..or if i switch to another window and then back to it , its all fine..
Avatar of zzynx
zzynx
Flag of Belgium image

Repainting problem(s)
You're not mixing AWT and Swing are you?
Any code?
Avatar of JohnnyAffa
JohnnyAffa

post your paint method
All of the above ;-)
And check you have no operations holding up the EDT
Avatar of desiboy1974

ASKER


jPanel3.setBackground(new java.awt.Color(255,255,255));
        jPanel3 = new JImagePanel(img6);
        jPanel1.add(jPanel3);
        jPanel3.setBounds(400, 400, 280, 150);

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.ImageObserver;
import java.applet.Applet;
public class JImagePanel extends JPanel
{
   private Image img;

   public JImagePanel(Image i)
   {
      this.img = i;
     
   }
 public Dimension getPreferredSize()
   {
      return new Dimension(5+img.getWidth(null), 5+img.getHeight(null));
     
   }


   public void paint(Graphics g)
   {
       //super.paint(g);
       g.drawImage(img, 10, 10, this);
       
   }
}
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
With Swing, you override paintComponent, not paint
The problem is probably elsewhere - possibly in something holding up the EDT
So:

   public void paintComponent(Graphics g)
   {
       super.paintComponent( g );
       g.drawImage(img, 10, 10, this);
   }
>>
should be

  public void paintComponent(Graphics g)
>>

LOL - missed that ;-)
>> With Swing, you override paintComponent, not paint
Well,... you should only override paint() for top level containers (eg. JFrame)
> LOL - missed that ;-)

:-)  I almost did too...  

> Well,... you should only override paint() for top level containers (eg. JFrame)

True, as they don't extend JComponent...
tried that..same problem..
public void paintComponent(Graphics g)
   {
       super.paint(g);
       g.drawImage(img, 10, 10, this);
       
   }
      super.paint(g);

should be

       super.paintComponent(g);

like I said...
>       super.paint(g);

will probably start a loop that will cause a StackOverflowException...
If it isn't that, then it could be a probalem with your image loading (the image may not be loaded by the time the panel appears, and so getPreferredSize goes a bit mad...

Can you post your code where you load the image?
>>tried that..same problem..

I suspected it wouldn't fix the problem totally, but it's nonetheless true. Have you any application threads running in event handlers?
thanks..it worked fine now..
:-)  Cool  :-)

Good luck with it :-)

Tim