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..
Java

Avatar of undefined
Last Comment
TimYates

8/22/2022 - Mon
zzynx

Repainting problem(s)
CEHJ

You're not mixing AWT and Swing are you?
zzynx

Any code?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
JohnnyAffa

post your paint method
TimYates

All of the above ;-)
CEHJ

And check you have no operations holding up the EDT
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
TimYates

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TimYates

With Swing, you override paintComponent, not paint
CEHJ

The problem is probably elsewhere - possibly in something holding up the EDT
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TimYates

So:

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

>>
should be

  public void paintComponent(Graphics g)
>>

LOL - missed that ;-)
zzynx

>> With Swing, you override paintComponent, not paint
Well,... you should only override paint() for top level containers (eg. JFrame)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
TimYates

> 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...
desiboy1974

ASKER
tried that..same problem..
desiboy1974

ASKER
public void paintComponent(Graphics g)
   {
       super.paint(g);
       g.drawImage(img, 10, 10, this);
       
   }
Your help has saved me hundreds of hours of internet surfing.
fblack61
TimYates

      super.paint(g);

should be

       super.paintComponent(g);

like I said...
TimYates

>       super.paint(g);

will probably start a loop that will cause a StackOverflowException...
TimYates

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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

>>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?
desiboy1974

ASKER
thanks..it worked fine now..
TimYates

:-)  Cool  :-)

Good luck with it :-)

Tim
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes