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/