Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel background/border problem

807569May 27 2006 — edited May 29 2006
Having a frustrating time with a simple subclass of JPanel. I subclassed JPanel so that I could overwite the paint method for scaling purposes. Here' s paint
 
        public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        // Compute image scale so that largest dim fits panel.
        float ih = image.getHeight(this);
        float iw = image.getWidth(this);
        float ar = ih/iw;
        // Scale the larger dim to fit panel
        float scale = 1.0f;
        if (ar > 1) scale = this.getHeight()/ih;
        else scale = this.getWidth()/iw;
        g2d.scale(scale, scale);
        g2d.drawImage(image,20,20,this);
    }
The problem is no matter what I try, I get a gray background (parent frame background) and no border. I have set the background in several places, in the test driver when the panel is created and added to a JFrame, in the constructor, and even in the paint routine. Nothing seems to effect the display. The image shows up scaled, but that part of the panel that is not image is still gray. If, in the constructor, I call isBackgroundSet() and getBackground(), they return "true" and "WHITE", but the background still shows ggggrrrrraaaayyyyy!

Anybody know what's wrong?

Thanks Mike
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2006
Added on May 27 2006
4 comments
246 views