import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; public class ImageShape extends SelectableShape { BufferedImage orig; double origW, origH; AffineTransform cumulative; /** * x,y is initial placement of top-left corner */ public ImageShape(String filename, double x, double y, Component container) { Image raw; Toolkit tk = Toolkit.getDefaultToolkit(); raw = tk.createImage(filename); MediaTracker m = new MediaTracker(container); m.addImage(raw, 0); try { m.waitForID(0); } catch (InterruptedException e) {} origW = raw.getWidth(null); origH = raw.getHeight(null); orig = new BufferedImage((int) origW, (int) origH, BufferedImage.TYPE_INT_ARGB); ((Graphics2D) orig.getGraphics()).drawImage(raw, 0, 0, null); cumulative = AffineTransform.getTranslateInstance(x,y); } public void draw(Graphics2D g2) { Image img = new AffineTransformOp(cumulative, AffineTransformOp.TYPE_BICUBIC).filter(orig, null); g2.drawImage(img, 0, 0, null); } public Rectangle2D getBounds2D() { Point2D a = cumulative.transform(new Point2D.Double(0,0), null); Point2D b = cumulative.transform(new Point2D.Double(origW, origH), null); double left = Math.min(a.getX(),b.getX()); double top = Math.min(a.getY(),b.getY()); double w = Math.abs(a.getX() - b.getX()); double h = Math.abs(a.getY() - b.getY()); return new Rectangle2D.Double(left, top, w, h); } public boolean contains(Point2D p) { return getBounds2D().contains(p); } public void translate(double dx, double dy) { cumulative.preConcatenate(AffineTransform.getTranslateInstance(dx, dy)); } }