Avatar of SaiSeng
SaiSeng asked on

How to draw image on top most Layer?

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

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

Why not just add an Image in an ImageIcon in a JLabel?
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
SaiSeng

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

thanks
CEHJ

Yes. where it's marked 'box' should be a JLabel with your image in it, as shown in the above example
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Webstorm

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
  }

Webstorm

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

Webstorm

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
      }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

>>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
Webstorm

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
     }
Webstorm

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();}
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
CEHJ

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
ASKER
SaiSeng

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);
 */
CEHJ

:-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.