Results 1 to 10 of 10

Thread: Apply Image as background to JPanel [RESOLVED]

Threaded View

  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

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