/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * JavaWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./image/Line.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.Graphics; import java.awt.Graphics2D; import java.awt.Color; import java.awt.geom.*; import 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; Color color; /** Create a Line from (0,0) to doubles (X,Y) of the given color */ public Line(double x, double y, String color){ super(Math.abs(x/2),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.color = color(color); } /** Create a Line from (0,0) to ints (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 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(Graphics g, int xx, int yy){ Graphics2D g2 = (Graphics2D)g; g2.setColor(this.color); // If Y is negative, we flip the line... if(this.y >= 0) g2.draw(new Line2D.Double(xx-this.pinholeX,yy-this.pinholeY,xx+this.pinholeX,yy+this.pinholeY)); else g2.draw(new Line2D.Double(xx-this.pinholeX,yy+this.pinholeY,xx+this.pinholeX,yy-this.pinholeY)); } /** Return the width of this Line Image */ public int width(){ return (int)(Math.abs(this.x)+1); } /** Return the height of this Line Image */ public int height(){ return (int)(Math.abs(this.y)+1); } }