001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.renderer.lite;
017:
018: import java.awt.Color;
019: import java.awt.Frame;
020: import java.awt.Graphics;
021: import java.awt.Graphics2D;
022: import java.awt.HeadlessException;
023: import java.awt.Panel;
024: import java.awt.Rectangle;
025: import java.awt.event.WindowAdapter;
026: import java.awt.event.WindowEvent;
027: import java.awt.geom.AffineTransform;
028: import java.awt.image.BufferedImage;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: import org.geotools.filter.FilterFactory;
033: import org.geotools.filter.FilterFactoryFinder;
034: import org.geotools.geometry.jts.ReferencedEnvelope;
035: import org.geotools.renderer.GTRenderer;
036: import org.geotools.test.TestData;
037:
038: /**
039: * DOCUMENT ME!
040: *
041: * @author Simone Giannecchini
042: */
043: public class RendererBaseTest {
044:
045: public RendererBaseTest() {
046:
047: }
048:
049: /**
050: * bounds may be null
051: *
052: * @param testName
053: * DOCUMENT ME!
054: * @param renderer
055: * DOCUMENT ME!
056: * @param timeOut
057: * DOCUMENT ME!
058: * @param bounds
059: * DOCUMENT ME!
060: *
061: * @throws Exception
062: * DOCUMENT ME!
063: */
064: protected static void showRender(String testName, Object renderer,
065: long timeOut, ReferencedEnvelope bounds) throws Exception {
066: int w = 300;
067: int h = 300;
068: final BufferedImage image = new BufferedImage(w, h,
069: BufferedImage.TYPE_INT_ARGB);
070: Graphics g = image.getGraphics();
071: g.setColor(Color.white);
072: g.fillRect(0, 0, w, h);
073: render(renderer, g, new Rectangle(w, h), bounds);
074:
075: final String headless = System.getProperty("java.awt.headless",
076: "false");
077: if (!headless.equalsIgnoreCase("true")
078: && TestData.isInteractiveTest())
079: try {
080: Frame frame = new Frame(testName);
081: frame.addWindowListener(new WindowAdapter() {
082: public void windowClosing(WindowEvent e) {
083: e.getWindow().dispose();
084: }
085: });
086:
087: Panel p = new Panel() {
088: /** <code>serialVersionUID</code> field */
089: private static final long serialVersionUID = 1L;
090:
091: public void paint(Graphics g) {
092: g.drawImage(image, 0, 0, this );
093: }
094: };
095:
096: frame.add(p);
097: frame.setSize(w, h);
098: frame.setVisible(true);
099:
100: Thread.sleep(timeOut);
101: frame.dispose();
102: } catch (HeadlessException exception) {
103: // The test is running on a machine without X11 display. Ignore.
104: return;
105: }
106:
107: boolean hasData = false; // All I can seem to check reliably.
108:
109: for (int y = 0; y < h; y++) {
110: for (int x = 0; x < w; x++) {
111: if (image.getRGB(x, y) != -1) {
112: hasData = true;
113: }
114: }
115: }
116:
117: assert (hasData);
118: }
119:
120: /**
121: * responsible for actually rendering.
122: *
123: * @param obj
124: * DOCUMENT ME!
125: * @param g
126: * @param rect
127: * DOCUMENT ME!
128: * @param bounds
129: */
130: private static void render(Object obj, Graphics g, Rectangle rect,
131: ReferencedEnvelope bounds) {
132: if (obj instanceof GTRenderer) {
133: GTRenderer renderer = (GTRenderer) obj;
134:
135: if (bounds == null) {
136: renderer.paint((Graphics2D) g, rect,
137: new AffineTransform());
138: } else {
139: renderer.paint((Graphics2D) g, rect, bounds);
140: }
141: }
142: }
143: }
|