Hi.
I'm trying to draw a very large image with Swing.
My input is an int array, that I would like to display as an image.
(each int is a RGB value)
The array is quite large, and I want to avoid creating a temporary
image
to draw to the offscreen buffer of a jpanel...
Here is the code I want to improve:
-----------------------------------------------------------
final class MyPanel extends JPanel {
private final MemoryImageSource mis;
private final FilteredImageSource fis;
private final AreaAveragingScaleFilter aas = new
AreaAveragingScaleFilter(100, 100);
// this is the constructor for my class.
// w is the image desired width
// h is the image desired height
// pixels is a w*h array with an rgb value.
public MyPanel(int w, int h, int[] pixels) {
// MemoryImageSource is a producer that feeds each
// of it's registered consumer with values of 'pixels'
mis = new MemoryImageSource(w, h, pixels, 0, w);
}
/**
* @param g
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// The method create image creates a temporary image
// which is drawn to the offscreen buffer of this panel.
g2.drawImage(getToolkit().createImage(mis), 0, 0, Color.BLACK,null);
}
}
-----------------------------------------------------------
This is not optimal, since the tempo image creation leads to an
OutOfMemory when
the pixels array is big.
To solve that problem, I would like to bypass the creation of a tempo
image.
This could be done if I had the offscreen image for the instance of
MyPanel.
I tryed using the RepaintManager, but w/o the results I hopped.
So if sbdy has any idea, I would greatly approciate.
> I'm trying to draw a very large image with Swing.
> My input is an int array, that I would like to display as an image.
> (each int is a RGB value)
> The array is quite large, and I want to avoid creating a temporary
> image to draw to the offscreen buffer of a jpanel...
for large images you should use JAI.
another way is NOT to create one big image from array, but only part it.
something like this (simplified):
int tileSize = 200;
MemoryImageSource mis;
Image img;
public MyPanel(int w, int h, int[] pixels) {
mis = new MemoryImageSource(tileSize, tileSize , pixels, 0, w);
mis.setAnimated(true);
img = Toolkit.getDefaultToolkit().createImage(mis);
}
//for repainting you should make loop over all tiles
public void paintComponent(Graphics g) {
super.paintComponent(g);
int tile = 0;
int xTileCount = w/ tileSize;
int yTileCount = w/ tileSize;
for(int i = 0; i< xTileCount; i++) {
for(int j = 0; j < yTileCount; j++) {
int offset = j * tileSize * w + i * tileSize;
mis.newPixels(pixels, offset, <ColorModel>, offset, w);
//paint img now;
g.drawImage(img, i*tileSize, j*tileSize, null);
}
}
}
--
____________
http://reader.imagero.com the best java image reader.
> Hi.
>
[quoted text clipped - 47 lines]
> I tryed using the RepaintManager, but w/o the results I hopped.
> So if sbdy has any idea, I would greatly approciate.
oneforall-allforone - 16 Feb 2004 12:07 GMT
> > I'm trying to draw a very large image with Swing.
> > My input is an int array, that I would like to display as an image.
[quoted text clipped - 3 lines]
>
> for large images you should use JAI.
In fact, I've already try to use JAI, but that doesn't match my design goals.
> another way is NOT to create one big image from array, but only part it.
>
[quoted text clipped - 27 lines]
> }
> }
Well, that seems to be the solution. Thanks a lot for your help ak