/*
*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.geoforge.awt.java2d;
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import org.geoforge.java.awt.color.GfrColor;
import org.geoforge.java.awt.font.GfrUtilFont;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.KEY_RENDERING;
import org.geoforge.awt.graphics2d.GfrUtilGraphics2D;
/**
*
* modified by bantchao
*/
abstract public class MySurfaceAbs extends Panel
{
transient private Object _objAntiAlias_ = RenderingHints.VALUE_ANTIALIAS_ON;
transient private Object _objRendering_ = RenderingHints.VALUE_RENDER_SPEED;
transient private int biw, bih;
transient private boolean toBeInitialized = true;
transient private int imageType;
transient private BufferedImage bimg;
transient private boolean clearOnce;
transient private Toolkit toolkit;
transient private boolean clearSurface = true;
transient private Paint texture;
transient private AlphaComposite composite;
// ...demos that extend Surface must implement this routine...
abstract public void render(int w, int h, Graphics2D g2);
transient private String _strWallPaper_ = null;
protected MySurfaceAbs(String strWallPaper)
{
this._strWallPaper_ = strWallPaper;
toolkit = getToolkit();
TexturePaint textTexture = getTextTexture();
this.setTexture(textTexture);
this.setComposite(true);
this.setAntiAlias(true);
this.setRendering(true);
}
protected MySurfaceAbs()
{
this((String) null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension d = getSize();
if (biw != d.width || bih != d.height) {
toBeInitialized = true;
biw = d.width;
bih = d.height;
}
if (imageType == 1) {
bimg = null;
} else if (bimg == null || toBeInitialized) {
bimg = createBufferedImage((Graphics2D) g,
d.width, d.height, imageType - 2);
clearOnce = true;
}
/*if (toBeInitialized) {
if (animating != null) {
animating.reset(d.width, d.height);
}
toBeInitialized = false;
startClock();
}
if (animating != null && animating.running()) {
animating.step(d.width, d.height);
}*/
Graphics2D g2 = createGraphics2D(d.width, d.height, bimg, g);
render(d.width, d.height, g2);
g2.dispose();
if (bimg != null) {
g.drawImage(bimg, 0, 0, null);
toolkit.sync();
}
/*if (perfMonitor) {
LogPerformance();*/
}
public BufferedImage createBufferedImage(Graphics2D g2,
int w,
int h,
int imgType)
{
BufferedImage bi = null;
/*if (imgType == 0) {
bi = g2.getDeviceConfiguration().
createCompatibleImage(w, h);
} else if (imgType > 0 && imgType < 14) {
bi = new BufferedImage(w, h, imgType);
} else if (imgType == 14) {
bi = createBinaryImage(w, h, 2);
} else if (imgType == 15) {
bi = createBinaryImage(w, h, 4);
} else if (imgType == 16) {
bi = createSGISurface(w, h, 32);
} else if (imgType == 17) {
bi = createSGISurface(w, h, 16);
}*/
return bi;
}
public Graphics2D createGraphics2D(int width,
int height,
BufferedImage bi,
Graphics g) {
Graphics2D g2d = null;
if (bi != null) {
g2d = bi.createGraphics();
} else {
g2d = (Graphics2D) g;
}
g2d.setBackground(getBackground());
GfrUtilGraphics2D.s_setRenderingAntiAlias(g2d, _objAntiAlias_);
g2d.setRenderingHint(KEY_RENDERING, _objRendering_);
if (clearSurface || clearOnce) {
g2d.clearRect(0, 0, width, height);
clearOnce = false;
}
if (texture != null) {
// set composite to opaque for texture fills
g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(texture);
g2d.fillRect(0, 0, width, height);
}
if (composite != null) {
g2d.setComposite(composite);
}
return g2d;
}
public void setAntiAlias(boolean aa) {
_objAntiAlias_ = aa ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF;
}
public void setRendering(boolean rd) {
_objRendering_ = rd ? RenderingHints.VALUE_RENDER_QUALITY : RenderingHints.VALUE_RENDER_SPEED;
}
public void setTexture(Object obj) {
if (obj instanceof GradientPaint) {
texture = new GradientPaint(0, 0, GfrColor.WHITE_2,
getSize().width * 2, 0, GfrColor.green);
} else {
texture = (Paint) obj;
}
}
public void setComposite(boolean cp) {
composite = cp
? AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)
: null;
}
public TexturePaint getTextTexture()
{
String strText = this._strWallPaper_;
if (strText == null)
strText = System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel());
TextLayout tlt = new TextLayout(strText,
GfrUtilFont.s_get(Font.BOLD, 10),
new FontRenderContext(null,
false, false));
int sw = (int) tlt.getBounds().getWidth();
int sh = (int) (tlt.getAscent() + tlt.getDescent());
BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
Graphics2D tG2 = bi.createGraphics();
tG2.setBackground(GfrColor.WHITE_2);
tG2.clearRect(0, 0, sw, sh);
tG2.setColor(GfrColor.GRAY_8);
tlt.draw(tG2, 0, tlt.getAscent());
Rectangle r = new Rectangle(0, 0, sw, sh);
return new TexturePaint(bi, r);
}
}