Back to index

wims  3.65+svn20090927
Line.java
Go to the documentation of this file.
00001 /*
00002 $Id: Line.java,v 1.3 2003/02/18 11:48:46 sander Exp $
00003 */
00004 
00005 
00006 /*
00007 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 For further information on the GNU Lesser General Public License,
00024 see: http://www.gnu.org/copyleft/lesser.html
00025 For further information on this library, contact: mainline@essi.fr
00026 */
00027 
00028 
00029 package fr.ove.openmath.jome.ctrlview.bidim;
00030 
00031 import java.awt.*;
00032 import java.io.Serializable;
00033 
00040 public class Line implements Serializable {
00044     private Point p1;
00045     
00049     private Point p2;
00050     
00054     private int thickness;
00055     
00065     private byte alignment;
00066     
00067     private Polygon line;
00068     
00073     public Line() {
00074         this(new Point(), new Point(), 1, CENTER);
00075     }
00076     
00084     public Line(Point p1, Point p2, int thickness, byte alignment) {
00085         this.p1 = p1;
00086         this.p2 = p2;
00087         this.thickness = thickness;
00088         this.alignment = alignment;
00089         computeLine();
00090     }
00091     
00096     public void setP1(Point p1) {
00097         this.p1 = p1;
00098         computeLine();
00099     }
00100     
00104     public Point getP1() {
00105         return p1;
00106     }
00107     
00112     public void setP2(Point p2) {
00113         this.p2 = p2;
00114         computeLine();
00115     }
00116     
00120     public Point getP2() {
00121         return p2;
00122     }
00123     
00128     public void setAlignment(byte alignment) {
00129         this.alignment = alignment;
00130     }
00131     
00135     public int getAlignment() {
00136         return alignment;
00137     }
00138     
00143     public void setThickness(int thickness) {
00144         this.thickness = thickness;
00145         computeLine();
00146     }
00147     
00151     public int getThickness() {
00152         return thickness;
00153     }
00154     
00159     public void draw(Graphics g) {
00160         g.fillPolygon(line);
00161     }
00162     
00166     public Polygon getLine() {
00167         return line;
00168     }
00169     
00170     private void computeLine() {
00171         line = new Polygon();
00172         Point vectDir = new Point(p2.x - p1.x, p2.y - p1.y);
00173         float ortho_x = -vectDir.y;
00174         float ortho_y = vectDir.x;
00175         // On normalise ortho
00176         float norme = (float) Math.sqrt(ortho_x*ortho_x + ortho_y*ortho_y);
00177         ortho_x = (ortho_x/norme)*((float) thickness);
00178         ortho_y = (ortho_y/norme)*((float) thickness);
00179         
00180         int x, y;
00181         
00182         switch(alignment) {
00183             case RIGHT :
00184                 line.addPoint(p1.x, p1.y);
00185                 line.addPoint(p2.x, p2.y);
00186                 line.addPoint((int) Math.round(((int) p2.x) - ortho_x), (int) Math.round(((int) p2.y) - ortho_y));
00187                 line.addPoint((int) Math.round(((int) p1.x) - ortho_x), (int) Math.round(((int) p1.y) - ortho_y));
00188                 break;
00189             case CENTER :
00190                 float half_x = ortho_x/2.0f;
00191                 float half_y = ortho_y/2.0f;
00192                 line.addPoint((int) Math.round(((int) p1.x) - half_x), (int) Math.round(((int) p1.y) - half_y));
00193                 line.addPoint((int) Math.round(((int) p2.x) - half_x), (int) Math.round(((int) p2.y) - half_y));
00194                 line.addPoint((int) Math.round(((int) p2.x) + half_x), (int) Math.round(((int) p2.y) + half_y));
00195                 line.addPoint((int) Math.round(((int) p1.x) + half_x), (int) Math.round(((int) p1.y) + half_y));
00196                 break;
00197             case LEFT :
00198                 line.addPoint(p1.x, p1.y);
00199                 line.addPoint(p2.x, p2.y);
00200                 line.addPoint((int) Math.round(((int) p2.x) + ortho_x), (int) Math.round(((int) p2.y) + ortho_y));
00201                 line.addPoint((int) Math.round(((int) p1.x) + ortho_x), (int) Math.round(((int) p1.y) + ortho_y));
00202                 break;
00203         }
00204     }
00205     
00213     public static final byte LEFT      = 0;
00214     
00218     public static final byte CENTER    = 1;
00219     
00223     public static final byte RIGHT     = 2;
00224 }