public JavaCanvas (Graphics gfx, JavaImage image) { super(gfx, image); g2d = image.bufferedImage().createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); float scale = image.scale().factor; g2d.scale(scale, scale); // push default state stateStack.push(new JavaCanvasState()); // All clears go to rgba(0,0,0,0). g2d.setBackground(new Color(0, true)); }
@Override public float height() { return (img == null) ? preHeight : super.height(); }
@Override public float width() { return (img == null) ? preWidth : super.width(); }
@Override public Image snapshot() { BufferedImage bmp = ((JavaImage)image).bufferedImage(); ColorModel cm = bmp.getColorModel(); boolean isAlphaPremultiplied = bmp.isAlphaPremultiplied(); WritableRaster raster = bmp.copyData(null); BufferedImage snap = new BufferedImage(cm, raster, isAlphaPremultiplied, null); return new JavaImage(gfx, image.scale(), snap, "<canvas>"); }
@Override public void paint (Graphics g) { g.drawImage(image.bufferedImage(), 0, 0, null); } }
@Override protected ImageImpl createImage (boolean async, int rwid, int rhei, String source) { return new JavaImage(plat, async, rwid, rhei, source); }
@Override public int ensureTexture() { // if we have a canvas, and it's dirty, force the recreation of our texture which will obtain // the latest canvas data if (canvas.dirty()) { canvas.clearDirty(); refreshTexture(); } return super.ensureTexture(); }
@Override protected Pattern toSubPattern(AbstractImageGL<?> image, boolean repeatX, boolean repeatY, float x, float y, float width, float height) { assert isReady() : "Cannot generate a pattern from unready image."; // we have to account for the scale factor when extracting our subimage BufferedImage subImage = img.getSubimage( scale.scaledFloor(x), scale.scaledFloor(y), scale.scaledCeil(width), scale.scaledCeil(height)); Rectangle2D rect = new Rectangle2D.Float(0, 0, width, height); return new JavaPattern(image, repeatX, repeatY, new TexturePaint(subImage, rect)); }
@Override public void draw (Object ctx, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh) { // adjust our source rect to account for the scale factor float f = scale().factor; sx *= f; sy *= f; sw *= f; sh *= f; // now render the image through a clip and with a scaling transform, so that only the desired // source rect is rendered, and is rendered into the desired target region float scaleX = dw/sw, scaleY = dh/sh; Graphics2D gfx = (Graphics2D)ctx; Shape oclip = gfx.getClip(); gfx.clipRect(MathUtil.ifloor(dx), MathUtil.ifloor(dy), MathUtil.iceil(dw), MathUtil.iceil(dh)); gfx.drawImage(img, new AffineTransform(scaleX, 0f, 0f, scaleY, dx-sx*scaleX, dy-sy*scaleY), null); gfx.setClip(oclip); }
@Override public Image snapshot() { BufferedImage bmp = ((JavaImage)image).bufferedImage(); ColorModel cm = bmp.getColorModel(); boolean isAlphaPremultiplied = bmp.isAlphaPremultiplied(); WritableRaster raster = bmp.copyData(null); BufferedImage snap = new BufferedImage(cm, raster, isAlphaPremultiplied, null); return new JavaImage(gfx, image.scale(), snap, "<canvas>"); }
/** * Sets the window icon. * * Takes icons of different sizes, preferring earlier ones in case of duplicate sizes. */ public void setIcon (playn.core.Image... icons) { assert icons.length > 0; List<BufferedImage> images = new ArrayList<BufferedImage>(); for (playn.core.Image icon : icons) images.add(((JavaImage)icon).bufferedImage()); _frame.setIconImages(images); }
@Override protected ImageImpl createImage (boolean async, int rwid, int rhei, String source) { return new JavaImage(plat, async, rwid, rhei, source); }
@Override public int ensureTexture() { // if we have a canvas, and it's dirty, force the recreation of our texture which will obtain // the latest canvas data if (canvas.dirty()) { canvas.clearDirty(); refreshTexture(); } return super.ensureTexture(); }
@Override protected Pattern toSubPattern(AbstractImageGL<?> image, boolean repeatX, boolean repeatY, float x, float y, float width, float height) { assert isReady() : "Cannot generate a pattern from unready image."; // we have to account for the scale factor when extracting our subimage BufferedImage subImage = img.getSubimage( scale.scaledFloor(x), scale.scaledFloor(y), scale.scaledCeil(width), scale.scaledCeil(height)); Rectangle2D rect = new Rectangle2D.Float(0, 0, width, height); return new JavaPattern(image, repeatX, repeatY, new TexturePaint(subImage, rect)); }