/* * * 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 java2d;
/** * Surface is the base class for the 2d rendering demos. Demos must * implement the render() method. Subclasses for Surface are * AnimatingSurface, ControlsSurface and AnimatingControlsSurface.
*/
@SuppressWarnings("serial") publicabstractclass Surface extends JPanel implements Printable {
public Object AntiAlias = VALUE_ANTIALIAS_ON; public Object Rendering = VALUE_RENDER_SPEED; public AlphaComposite composite; public Paint texture; public String perfStr; // PerformanceMonitor public BufferedImage bimg; publicint imageType; public String name; publicboolean clearSurface = true; // Demos using animated gif's that implement ImageObserver set dontThread. publicboolean dontThread; public AnimatingSurface animating; protectedlong sleepAmount = 50; privatelong orig, start, frame; private Toolkit toolkit; privateboolean perfMonitor, outputPerf; privateint biw, bih; privateboolean clearOnce; privateboolean toBeInitialized = true;
public Surface() {
setDoubleBuffered(thisinstanceof AnimatingSurface);
toolkit = getToolkit();
name = this.getClass().getSimpleName();
setImageType(0);
// To launch an individual demo with the performance str output : // java -Dj2ddemo.perf= -cp J2Ddemo.jar demos.Clipping.ClipAnim try { if (System.getProperty("j2ddemo.perf") != null) {
perfMonitor = outputPerf = true;
}
} catch (Exception ex) {
} if (thisinstanceof AnimatingSurface) {
animating = (AnimatingSurface) this;
}
}
private BufferedImage createBinaryImage(int w, int h, int pixelBits) { int bytesPerRow = w * pixelBits / 8; if (w * pixelBits % 8 != 0) {
bytesPerRow++;
} byte[] imageData = newbyte[h * bytesPerRow];
IndexColorModel cm = null; switch (pixelBits) { case 1:
cm = new IndexColorModel(pixelBits, lut1Arr.length,
lut1Arr, lut1Arr, lut1Arr); break; case 2:
cm = new IndexColorModel(pixelBits, lut2Arr.length,
lut2Arr, lut2Arr, lut2Arr); break; case 4:
cm = new IndexColorModel(pixelBits, lut4Arr.length,
lut4Arr, lut4Arr, lut4Arr); break; default:
Logger.getLogger(Surface.class.getName()).log(Level.SEVERE, null, new Exception("Invalid # of bit per pixel"));
}
DataBuffer db = new DataBufferByte(imageData, imageData.length);
WritableRaster r = Raster.createPackedRaster(db, w, h, pixelBits, null); returnnew BufferedImage(cm, r, false, null);
}
private BufferedImage createSGISurface(int w, int h, int pixelBits) { int rMask32 = 0xFF000000; int rMask16 = 0xF800; int gMask32 = 0x00FF0000; int gMask16 = 0x07C0; int bMask32 = 0x0000FF00; int bMask16 = 0x003E;
DirectColorModel dcm = null;
DataBuffer db = null;
WritableRaster wr = null; switch (pixelBits) { case 16: short[] imageDataUShort = newshort[w * h];
dcm = new DirectColorModel(16, rMask16, gMask16, bMask16);
db = new DataBufferUShort(imageDataUShort,
imageDataUShort.length);
wr = Raster.createPackedRaster(db, w, h, w, newint[] { rMask16, gMask16, bMask16 }, null); break; case 32: int[] imageDataInt = newint[w * h];
dcm = new DirectColorModel(32, rMask32, gMask32, bMask32);
db = new DataBufferInt(imageDataInt, imageDataInt.length);
wr = Raster.createPackedRaster(db, w, h, w, newint[] { rMask32, gMask32, bMask32 }, null); break; default:
Logger.getLogger(Surface.class.getName()).log(Level.SEVERE, null, new Exception("Invalid # of bit per pixel"));
}
returnnew BufferedImage(dcm, wr, false, null);
}
public Graphics2D createGraphics2D(int width, int height,
BufferedImage bi,
Graphics g) {
Graphics2D g2 = null;
if (bi != null) {
g2 = bi.createGraphics();
} else {
g2 = (Graphics2D) g;
}
if (texture != null) { // set composite to opaque for texture fills
g2.setComposite(AlphaComposite.SrcOver);
g2.setPaint(texture);
g2.fillRect(0, 0, width, height);
}
if (composite != null) {
g2.setComposite(composite);
}
return g2;
}
// ...demos that extend Surface must implement this routine... publicabstractvoid render(int w, int h, Graphics2D g2);
/** * It's possible to turn off double-buffering for just the repaint * calls invoked directly on the non double buffered component. * This can be done by overriding paintImmediately() (which is called * as a result of repaint) and getting the current RepaintManager and * turning off double buffering in the RepaintManager before calling * super.paintImmediately(g).
*/
@Override publicvoid paintImmediately(int x, int y, int w, int h) {
RepaintManager repaintManager = null; boolean save = true; if (!isDoubleBuffered()) {
repaintManager = RepaintManager.currentManager(this);
save = repaintManager.isDoubleBufferingEnabled();
repaintManager.setDoubleBufferingEnabled(false);
} super.paintImmediately(x, y, w, h);
if (repaintManager != null) {
repaintManager.setDoubleBufferingEnabled(save);
}
}
@Override publicvoid paint(Graphics g) {
super.paint(g);
Dimension d = getSize();
if (biw != d.width || bih != d.height) {
toBeInitialized = true;
biw = d.width;
bih = d.height;
}
publicstaticvoid createDemoFrame(Surface surface) { final DemoPanel dp = new DemoPanel(surface, new DemoInstVarsAccessorImplBase());
Frame f = new Frame("J2D Demo - " + surface.name);
f.addWindowListener(new WindowAdapter() {
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.