Results 1 to 10 of 10

Thread: Apply Image as background to JPanel [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Resolved Apply Image as background to JPanel [RESOLVED]

    How can I apply an image as a background to a JPanel? I can do this fine with the graphic object, but it's drawing is temporary... so I thought of getting to do a new class extending JPanel...

    what I got so far is this:

    Code:
    package rukupaint;
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class clsPaintPanel extends JPanel {
        
        /** Creates a new instance of clsPaintPanel */
        public clsPaintPanel() {
        }
    
        protected void paintComponent (Graphics g) {
            super.paintComponent (g);
    
            if (this.image != null) {
                Graphics2D g2d = (Graphics2D) g;
                int iw = this.image.getWidth(this);
                int ih = this.image.getHeight(this);
                int w = iw != 0 ? iw : 1;
                int h = ih != 0 ? ih : 1;
                double sx = ((double) getWidth()) / ((double) w);
                double sy = ((double) getHeight()) / ((double) h);
                AffineTransform at = new AffineTransform ();
    
                if (this.maintainAspectRatio) {
                    sx = sy = Math.max(sx, sy);
                }
                
                at.scale(sx, sy);
                g2d.drawImage(this.image, at, this);
            }
        }
    }
    I've seen this on another forum, I do not claim the code to be mine, I just need to understand how to logically solve my problem...

    But there doesn't seem to be an "image" property to the JPanel...

    help on this would be appreciated!!!

    (By the way if there is some object I havn't seen that runs a background picture property, don't hesitate sharing the class/object, thx!!!)
    Last edited by Ruku; May 31st, 2006 at 03:49 AM.

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Apply Image as background to JPanel

    you should add
    Code:
    private Image image;
        private boolean maintainAspectRatio;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Apply Image as background to JPanel

    Quote Originally Posted by Ruku
    How can I apply an image as a background to a JPanel? I can do this fine with the graphic object, but it's drawing is temporary... so I thought of getting to do a new class extending JPanel...

    what I got so far is this:

    Code:
    package rukupaint;
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class clsPaintPanel extends JPanel {
        
        /** Creates a new instance of clsPaintPanel */
        public clsPaintPanel() {
        }
    
        protected void paintComponent (Graphics g) {
            super.paintComponent (g);
    
            if (this.image != null) {
                Graphics2D g2d = (Graphics2D) g;
                int iw = this.image.getWidth(this);
                int ih = this.image.getHeight(this);
                int w = iw != 0 ? iw : 1;
                int h = ih != 0 ? ih : 1;
                double sx = ((double) getWidth()) / ((double) w);
                double sy = ((double) getHeight()) / ((double) h);
                AffineTransform at = new AffineTransform ();
    
                if (this.maintainAspectRatio) {
                    sx = sy = Math.max(sx, sy);
                }
                
                at.scale(sx, sy);
                g2d.drawImage(this.image, at, this);
            }
        }
    }
    I've seen this on another forum, I do not claim the code to be mine, I just need to understand how to logically solve my problem...

    But there doesn't seem to be an "image" property to the JPanel...

    help on this would be appreciated!!!

    (By the way if there is some object I havn't seen that runs a background picture property, don't hesitate sharing the class/object, thx!!!)

    You paint the image... Simply add this class and use it in your other class.

  4. #4

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Apply Image as background to JPanel

    Still can't solve this, sorry if I've been unclear... What I want is just to draw an image that sticks to the object... such as .net's picturebox...

    since there is no picturebox in Java I thought of using a jPanel, but when I move a window over what I've drawned the image disapears!

    So how can I do this sticky image thing?




    To ComputerJy: My class is working, yeah I forgot some declarations in the code i've pasted...!!

    To System_Error: I did, but what happens is that all my image gets white and nothing is drawned... o_O
    Last edited by Ruku; May 29th, 2006 at 07:22 PM.

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Apply Image as background to JPanel

    I found this link. it might be usefull
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Apply Image as background to JPanel

    thx for the link... but I don't understand what exacly makes the image sticky within the paintComponent(Graphics g) sub.... o_O

    (Graphics2D g2d = (Graphics2D) g

    looks like the 2 most important lines are:

    super.paintComponent(g);
    g2d.drawImage(this.image, at, this);

    buuut it doesn't quite work... nothing gets drawned...

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Apply Image as background to JPanel

    Did you change the file name?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Apply Image as background to JPanel

    uh, no, what filename?

    I got my class with the constructor working well...

    the only thing I didn't get is that ImageFill class within the link you gave me... can't find it... and I've imported everythin' they did...
    Last edited by Ruku; May 30th, 2006 at 02:12 PM.

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Apply Image as background to JPanel

    In their code, the image is captured from a file "background.jpg" in the constructor, make sure you change that file to a valid image file.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Apply Image as background to JPanel

    Code:
        protected void paintComponent(Graphics g) {
            try {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, this);
            }
            catch (Exception ex) {
            }
        }
    got everything working...!

    I just needed to call the repaint(); method and everything was good!!!


    Last edited by Ruku; May 31st, 2006 at 03:49 AM.

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width