/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/OverlayXY.java * * * * This file is part of AndroidWorld. * * * * AndroidWorld is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation, either version * * 3 of the License, or (at your option) any later version. * * * * AndroidWorld is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with AndroidWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.image; import android.world.Posn; import android.graphics.Canvas; /** * * * Represents the Overlaying of two images with the given offset. * *
 *    new OverlayXY(new Rectangle(20, 20, "outline", "black"),
 *                20, 0, new Rectangle(20, 20, "outline", "black"))
* *
* *
 *    new OverlayXY(new Rectangle(20, 20, "solid", "red"),
 *                20, 20,
 *                new Rectangle(20, 20, "solid", "black"))
* *
* *
 *    new OverlayXY(new Rectangle(20, 20, "solid", "red"),
 *                -20, -20,
 *                new Rectangle(20, 20, "solid", "black"))
* *
* *
 *    new OverlayXY(
 *           new OverlayXY(new Ellipse(40, 40, "outline", "black"),
 *                 10, 15, new Ellipse(10, 10, "solid", "forestgreen")),
 *                 20, 15, new Ellipse(10, 10, "solid", "forestgreen"))
* *
*
* * Image overlays are also available as a method on Images. * *
 *    new Rectangle(20, 20, "outline", "black")
 *        .overlayxy(new Rectangle(20, 20, "outline", "black"), 20, 0)
* *
* *
 *    new Rectangle(20, 20, "solid", "black")
 *        .overlayxy(new Rectangle(20, 20, "solid", "red"), 20, 20)
* *
* *
 *    new Rectangle(20, 20, "solid", "black")
 *        .overlayxy(new Rectangle(20, 20, "solid", "red"), -20, -20)
* *
* *
 *    new Ellipse(10, 10, "solid", "forestgreen")
 *        .overlayxy(20, 15, new Ellipse(10, 10, "solid", "forestgreen")
 *                               .overlayxy(10, 15, new Ellipse(40, 40, "outline", "black"))
* *
* */ public class OverlayXY extends Overlay{ private double dx, dy; private double tx, ty; /** Construct an OverlayXY from the two images and the offset (X,Y) */ public OverlayXY(Image top, int x, int y, Image bot){ this(top,(double)x,y,bot); } /** Construct an OverlayXY from the two images and the Posn offset */ public OverlayXY(Image top, Posn p, Image bot){ this(top,p.x,p.y,bot); } /** Construct an OverlayXY from the two images and the offset (X,Y) */ public OverlayXY(Image top, double x, double y, Image bot){ super(top,bot, calcwidth(top,bot,x), calcheight(top,bot,y)); this.dx = x; this.dy = y; // Where to draw the Top image... this.tx = -pinholeX+top.pinholeX; if(top.width() > bot.width()+dx) this.tx = pinholeX-top.pinholeX; this.ty = -pinholeY+top.pinholeY; if(top.height() > bot.height()+dy) this.ty = pinholeY-top.pinholeY; } private static double calcwidth(Image t, Image b, double dx){ return Math.max(t.width(), b.width()+dx)-((dx < 0)?dx:0); } private static double calcheight(Image t, Image b, double dy){ return Math.max(t.height(), b.height()+dy)-((dy < 0)?dy:0); } /** Draw this OverlayXY image into a Graphics */ public void paint(Canvas c, int xx, int yy){ bot.paint(c, round(xx+tx+dx+bot.pinholeX-top.pinholeX), round(yy+ty+dy+bot.pinholeY-top.pinholeY)); top.paint(c, round(xx+tx), round(yy+ty)); } }