Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java.awt question

  • 29-09-2001 01:11PM
    #1
    Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭


    How do you paint an Image over a Panel? I'm trying to display an image on a panel but the image is drawn behind the panel not in front of it. :mad:

    Any idea's?


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I am assuming that you are adding that Panel to a Frame and the image is being drawn on the Frame (behind the Panel), yeah?

    Right, this is what I would do,

    Create a new class that inherits from java.awt.Panel . Override it's paint method, with something like this
    public void paint(Graphics g){
            Image img = Toolkit.getDefaultToolkit().getImage(file_string);        
            g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
    }
    

    Then simply add this Panel to your Frame. The kewl thing about this is if, your Panel get's resized the image will scale accordingly.

    ;-phobos-)


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    or if you don't need a whole class for the panel, you can just get it's graphics context and draw to that, like this:
    Panel p=new Panel();
    Graphics g=p.getGraphics();
    g.drawImage(....);
    

    Panels can be a bit strange so I'm not 100% sure if this works, but it should.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Yes that would work alright.

    Why do I always think that ppl will want to make several instance of something. Bad phobos, BAD!!

    ;-phobos-)


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Neither of thos are working for me, but they look sound. I'll review my code and let you know.

    thanks


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    try adding a Canvas to the panel and then draw to the canvas (the same way as above).


  • Advertisement
Advertisement