graphics part very big, scrollpane, buffering graphics object?

My situation is like this. I draw quite a big jpanel (approx. 5000 * 1000 pixel) with some rectangles and ellipses (no other components are painted) in the paint method in order to look at parts of it afterwards through a viewport/scollpane.

the drawing works fine, but there remain still two problems.

I) The panel is not properly repainted when i call the repaint method, each time the scrollbars are being clicked. But if the application runs in a jframe i can repaint it indirectly properly when i resize the jframe. So painting works, but calling the repaint method manually doesnt work.

II) It takes quite a long time to draw the whole panel, because there are several loops which it has to run through and all in all it paints about 3000 rectangles. So my aim would be to draw everything one time into an object (e.g maybe a buffered image) and afterwards i only want to scroll over this image. Is something like this possible. How can i save the graphics object into an buffered image or something similar? Thanks a lot...

[1056 byte] By [derlangsame] at [2007-9-27 19:44:09]
# 1

I don't have much help on (I), other than trying repaint() on the scroll pane, or trying invalidate() on the panel.

On (II), you can simply create a BufferedImage of the desired size, get its Graphics (getGraphics()), and use it to paint all your objects just as you paint them onto a panel. On the panel, you then paint this BufferedImage (using drawImage()). It will use a bit of memory though (expect around 15-20 MBytes for a 5000 x 1000 pixel image).

MartinRoth at 2007-7-6 23:05:18 > top of Java-index,Security,Cryptography...
# 2

Instead of extending a JPanel, extend a JComponent. Also, for a swing component overide the paintComponent method, not the paint method. Creating an Image or BufferedImage object for this is not a real good idea. At 5000 * 1000 pixels and 24 bit color depth your looking at an Image which takes up 14.6Mb or memory.

kterr1 at 2007-7-6 23:05:19 > top of Java-index,Security,Cryptography...
# 3

Thanks very much!

Most of your suggestions i ve already tried out, unfortunately it didn' work, but i found now another solution (if your interested). I use a class library which is for free (ACME free GIF encoder (www.acme.com)). I make something like a screenshot of my panel and once this gif is stored in a buffered stream each time the paint method is called it paints from then on the gif. this is now very fast.

it goes like this (createImage is located in the panel which lies in the scrollpane and which contains the big amount of graphic data):

protected void createImage(){

try {

BufferedImage image = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

this.paint(g);

GifEncoder encoder = new GifEncoder(image, _bs);

encoder.encode();

_bs.close(); // _bs is a ByteArrayOutputStream

} catch(Exception e) {

System.out.println("Problems at createImage");

}

byte[] b = _bs.toByteArray();

ImageIcon i = new ImageIcon(b);

_img = i.getImage();

_imgReady=true;

}

and the paint method of this component looks like this:

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

if(!imgReady) {

g2.setColor(Color.white);

g2.fillRect(0,0,this.getWidth(),this.getHeight());

drawTableGrid(g2);

drawTesterIds(g2);

drawXAxisStuff(g2);

drawBlocks(g2);

}

else {

g2.drawImage(_img,0,0,this);

}

derlangsame at 2007-7-6 23:05:19 > top of Java-index,Security,Cryptography...
# 4

Have you tried using the ScrollMode property of your viewport ?

Something like that:

myScrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE)

or

myScrollPane.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE)

The first one does use a full bitmap to store your document, while the second one does only repaint the newly-visible part.

Laurent.

jeanpier19 at 2007-7-6 23:05:19 > top of Java-index,Security,Cryptography...
# 5
yes i did, but it didnt work, i don't know why. but no matter, the problem is solved :) thx
derlangsame at 2007-7-6 23:05:19 > top of Java-index,Security,Cryptography...