I append the full source code[1] but the painting part is:

private class PanelMap extends JPanel {

        @Override
        public void paint(Graphics g) {
            try {
                Graphics2D g2d = (Graphics2D) g;
                Rectangle rectangle = new Rectangle(getSize());

                //System.out.println("Map size (px): "+ rectangle.height
+ "x" + rectangle.width);

                renderer.paint(g2d, rectangle,
mapContext.getLayerBounds());
            } catch (IOException ex) {

Logger.getLogger(PanelGeoMap.class.getName()).log(Level.SEVERE,
null, ex);
            }
        }
}

I override the JPanel's paint method. PanelMap is inside a JScrollPane
and the renderer, in theory, only have to draw the visible viewport.


P.S.- I'm working in hardcoding the CRS for the a test.

[1]
-- full code start
public class PanelGeoMap extends JPanel implements MouseMotionListener {

    private class PanelMap extends JPanel {

        @Override
        public void paint(Graphics g) {
            try {
                Graphics2D g2d = (Graphics2D) g;
                Rectangle rectangle = new Rectangle(getSize());

                //System.out.println("Map size (px): " +
rectangle.height
+ "x" + rectangle.width);

                renderer.paint(g2d, rectangle,
mapContext.getLayerBounds());
            } catch (IOException ex) {

Logger.getLogger(PanelGeoMap.class.getName()).log(Level.SEVERE,
null, ex);
            }
        }
    }

    private PanelMap panelMap;
    private JScrollPane scrollPaneMap;

    private Dimension preferredSizeMaximum;

    private CoordinateReferenceSystem crs;
    private GTRenderer renderer;
    private Envelope env;
    private DefaultMapContext mapContext;

    public PanelGeoMap() {
        renderer = new StreamingRenderer();

        File file = null;
        JFileChooser fc = new JFileChooser();
        int ret = fc.showOpenDialog(this);

        if (ret == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
        } else {
            System.exit(0);
        }

        AbstractGridCoverage2DReader reader;
        try {
            //reader = new GeoTiffReader(file, new
Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
            reader = new ImageMosaicReader(file, new
Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
        } catch (IOException ex) {
            ex.printStackTrace();
            return;
        }

        GridCoverage2D coverage;
        try {
            coverage = (GridCoverage2D) reader.read(null);
        } catch (IOException ex) {
            ex.printStackTrace();
            return;
        }

        crs = coverage.getCoordinateReferenceSystem();
        env = coverage.getEnvelope();
        RenderedImage image = coverage.getRenderedImage();

        StyleBuilder styleBuilder = new StyleBuilder();

        ColorMap cm = styleBuilder.createColorMap(
                new String[]{"0", "255"},
                new double[]{0, 255},
                new Color[]{ new Color(0, 0, 0),
                             new Color(255, 255, 255)},
                ColorMap.TYPE_RAMP);                    // XXX: ?


        RasterSymbolizer rs = styleBuilder.createRasterSymbolizer(cm,
1);
        Style st = styleBuilder.createStyle(rs);

        mapContext = new DefaultMapContext(crs);
        mapContext.addLayer(reader, st);
        renderer.setContext(mapContext);

        RenderingHints hints = new
RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
        renderer.setJava2DHints(hints);
        Map renderParams = new HashMap();
        renderParams.put("optimizedDataLoadingEnabled", new
Boolean(true));
        renderer.setRendererHints(renderParams);

        int width = image.getWidth();
        int height = image.getHeight();

        /* Debug. Speed compare */
        JFrame frameJAI = new JFrame("DisplayJAI");
        frameJAI.getContentPane().add(new JScrollPane(new
DisplayJAI(image)));
        frameJAI.setVisible(true);

        System.out.println("Image size: " + width + "x" + height);

        panelMap = new PanelMap();
        panelMap.addMouseMotionListener(this);
        preferredSizeMaximum = new Dimension(width, height);
        panelMap.setPreferredSize(preferredSizeMaximum);

        scrollPaneMap = new JScrollPane(panelMap);

scrollPaneMap.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scrollPaneMap.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPaneMap.setAutoscrolls(true);

        add(scrollPaneMap, BorderLayout.CENTER);
    }

    public void zoomIn() {
        Dimension preferredSizeActual = panelMap.getPreferredSize();

        panelMap.setPreferredSize(new
Dimension(preferredSizeActual.width
- 2000, preferredSizeActual.height - 2000));
        panelMap.revalidate();
    }

    public void zoomOut() {
        Dimension preferredSizeActual = panelMap.getPreferredSize();
    }

    public void mouseDragged(MouseEvent e) {
        System.out.println("Dragging " + e.getX() + " " + e.getY());
        Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
        scrollPaneMap.scrollRectToVisible(r);
    }

    public void mouseMoved(MouseEvent e) {

    }
}
-- full code end

On mar, 2007-12-11 at 12:42 -0800, Jody Garnett wrote:
> Diego Fdez. Durán wrote:
> > I don't know if this questions are addressed to me but...
> >
> > Now I'm doing this[1], passing the reader directly to the
> > StreamingRenderer. I don't know how StreamingRenderer works internally.
> > I could download the StreamingRenderer from the SVN and take a look if
> > you want (but the original dev probably knows better than me how to
> > improve the renderer). I need to get the optimal performance and I'll be
> > glad if I can help in the improvement of GeoTools in the process.
> >
> > Thanks.
> >   
> So two observations:
> 1) This part looks to be slow; can you hard code the CRS for a moment 
> and see if you get a speed up? Not sure a good way to get the CRS but 
> possible we can ask Simone to store it in the metadata the reader can 
> get access to?
> > GridCoverage2D coverage;
> >         try {
> >             coverage = (GridCoverage2D) reader.read(null);
> >         } catch (IOException ex) {
> >             ex.printStackTrace();
> >             return;
> >         }
> >
> >         crs = coverage.getCoordinateReferenceSystem();
> >         RenderedImage image = coverage.getRenderedImage();
> 2) This part looks fine
> >         mapContext = new DefaultMapContext(crs);
> >         mapContext.addLayer(reader, st);
> >         renderer.setContext(mapContext);
> But I want to know what happens next; when you use the renderer to draw 
> (or paint) what are you painting onto?
> Jody
> 
> 
> 
> 
-- 
Diego Fdez. Durán <[EMAIL PROTECTED]> | http://www.goedi.net
GPG : 925C 9A21 7A11 3B13 6E43 50DB F579 D119 90D2 66BB

Attachment: signature.asc
Description: Esta parte del mensaje está firmada digitalmente

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to