001/* 002 * $Id: PainterPaint.java 4082 2011-11-15 18:39:43Z kschaefe $ 003 * 004 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library 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 GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021package org.jdesktop.swingx.painter; 022 023import java.awt.Graphics2D; 024import java.awt.Paint; 025import java.awt.PaintContext; 026import java.awt.Rectangle; 027import java.awt.RenderingHints; 028import java.awt.Transparency; 029import java.awt.geom.AffineTransform; 030import java.awt.geom.Rectangle2D; 031import java.awt.image.BufferedImage; 032import java.awt.image.ColorModel; 033import java.awt.image.Raster; 034 035import org.jdesktop.swingx.util.GraphicsUtilities; 036 037/** 038 * 039 * @author Karl George Schaefer 040 */ 041public class PainterPaint<T> implements Paint { 042 protected static class PainterPaintContext<T> implements PaintContext { 043 private Painter<T> painter; 044 private T object; 045 private BufferedImage saved; 046 047 public PainterPaintContext(Painter<T> painter, T object) { 048 painter.getClass(); // null check 049 this.painter = painter; 050 this.object = object; 051 } 052 053 /** 054 * {@inheritDoc} 055 */ 056 @Override 057 public void dispose() { 058 //does nothing 059 } 060 061 /** 062 * {@inheritDoc} 063 */ 064 @Override 065 public ColorModel getColorModel() { 066 if (saved == null) { 067 return GraphicsUtilities.createCompatibleImage(1, 1).getColorModel(); 068 } 069 070 return saved.getColorModel(); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 @Override 077 public Raster getRaster(int x, int y, int w, int h) { 078 if (saved == null || saved.getWidth() != w || saved.getHeight() != h) { 079 saved = GraphicsUtilities.createCompatibleImage(w, h); 080 Graphics2D g2d = saved.createGraphics(); 081 082 try { 083 if (painter instanceof AbstractPainter) { 084 ((AbstractPainter<?>) painter).setInPaintContext(true); 085 } 086 painter.paint(g2d, object, w, h); 087 } finally { 088 g2d.dispose(); 089 if (painter instanceof AbstractPainter) { 090 ((AbstractPainter<?>) painter).setInPaintContext(false); 091 } 092 } 093 } 094 095 return saved.getData(); 096 } 097 } 098 099 private final PainterPaintContext<T> context; 100 101 public PainterPaint(Painter<T> painter, T object) { 102 context = new PainterPaintContext<T>(painter, object); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, 110 Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) { 111 return context; 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public int getTransparency() { 119 return Transparency.BITMASK; 120 } 121}