We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to draw image on top most Layer?

SaiSeng
SaiSeng asked
on
Medium Priority
659 Views
Last Modified: 2012-08-14
my java file is @ http://www.geocities.com/giasens/files/JavaBingo.zip

i have created CardLayout,
inside a "card", i hav many inner BorderLayout(s)
now, i just realise i couldnt drawImage on my top most layer...

can any one HELP me?? thanks.. ..

btw, can anyone suggest me where to learn paint() ??
i couldnt get what's repaint() about...

thanks again.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Why not just add an Image in an ImageIcon in a JLabel?
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
btw, the following image shows what i want to paint on
http://www.geocities.com/giasens/files/myBox.JPG

thanks
CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes. where it's marked 'box' should be a JLabel with your image in it, as shown in the above example
Top Expert 2004

Commented:
Hi SaiSeng,

Your links seems not good : "This page is not available. " or "Sorry, the page you requested was not found. "

>> i just realise i couldnt drawImage on my top most layer...
  public void paint(Graphics g)
  {
     g.drawImage(img,0,0,null); // Image img already loaded
  }

Top Expert 2004

Commented:
>> Your links seems not good
In fact your links are good if we first visit your home page : http://www.geocities.com/giasens/

Top Expert 2004

Commented:
Replace:
      public void paint(Graphics g)
      {
             g.drawImage(box, 80, 80, this);
      }

By:
      public void paint(Graphics g)
      {
                      g.drawImage(box, 80, 80, this);
                      super.paint(g); // for lightweight child components
      }
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>public void paint(Graphics g)

Actually that is not the way to do things if using Swing. See here for how to:

https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20927185.html
Top Expert 2004

Commented:
By:
     public void paint(Graphics g)
     {
            g.drawImage(box, 20, 140, this);  // 20,140 as in myBox.jpg
            super.paint(g); // for lightweight child components
     }
Top Expert 2004

Commented:
You can wait until the image is loaded:

   public void init()
   {
        box = getImage(getCodeBase(), "box.jpg");
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(box, 0);
        try{
        tracker.waitForAll();
        }catch(Exception e){e.printStackTrace();}
CERTIFIED EXPERT
Top Expert 2016

Commented:
Using an ImageIcon will get you out of all the little gotchas caused by Java loading its images aynchronously, apart from saving you having to rewrite your JPanel as a JPanel subclass

Author

Commented:
paint doesnt help... but Jlabel...
thanks anyway ;)
now i'm trying to addMouseListener on the icon ;)

/*
 * ImageIcon icon = new ImageIcon("box.jpg");
 * JLabel label = new JLabel(icon);
 */
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.