|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--acm.graphics.GObject | +--acm.graphics.GLabel
public class GLabel extends GObject
The GLabel class is a graphical object whose appearance consists
of a text string.
To use the GLabel class, the first
step is to construct a new GLabel object and add it to an existing GCanvas (assumed here to be
stored in the variable gc), as follows:
gc.add(glabel); This code creates a GLabel containing the string "Hello, world." with its origin at the point (50, 60) and installs the label in the GCanvas as shown in the figure to the right. |
![]() |
Most graphical objects in Java use the upper left corner of the figure to define their location. Strings, however, work differently. The location at which a string is displayed is always taken to be at the leftmost edge of the first character along what is called the baseline, which is the line on which the uppercase letters sit. Some lowercase letters (g, j, p, q, and y) descend below the baseline, as do several special characters like the comma. The location of the start of the string and the concept of the baseline are illustrated in the diagram to the right. |
![]() |
Unless you specify otherwise, a string is displayed in a standard
font, which is defined in this class as the constant
DEFAULT_FONT.
You can change the appearance, style, and size of the string by
using the setFont method,
which takes a Font as its argument, as defined in the
java.awt package. For example, to change the string
so that it uses a boldface serif type face at a larger point size,
you could execute the following code:
Note that changing the font can change the displayed size of the string, which will be reflected in calls to getWidth or getSize. |
![]() |
Just as with other objects of the
GObject class,
you can change the color of the displayed text by invoking
the setColor method,
which takes a Color as its argument, as defined in the
java.awt package. Thus, the following statement
changes the text color to blue:
|
![]() |
Once installed on the canvas, you can move a string using either
the setLocation method,
which moves an object to a specific position,
or the move method,
which moves an object relative to its current position.
To move the string rightward 50 pixels, for example, you could use
the following statement:
As you can see from the diagram, the string is clipped by the edges of the canvas. |
![]() |
For a GLabel object, one of the operations you
will often want to perform is to center the string relative to a particular
position, such as the center of the screen. To do so, the simplest
approach is to use the getWidth
method to find the width of the string in pixels, and then start the
string half that distance to the left of the central point. Thus, the
following setLocation call
centers the string horizontally in the GCanvas:
|
![]() |
As a final example, you can use the
setLabel method to
change the character string that the GLabel displays.
Thus, if you call
the string "Hello, world." gets replaced by "Hi!". The position of the string doesn’t change, so that you would then need to repeat the centering operation if you wanted this string to appear in the middle of the window. |
![]() |
Field Summary | |
Font | DEFAULT_FONT |
Constructor Summary |
GLabel(String str) |
GLabel(String str,
double x,
double y) |
Method Summary | |
double | getAscent() |
GRectangle | getBounds() |
double | getDescent() |
Font | getFont() |
double | getHeight() |
String | getLabel() |
double | getWidth() |
void | setFont(Font font) |
void | setFont(String str) |
void | setLabel(String str) |
Inherited Method Summary | |
void | addMouseListener(MouseListener listener) |
void | addMouseMotionListener(MouseMotionListener listener) |
boolean | contains(GPoint pt) |
boolean | contains(double x, double y) |
Color | getColor()GLabel .
|
GPoint | getLocation()GLabel as a GPoint object.
|
GDimension | getSize() |
double | getX() |
double | getY() |
boolean | isVisible()GLabel is visible.
|
void | move(double dx, double dy)dx and dy .
|
void | movePolar(double r, double theta) |
void | removeMouseListener(MouseListener listener) |
void | removeMouseMotionListener(MouseMotionListener listener) |
void | sendBackward() |
void | sendForward() |
void | sendToBack() |
void | sendToFront() |
void | setColor(Color color)GLabel .
|
void | setLocation(GPoint pt) |
void | setLocation(double x, double y)GLabel to the point (x , y ).
For a GLabel , the location is the point on the text baseline at which the
text starts.
|
void | setVisible(boolean visible)GLabel .
|
Field Detail |
public static final Font DEFAULT_FONT
Constructor Detail |
public GLabel(String str)
Usage: | GLabel glabel = new GLabel(str); | ||
Parameter: |
|
public GLabel(String str,
double x,
double y)
Usage: | GLabel glabel = new GLabel(str, x, y); | ||||||
Parameters: |
|
Method Detail |
public double getAscent()
Usage: | double ascent = glabel.getAscent(); |
Returns: | The ascent of this string in pixels |
public GRectangle getBounds()
Usage: | GRectangle bounds = glabel.getBounds(); |
Returns: | The bounding box for this object |
public double getDescent()
Usage: | double descent = glabel.getDescent(); |
Returns: | The descent of this string in pixels |
public Font getFont()
Usage: | Font font = glabel.getFont(); |
Returns: | The font in use by this object |
public double getHeight()
Usage: | double height = glabel.getHeight(); |
Returns: | The height of this string |
public String getLabel()
Usage: | String str = glabel.getLabel(); |
Returns: | The string displayed by this object |
public double getWidth()
Usage: | double width = glabel.getWidth(); |
Returns: | The width of this object |
public void setFont(Font font)
Usage: | glabel.setFont(font); | ||
Parameter: |
|
public void setFont(String str)
Usage: | glabel.setFont(str); | ||
Parameter: |
|
public void setLabel(String str)
Usage: | glabel.setLabel(str); | ||
Parameter: |
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |