/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 <http://www.gnu.org/licenses/>. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.image; import android.graphics.Canvas; import android.graphics.Paint; import android.world.Posn; /** * <style type='text/css'><!-- * .com{ font-style: italic; color: #880000; } * .keyw{ font-weight: bold; color: #000088; } * .num{ color: #00AA00; } * .str{ color: #CC00AB; } * .prim{ color: #0000FF; } * img.example{ padding-left: 50px; padding-bottom: 30px; } * --></style> * * Class representing a Line from (0,0) to the given (X,Y). * * <pre> * <span class='keyw'>new</span> Line(<span class='num'>30</span>, <span class='num'>30</span>, <span class='str'>"black"</span>)</pre> * <img class="example" src="test/images/line-1.png" /> * <br /> * * <pre> * <span class='keyw'>new</span> Line(<span class='num'>-30</span>, <span class='num'>20</span>, <span class='str'>"red"</span>)</pre> * <img class="example" src="test/images/line-2.png" /> * <br /> * * <pre> * <span class='keyw'>new</span> Line(<span class='num'>30</span>, <span class='num'>-20</span>, <span class='str'>"red"</span>)</pre> * <img class="example" src="test/images/line-3.png" /> * <br /> * */ 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); } }