Advertisement
Guest User

LabeledBar.java

a guest
Jun 13th, 2014
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Rectangle;
  4. import java.awt.geom.Line2D;
  5. /** LabeledBar is a rectangle with an interior label.
  6.  * @author John Dalbey
  7.  * @version 2014.6.13
  8.  */
  9. public class LabeledBar
  10. {
  11.     private int xLeft;
  12.     private int yTop;
  13.     private int width;
  14.     private int height;
  15.     private String label;
  16.     private Color color;
  17.     /** Construct this object from the specified dimensions.
  18.      * @param x x coordinate of the upper-left corner of this bar.
  19.      * @param y y coordinate of the upper-left corner of this bar.
  20.      * @param aWidth width of the bar in pixels.
  21.      * @param label the text to be displayed inside the bar.
  22.      * @param color desired color of the lines of the bar.
  23.      */
  24.     public LabeledBar(int x, int y, int aWidth, String label, Color color)
  25.     {
  26.         xLeft = x;
  27.         yTop = y;
  28.         width = aWidth;
  29.         height = 20;
  30.         this.label = label;
  31.         this.color = color;
  32.     }
  33.  
  34.     /** Draw this bar on the supplied graphics context.
  35.      * @param g2 the context on which to draw this bar.
  36.      */
  37.     public void draw(Graphics2D g2)
  38.     {
  39.         Rectangle leftRectangle = new Rectangle(
  40.             xLeft, yTop,
  41.             width, height);
  42.  
  43.         g2.setColor(color);
  44.         g2.draw(leftRectangle);
  45.         g2.drawString(label, xLeft+height/4, yTop+height*3/4);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement