001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.renderer;
034:
035: import java.awt.Dimension;
036: import java.awt.Graphics2D;
037: import java.awt.Image;
038: import java.awt.RenderingHints;
039: import java.awt.image.ImageObserver;
040:
041: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
042:
043: /**
044: * Reading and writing can be done on separate threads.
045: */
046: public class ThreadSafeImage implements Cloneable {
047: private Image image = null;
048: private Graphics2D graphics = null;
049: private LayerViewPanel panel;
050: private GraphicsState dummyGraphicsState = new GraphicsState() {
051: public void restore(Graphics2D g) {
052: }
053: };
054:
055: public ThreadSafeImage(LayerViewPanel panel) {
056: this .panel = panel;
057: }
058:
059: private Image getImage() {
060: if (image == null) {
061: image = panel.createBlankPanelImage();
062: }
063:
064: return image;
065: }
066:
067: private Graphics2D getGraphics() {
068: if (graphics == null) {
069: graphics = (Graphics2D) getImage().getGraphics();
070: //Not sure if we need antialiasing here. Oh well, doesn't hurt. [Jon Aquino 11/21/2003]
071: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
072: RenderingHints.VALUE_ANTIALIAS_ON);
073: }
074:
075: return graphics;
076: }
077:
078: public synchronized void copyTo(Graphics2D destination,
079: ImageObserver imageObserver) {
080: if (getImage() == null) {
081: //Nothing we can do. [Jon Aquino]
082: return;
083: }
084:
085: destination.drawImage(getImage(), 0, 0, imageObserver);
086: }
087:
088: /**
089: * @return false if we cannot generate an off-screen panel from the panel
090: * because of various conditions during initialization
091: */
092: private boolean isPanelReady() {
093: if (panel.getSize().equals(new Dimension(0, 0))) {
094: return false;
095: }
096:
097: if (getImage() == null) {
098: return false;
099: }
100:
101: return true;
102: }
103:
104: public synchronized void draw(Drawer drawer) throws Exception {
105: if (!isPanelReady()) {
106: return;
107: }
108: Graphics2D g = (Graphics2D) image.getGraphics();
109: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
110: RenderingHints.VALUE_ANTIALIAS_ON);
111: drawer.draw(g);
112: }
113:
114: public synchronized GraphicsState getGraphicsState() {
115: if (!isPanelReady()) {
116: return dummyGraphicsState;
117: }
118:
119: return new GraphicsState(getGraphics());
120: }
121:
122: public synchronized void setGraphicsState(GraphicsState gs) {
123: if (!isPanelReady()) {
124: return;
125: }
126:
127: gs.restore(getGraphics());
128: }
129:
130: /**
131: * If the panel is not ready, returns null.
132: */
133: public Object clone() {
134: ThreadSafeImage clone = new ThreadSafeImage(panel);
135:
136: if (!clone.isPanelReady()) {
137: return null;
138: }
139:
140: copyTo(clone.getGraphics(), null);
141:
142: return clone;
143: }
144:
145: public interface Drawer {
146: public void draw(Graphics2D g) throws Exception;
147: }
148: }
|