Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

JPanelGraphics..

Thread view: 
6e - 19 Aug 2005 21:40 GMT
Hi thanks for trying to help.

Im trying to create a JPanel that contains a RenderedImage, that can be
added to a JFrame.

I extended the JPanel class to be able to "draw" the Image that I pass
to it on each paint of the component, however, when I add it to the
JFrame, I only see a blank JPanel.

I thought that if I took out the (super) calls, that it may correct the
problem, but then it just shows up as a tiny empty JPanel...  same
thing happens if I take out setPreferredSize(new Dimension(400,200));

Code follows, thanks for you sage opinions!

------------
public class JPanelGraphics extends JPanel{

    RenderedImage myGraphics;

    JPanelGraphics (RenderedImage g){
        super();
        myGraphics = g;
    }

    public void paintComponent(Graphics2D g) {
        super.paintComponent(g);
        g.drawRenderedImage(myGraphics,
AffineTransform.getTranslateInstance(0,0 ));
    }
}
----------
main()
{
..........
JPanelGraphics pgGraphics = new JPanelGraphics(riGraphic);
pgGraphics.setPreferredSize(new Dimension(400,200));
panInputGrid.add(pgGraphics);

//add components to frame
jfrWindow.getContentPane().add(pgGraphics);

..........
}
Knute Johnson - 20 Aug 2005 18:04 GMT
> Hi thanks for trying to help.
>
[quoted text clipped - 40 lines]
> ..........
> }

6e:

You need the setPreferredSize() to tell the LayoutManager how big to
make the JPanel.  That's why it is a little tiny box, the default layout
manager, BorderLayout, smashes it.  You don't need any of the calls to
super because you are doing your own painting.  If you don't need a
RenderedImage, just use BufferedImage.

import java.awt.*;
import java.awt.image.*;

public class ImagePanel extends JPanel {
    BufferedImage image;

    ImagePanel(BufferedImage bi) {
        image = bi;
        int w = bi.getWidth();
        int h = bi.getHeight();
        setPreferredSize(new Dimension(w,h));
    }

    public void paintComponent(Graphics g) {
        g.drawImage(image,0,0,this);
    }
}

knute...
email s/nospam/knute/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.