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 / February 2004

Tip: Looking for answers? Try searching our database.

Improving large image drawing

Thread view: 
oneforall-allforone - 10 Feb 2004 13:54 GMT
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.
ak - 10 Feb 2004 14:47 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.
> (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


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.