/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * JavaWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./image/OverlayXY.java * * * * This file is part of JavaWorld. * * * * JavaWorld 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. * * * * JavaWorld 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 JavaWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package image; import java.awt.*; import world.Posn; /** * * * Represents the Overlaying of two images after moving the bottom image by 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 = -this.pinholeX+top.pinholeX; if(top.width() > bot.width()+this.dx) this.tx = this.pinholeX-top.pinholeX; //if(x < 0)this.tx += -x; this.ty = -this.pinholeY+top.pinholeY; if(top.height() > bot.height()+this.dy) this.ty = this.pinholeY-top.pinholeY; //if(y < 0)this.ty += -y; } 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(Graphics g, int xx, int yy){ this.bot.paint(g, round(xx+this.tx+this.dx+this.bot.pinholeX-this.top.pinholeX), round(yy+this.ty+this.dy+this.bot.pinholeY-this.top.pinholeY)); this.top.paint(g, round(xx+this.tx), round(yy+this.ty)); } }