/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/image/Line.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.Canvas; import android.graphics.Paint; import android.world.Posn; /** * * * Class representing a Line from (0,0) to the given (X,Y). * *
 *    new Line(30, 30, "black")
* *
* *
 *    new Line(-30, 20, "red")
* *
* *
 *    new Line(30, -20, "red")
* *
* */ public class Line extends Image{ double x; double y; Paint paint; /** Create a Line from (0,0) to doubles (X,Y) of the given color */ public Line(int x, int y, String color){ this((double)x,y,color); } /** Create a Line from (0,0) to ints (X,Y) of the given color */ public Line(double x, double y, String color){ super(round(Math.abs(x/2)),round(Math.abs(y/2))); this.x = Math.abs(x); // Make sure Y is the only possible negative... if(x*y >= 0){ this.y = Math.abs(y); }else{ this.y = -Math.abs(y); } this.paint = painter(color(color), Image.OUTLINE); } /** Create a Line from (0,0) to doubles (X,Y) of the given color */ public Line(Posn p, String color){ this(p.x,p.y,color); } /** Draw this image into a Graphics */ public void paint(Canvas c, int xx, int yy){ // If Y is negative, we flip the line... if(y >= 0) c.drawLine((float)(xx-pinholeX),(float)(yy-pinholeY), (float)(xx+pinholeX),(float)(yy+pinholeY), paint); else c.drawLine((float)(xx-pinholeX),(float)(yy+pinholeY), (float)(xx+pinholeX),(float)(yy-pinholeY), paint); } /** Return the width of this Image */ public int width(){ return round(Math.abs(x)+1); } /** Return the height of this Image */ public int height(){ return round(Math.abs(y)+1); } }