/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/Scene.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.graphics.Bitmap; import android.graphics.Canvas; import android.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(Canvas c, 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,(int)x,(int)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.placeImage(new Line(p2.x-p1.x, p2.y-p1.y, color), (p2.x+p1.x)/2, p2.y+p1.y/2); } /** 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(Canvas c, int xx, int yy){ next.paint(c,xx,yy); img.paint(c,round(xx+x),round(yy+y)); } /** Calculate the width of the combined Scene/Image */ public int width(){ return next.width(); } /** Calculate the height of the combined Scene/Image */ public int height(){ return 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); java.io.FileOutputStream out = new java.io.FileOutputStream(name); // Android appears to only have JPEG and PNG rast.img.compress((ext.toLowerCase().equals("png"))? Bitmap.CompressFormat.PNG: Bitmap.CompressFormat.JPEG, 100, out); return true; }catch(Exception e){ throw new RuntimeException(e); } } }