/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * JavaWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./image/Scene.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 java.io.File; import javax.imageio.ImageIO; import image.RasterImage; import world.Posn; /** * * * An abstract Class representing a Scene (a cropped Image). Images * can be placed on a Scene at a specific location, resulting in a * new Scene. * *
 *    new EmptyScene(48, 48).placeImage(new Triangle(32, "solid", "red"), 24, 24)
* *
* *
 *    new EmptyScene(48, 48).placeImage(new Triangle(64, "solid", "red"), 24, 24)
* *
* *
 *    new EmptyScene(48, 48).addLine(0, 0, 48, 48, "blue")
* *
* *
 *    new EmptyScene(48, 48).addLine(4, 24, 44, 24, "green")
* *
* *
 *    new EmptyScene(50, 50)
 *         .placeImage(new Overlay(new Circle(20, "outline", "black"),
 *                                 new Circle(20, "solid", "wheat")), 25, 25)
 *         .placeImage(new Circle(5, "solid", "lightblue"), 18, 20)
 *         .placeImage(new Rectangle(10, 3, "solid", "lightblue"), 33, 20)
 *         .placeImage(new Ellipse(20, 8, "solid", "red"), 25, 35)
* *
*/ public abstract class Scene extends Image{ protected Scene(){ super(0,0); } /** Draw the scene into a Graphics */ public abstract void paint(Graphics g, int x, int y); /** Place another Image on top of this Scene at (x,y) */ public Scene placeImage(Image i, double x, double y){ return new Placed(i,x,y,this); } /** Place another Image on top of this Scene at (x,y) */ public Scene placeImage(Image i, int x, int y){ return new Placed(i,x,y,this); } /** Place another Image on top of this Scene at the given Posn */ public Scene placeImage(Image i, Posn p){ return new Placed(i,p.x,p.y,this); } /** Add a line to this Scene from (x,y) to (xx, yy) */ public Scene addLine(int x, int y, int xx, int yy, String color){ return this.addLine((double)x, y, xx, yy, color); } /** Add a line to this Scene from (x,y) to (xx, yy) */ public Scene addLine(double x, double y, double xx, double yy, String color){ return this.placeImage(new Line(xx-x, yy-y, color), (xx+x)/2, (yy+y)/2); } /** Add a line to this Scene from the first Posn to the second */ public Scene addLine(Posn p1, Posn p2, String color){ return this.addLine(p1.x, p1.y, p2.x, p2.y, color); } /** Represents an Image Placed on top of a Scene at offset X/Y */ private static class Placed extends Scene{ private Image img; private double x; private double y; private Scene next; Placed(Image img, int x, int y, Scene next){ this(img, (double)x, y, next); } Placed(Image img, double x, double y, Scene next){ this.img = img; this.x = x; this.y = y; this.next = next; } /** Paint the next scene, then place the image on top */ public void paint(Graphics g, int xx, int yy){ this.next.paint(g,xx,yy); this.img.paint(g,round(xx+this.x),round(yy+this.y)); } /** Calculate the width of the combined Scene/Image */ public int width(){ return this.next.width(); } public int height(){ return this.next.height(); } } /** Save this Scene to a File */ public boolean toFile(String name){ try{ RasterImage rast = this.rasterize(); int dot = name.lastIndexOf('.'); String ext = name.substring(dot+1); return ImageIO.write(rast.img, ext, new File(name)); }catch(Exception e){ System.err.println("Error writing Scene: ["+e.getClass().getSimpleName()+"] "+e.toString()); e.printStackTrace(); return false; } } }