001: package org.geotools.demo;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.io.File;
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009:
010: import javax.swing.JButton;
011: import javax.swing.JFileChooser;
012: import javax.swing.JFrame;
013: import javax.swing.JPanel;
014: import javax.swing.WindowConstants;
015: import javax.swing.filechooser.FileFilter;
016:
017: import org.geotools.factory.CommonFactoryFinder;
018: import org.geotools.gce.image.WorldImageReader;
019: import org.geotools.geometry.jts.ReferencedEnvelope;
020: import org.geotools.gui.swing.JMapPane;
021: import org.geotools.map.DefaultMapContext;
022: import org.geotools.map.MapContext;
023: import org.geotools.renderer.lite.StreamingRenderer;
024: import org.geotools.styling.FeatureTypeStyle;
025: import org.geotools.styling.RasterSymbolizer;
026: import org.geotools.styling.Rule;
027: import org.geotools.styling.Style;
028: import org.geotools.styling.StyleFactory;
029: import org.geotools.styling.Symbolizer;
030:
031: /**
032: * This example opens up a world plus image file.
033: *
034: * @author Jody Garnett
035: */
036: public class ImageLab2 {
037:
038: /**
039: * Prompt the user for a file and open up ImageLab.
040: * @param args filename of image
041: */
042: public static void main(String[] args) throws Exception {
043: File file = getImageFile(args);
044:
045: WorldImageReader reader = new WorldImageReader(file);
046: MapContext map = new DefaultMapContext(reader.getCrs());
047:
048: // set up style
049: Style style = createStyle();
050:
051: map.addLayer(reader.read(null), style);
052: map.setAreaOfInterest(new ReferencedEnvelope(reader
053: .getOriginalEnvelope()));
054:
055: showMap(map);
056: }
057:
058: private static Style createStyle() {
059: StyleFactory styleFactory = CommonFactoryFinder
060: .getStyleFactory(null);
061: RasterSymbolizer symbolizer = styleFactory
062: .createRasterSymbolizer();
063: Rule rule = styleFactory.createRule();
064: rule.setSymbolizers(new Symbolizer[] { symbolizer });
065: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
066: fts.setRules(new Rule[] { rule });
067: Style style = styleFactory.createStyle();
068: style.setFeatureTypeStyles(new FeatureTypeStyle[] { fts });
069: return style;
070: }
071:
072: private static void showMap(MapContext map) throws IOException {
073: final JMapPane mapPane = new JMapPane(new StreamingRenderer(),
074: map);
075: mapPane.setMapArea(map.getLayerBounds());
076: JFrame frame = new JFrame("ImageLab2");
077:
078: frame.setLayout(new BorderLayout());
079: frame.add(mapPane, BorderLayout.CENTER);
080: JPanel buttons = new JPanel();
081: JButton zoomInButton = new JButton("Zoom In");
082: zoomInButton.addActionListener(new ActionListener() {
083: public void actionPerformed(ActionEvent e) {
084: mapPane.setState(JMapPane.ZoomIn);
085: }
086: });
087: buttons.add(zoomInButton);
088:
089: JButton zoomOutButton = new JButton("Zoom Out");
090: zoomOutButton.addActionListener(new ActionListener() {
091: public void actionPerformed(ActionEvent e) {
092: mapPane.setState(JMapPane.ZoomOut);
093: }
094: });
095: buttons.add(zoomOutButton);
096:
097: JButton pamButton = new JButton("Move");
098: pamButton.addActionListener(new ActionListener() {
099: public void actionPerformed(ActionEvent e) {
100: mapPane.setState(JMapPane.Pan);
101: }
102: });
103: buttons.add(pamButton);
104:
105: frame.add(buttons, BorderLayout.NORTH);
106:
107: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
108: frame.setSize(600, 400);
109: frame.setVisible(true);
110: }
111:
112: private static File getImageFile(String[] args)
113: throws FileNotFoundException {
114: File file;
115: if (args.length == 0) {
116: JFileChooser chooser = new JFileChooser();
117: chooser.setDialogTitle("Open Image file");
118: chooser.setFileFilter(new FileFilter() {
119: public boolean accept(File f) {
120: return f.isDirectory()
121: || f.getPath().endsWith("jpg")
122: || f.getPath().endsWith("JPG");
123: }
124:
125: public String getDescription() {
126: return "JPEG";
127: }
128: });
129: int returnVal = chooser.showOpenDialog(null);
130:
131: if (returnVal != JFileChooser.APPROVE_OPTION) {
132: System.exit(0);
133: }
134: file = chooser.getSelectedFile();
135:
136: System.out.println("Opening Image file: " + file.getName());
137: } else {
138: file = new File(args[0]);
139: }
140: if (!file.exists()) {
141: throw new FileNotFoundException(file.getAbsolutePath());
142: }
143: return file;
144: }
145: }
|