001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016 package lapisx.swing;
017
018 import java.awt.*;
019 import javax.swing.*;
020
021 /**
022 * Lightweight component that just draws a one-pixel horizontal or
023 * vertical line.
024 */
025 public class Line extends JComponent {
026 int orientation;
027
028 public int getOrientation () { return orientation; }
029 public void setOrientation (int orientation) { this.orientation = orientation; repaint (); }
030
031 /**
032 * Make a horizontal line in a 10x10 bounding box.
033 */
034 public Line () {
035 this (10, 10, SwingConstants.HORIZONTAL);
036 }
037
038 /**
039 * Make a Line.
040 * @param w Width of line's bounding box
041 * @param h Height of line's bounding box
042 * @param orientation SwingConstants.HORIZONTAL for horizontal line
043 * w pixels long;
044 * SwingConstants.VERTICAL for vertical line h pixels long.
045 */
046 public Line (int width, int height, int orientation) {
047 this.orientation = orientation;
048 setPreferredSize (new Dimension (width, height));
049 }
050
051 public void paintComponent (Graphics g) {
052 super.paintComponent (g);
053
054 Insets insets = getInsets ();
055 Dimension size = getSize ();
056 int width = size.width - insets.left - insets.right;
057 int height = size.height - insets.top - insets.bottom;
058
059 g.setColor (getForeground ());
060 switch (orientation) {
061 case SwingConstants.HORIZONTAL:
062 {
063 int x = insets.left;
064 int y = insets.top + height/2;
065 g.drawLine (x, y, x + width, y);
066 break;
067 }
068 case SwingConstants.VERTICAL:
069 {
070 int x = insets.left + width/2;
071 int y = insets.top;
072 g.drawLine (x, y, x, y + height);
073 break;
074 }
075 }
076 }
077 }
078