public class DefaultGrf extends Object implements Grf
The state of any graphics drawn is persistent and can be re-drawn onto a Graphics2D object using the paint method. This should be invoked from the paintComponent method of the JComponent.
public void paintComponent( Graphics g ) { super.paintComponent( g ); defaultGrf.paint( (Graphics2D)g ): }Although if you're using a Plot, then you should use its paint method (which will eventually call this paint method).
Standard Color
objects are encoded to an integer using
encodeColor(java.awt.Color)
method,
so that all colours can be represented by a unique integer
for passing through the Grf interface. To reconstruct a Color use the
decodeColor(int)
method.
In addition to the standard Grf interface this implementation also offers a set of additional public methods that can be used directly. For instance a set of double precision methods are available for extra efficiency (the standard Grf interface only works with floats) and methods are provided that control the fonts that are used (see the addFont, deleteFont methods).
Modifier and Type | Field and Description |
---|---|
static double |
BAD
Constant defining a BAD value (used to break polylines), this is the
same as AST__BAD by definition.
|
static int |
DASH
Use a dashed line drawing style.
|
static String |
DEFAULT
Name of the default context list.
|
static int |
DOT
Use a dotted line drawing style.
|
static int |
DOTDASH
Use a dot dash line drawing style.
|
static int |
GRF__ALPHA
Constants defining different attributes.
|
static int |
LONGDASH
Use a long dash line drawing style.
|
static int |
PLAIN
Use a plain line drawing style.
|
static int |
SHORTDASH
Use a short dash line drawing style.
|
GRF__COLOUR, GRF__ESC, GRF__FONT, GRF__LINE, GRF__MARK, GRF__MJUST, GRF__SCALES, GRF__SIZE, GRF__STYLE, GRF__TEXT, GRF__WIDTH
Constructor and Description |
---|
DefaultGrf(FontRenderContext frc)
Constructor Initialise the font render context being used.
|
DefaultGrf(JComponent component)
Constructor which initialises the font render context from a
Component.
|
Modifier and Type | Method and Description |
---|---|
int |
addFont(Font font)
Add a new font.
|
double |
attr(int attr,
double value,
int prim)
Enquires or sets a graphics attribute value.
|
double |
attribute(int attr,
double value,
int prim)
Enquire or set a graphics attribute value This function returns the
current value of a specified graphics attribute, and optionally
establishes a new value.
|
double[] |
axisScale()
Get the axis scales.
|
static int[] |
bound(double xcoord,
double[] values)
Locate the indices of the two coordinates that lie closest to a
given coordinate.
|
int |
cap(int cap,
int value)
Indicates which abilities this
Grf implementation has. |
double[] |
charHeight()
Return the character height in component coordinates.
|
void |
clear()
Clears all known graphics.
|
static Color |
decodeColor(int value)
Decode a colour encoded as an integer using encodeColor.
|
int |
deleteFont(Font font)
Disable a font by removing it from the known list.
|
static void |
drawLine(Graphics2D g2,
DefaultGrfContainer cont)
Draw a polyline onto a given Graphics2D object, using the state in a
given GrfContainer.
|
protected void |
drawMark(Graphics2D g2,
DefaultGrfContainer cont)
Draw a graphics marker.
|
protected void |
drawText(Graphics2D g2,
DefaultGrfContainer cont)
Draw a text string.
|
static int |
encodeColor(Color colour)
Encode a Color to a unique integer value that can be used in
the Grf interface.
|
void |
establishContext(String key)
Establish a graphics context for grouping a set of related drawing
operations.
|
void |
flush()
Flush all graphics, thereby redrawing them.
|
protected double[] |
getAnchor(String just,
double angle,
double x,
double y,
double[] bbox)
Return the X and Y AST anchor position of a string.
|
Font |
getFont(int index)
Get a known font by graphics index.
|
int |
getFontIndex(Font font)
Get the graphics index of a known font.
|
FontRenderContext |
getFontRenderContext()
Get the context used to check the drawing of fonts.
|
void |
line(int n,
float[] x,
float[] y)
Draws a polyline (a set of connected lines).
|
static Stroke |
lineStroke(int type,
float width)
Create a Stroke for drawing a line using one of the possible line
styles.
|
void |
mark(int n,
float[] x,
float[] y,
int type)
Displays markers at the given positions.
|
void |
marker(double[] x,
double[] y,
int type)
Update current graphics context to include a set of markers.
|
void |
paint(Graphics g)
Paints the graphics written to this Grf object since its
construction or the last call of
clear into the
graphics context given. |
void |
paint(Graphics2D g2)
Draw the complete current graphics context onto a Graphics2D
object.
|
void |
polyline(double[] x,
double[] y)
Update current graphics context to include a polyline.
|
float[] |
qch()
Returns the character height in world coordinates.
|
void |
reAdd(String key,
Object context)
Re-add a removed context.
|
Object |
remove(String key)
Remove and return all graphics in a given context.
|
void |
reset()
Clear all graphics buffers.
|
float[] |
scales()
Gets the axis scales.
|
void |
setClipRegion(Rectangle region)
Set the clipping region (null to reset).
|
void |
text(String text,
double x,
double y,
String just,
double upx,
double upy)
Update current graphic context with a character string.
|
void |
text(String text,
float x,
float y,
String just,
float upx,
float upy)
Displays a character string at a given position using a specified
justification and up-vector.
|
double[] |
textExtent(String text,
double x,
double y,
String just,
double upx,
double upy)
Return the extent of a character string.
|
protected DefaultGrfContainer |
textProperties(String text,
double x,
double y,
String just,
double upx,
double upy)
Calculate the properties of a text object.
|
float[][] |
txExt(String text,
float x,
float y,
String just,
float upx,
float upy)
Gets the extent of a character string.
|
protected void |
update(Graphics2D g2)
Draw the all known graphics to a component.
|
public static final int GRF__ALPHA
public static final double BAD
public static final int PLAIN
public static final int DASH
public static final int DOT
public static final int SHORTDASH
public static final int LONGDASH
public static final int DOTDASH
public static final String DEFAULT
public DefaultGrf(FontRenderContext frc)
frc
- font render contextpublic DefaultGrf(JComponent component)
component
- component supplying font render contextpublic FontRenderContext getFontRenderContext()
public void establishContext(String key)
DEFAULT
is recognised as the default
context. This is the initial state and can be reused (although
switching back will produce a confusion in the Z-ordering of the
graphics, all elements drawn to the default context will be rendered
first).key
- a key to associate with this context. If null then the
DEFAULT context will be assumed.public void paint(Graphics2D g2)
g2
- the Graphics2D object to repaint.public void reset()
public Object remove(String key)
reAdd(java.lang.String, java.lang.Object)
method (although the
Z-order will now be different).public void reAdd(String key, Object context)
remove(java.lang.String)
method.public void polyline(double[] x, double[] y)
x
- array of polyline X verticesy
- array of polyline Y verticespublic void marker(double[] x, double[] y, int type)
x
- array of X positionsy
- array of Y positionstype
- an integer indicating the type of symbol
required. The possible values are defined as
constants in the DefaultGrfMarker class.public void text(String text, double x, double y, String just, double upx, double upy)
text
- Pointer to the string to be displayed.x
- The reference x coordinate.y
- The reference y coordinate.just
- A character string which specifies the location within the
text string which is to be placed at the reference
position given by x and y. The first character may
be 'T' for "top", 'C' for "centre", or 'B' for
"bottom", and specifies the vertical location of
the reference position. Note, "bottom" corresponds
to the base-line of normal text. Some characters
(eg "y", "g", "p", etc) descend below the
base-line. The second character may be 'L' for
"left", 'C' for "centre", or 'R' for "right", and
specifies the horizontal location of the reference
position. If the string has less than 2 characters
then 'C' is used for the missing characters.upx
- The x component of the up-vector for the text, in graphics
world coordinates. If necessary the supplied value
should be negated to ensure that positive values
always refer to displacements from left to right on
the screen.upy
- The y component of the up-vector for the text, in graphics
world coordinates. If necessary the supplied value
should be negated to ensure that positive values
always refer to displacements from bottom to top on
the screen.protected DefaultGrfContainer textProperties(String text, double x, double y, String just, double upx, double upy)
public void flush()
public double[] textExtent(String text, double x, double y, String just, double upx, double upy)
Note: The text rotation and position is determined by creating an affine transformation and applying this directly to the unrotated bounding box. I hope this saves time and is faster than adding a transform to the Graphics2D object.
text
- Pointer to the string to be measured.x
- The reference x coordinate.y
- The reference y coordinate.just
- A character string which specifies the location within the
text string which is to be placed at the reference
position given by x and y. The first character may
be 'T' for "top", 'C' for "centre", or 'B' for
"bottom", and specifies the vertical location of
the reference position. Note, "bottom" corresponds
to the base-line of normal text. Some characters
(eg "y", "g", "p", etc) descend below the
base-line. The second character may be 'L' for
"left", 'C' for "centre", or 'R' for "right", and
specifies the horizontal location of the reference
position. If the string has less than 2 characters
then 'C' is used for the missing characters.upx
- The x component of the up-vector for the text, in graphics
world coordinates. If necessary the supplied value should be
negated to ensure that positive values always refer to
displacements from left to right on the screen.upy
- The y component of the up-vector for the text, in graphics
world coordinates. If necessary the supplied value should be
negated to ensure that positive values always refer to
displacements from bottom to top on the screen.public double[] charHeight()
public double attribute(int attr, double value, int prim)
attr
- An integer value identifying the required attribute. The
following symbolic values are defined:
value
- A new value to store for the attribute. If this is BAD no
value is stored.prim
- The sort of graphics primitive to be drawn with the new
attribute. Identified by the following values:
public double[] axisScale()
public int addFont(Font font)
font
- the new Font.public int deleteFont(Font font)
font
- the font to remove from the known listpublic int getFontIndex(Font font)
font
- the font to locate.public Font getFont(int index)
index
- graphics index of the required font.public void setClipRegion(Rectangle region)
region
- a region specifying the outer limits of the region to
draw in.protected double[] getAnchor(String just, double angle, double x, double y, double[] bbox)
just
- the AST justification string.angle
- rotation of text when plotted.x
- the x coordinate of the bounding box lower corner.y
- the y coordinate of the bounding box lower corner.bbox
- the bounding box of the text that is to be plotted (from
Font.getStringBounds()).protected void update(Graphics2D g2)
public static void drawLine(Graphics2D g2, DefaultGrfContainer cont)
g2
- the Graphics2D object that we are drawing into.cont
- container with the graphics state set.public static Stroke lineStroke(int type, float width)
type
- one of the possible types: PLAIN, DASH, DOT, SHORTDASH,
LONGDASH or DOTDASH.width
- the width of the line that will be drawn with this Stroke.protected void drawText(Graphics2D g2, DefaultGrfContainer cont)
protected void drawMark(Graphics2D g2, DefaultGrfContainer cont)
public static int[] bound(double xcoord, double[] values)
xcoord
- the coordinate value to bound.values
- the coordinates to check.public static int encodeColor(Color colour)
colour
- the Color to encode.public static Color decodeColor(int value)
value
- the integer that represents a colour.public void paint(Graphics g)
Grf
clear
into the
graphics context given.paint
in interface Grf
g
- the graphics context. It will be cast to a
Graphics2D
, but hopefully it will be
one of those anyway.public void clear()
Grf
paint
will draw
nothing to its graphics context.public void line(int n, float[] x, float[] y)
Grf
n
is less than 2, or if
x
or y
are null.public void mark(int n, float[] x, float[] y, int type)
Grf
n
is less than 2, or if
x
or y
are null.public void text(String text, float x, float y, String just, float upx, float upy)
Grf
just
is equivalent to supplying "CC". upx=upy=0
should
throw an Exception.text
in interface Grf
text
- the string to be displayedx
- the reference x coordinatey
- the reference y coordinatejust
- a character string which specifies the location within
the text string which is to be placed at the reference
position given by x and y. The first character
may be 'T' for "top", 'C' for "centre", or 'B' for
"bottom", and specifies the vertical location of the
reference position. Note, "bottom" corresponds to the
base-line of normal text. Some characters (eg "y",
"g", "p", etc) descend below the base-line. The
second character may be 'L' for "left", 'C' for
"centre", or 'R' for "right", and specifies the
horizontal location of the reference position. If
the string has less than 2 characters then 'C'
is used for the missing characters.upx
- The x component of the up-vector for the text,
in graphics world coordinates. If necessary the
supplied value should be negated to ensure that
positive values always refer to displacements from
left to right on the screen.upy
- The y component of the up-vector for the text,
in graphics world coordinates. If necessary the
supplied value should be negated to ensure that
positive values always refer to displacements from
bottom to top on the screen.public double attr(int attr, double value, int prim)
DefaultGrfFontManager
class.
encodeColor(java.awt.Color)
call.
Normally bits 0-23 of the integer represent the red green
and blue intensities in the usual way, and bits 24-31
represent the alpha value (0xff
means opaque and
0x00
means transparent), the exception being
the value opaque white which is 0x00000000.
Note this means that a 24-bit colour value along the lines
0xc0c0c0
would give you a completely transparent
colour - probably not what you want. To specify normal
(opaque) grey, you should use 0xffc0c0c0
or
equivalantly encodeColor(Color.LIGHT_GRAY)
.
attr
in interface Grf
attr
- an integer value identifying the required attribute.
One of the following:
value
- a new value to store for the attribute. If this is
AstObject.AST__BAD no value is stored.prim
- the sort of graphics primitive to be drawn with the
new attribute. One of the following:
public float[] qch()
Grf
public float[][] txExt(String text, float x, float y, String just, float upx, float upy)
Grf
text
method.
The returned box includes any leading or trailing spaces.
The order of the corners is anti-clockwise (in world coordinates)
starting at the bottom left. A null value for just
is equivalent to supplying "CC". upx=upy=0
should
throw an Exception.txExt
in interface Grf
text
- the string to be displayedx
- the reference x coordinatey
- the reference y coordinatejust
- a character string which specifies the location within
the text string which is to be placed at the reference
position given by x and y. The first character
may be 'T' for "top", 'C' for "centre", or 'B' for
"bottom", and specifies the vertical location of the
reference position. Note, "bottom" corresponds to the
base-line of normal text. Some characters (eg "y",
"g", "p", etc) descend below the base-line. The
second character may be 'L' for "left", 'C' for
"centre", or 'R' for "right", and specifies the
horizontal location of the reference position. If
the string has less than 2 characters then 'C'
is used for the missing characters.upx
- The x component of the up-vector for the text,
in graphics world coordinates. If necessary the
supplied value should be negated to ensure that
positive values always refer to displacements from
left to right on the screen.upy
- The y component of the up-vector for the text,
in graphics world coordinates. If necessary the
supplied value should be negated to ensure that
positive values always refer to displacements from
bottom to top on the screen.public int cap(int cap, int value)
Grf
Grf
implementation has.
This method is used by a Plot
to determine if
this object has given capability. Which capability is being
enquired about is indicated by the value of the cap
argument.
According to the values of cap
and val
,
the method should return a value indicating
capability as described below:
Grf.scales()
method, and zero otherwise.
The value
argument should be ignored.
Grf.text(java.lang.String, float, float, java.lang.String, float, float)
and Grf.txExt(java.lang.String, float, float, java.lang.String, float, float)
methods recognise "M" as a
character in the justification string. If the first character of
a justification string is "M", then the text should be justified
with the given reference point at the bottom of the bounding box.
This is different to "B" justification, which requests that the
reference point be put on the baseline of the text, since some
characters hang down below the baseline. If text
or txExt
cannot differentiate between "M" and "B",
then this method should return zero, in which case "M"
justification will never be requested by Plot
The value
argument should be ignored.
Grf.text(java.lang.String, float, float, java.lang.String, float, float)
and Grf.txExt(java.lang.String, float, float, java.lang.String, float, float)
methods can recognise and interpret graphics escape sequences
within the supplied string. Zero should be returned if
escape sequences cannot be interpreted (in which case the
Plot
will interpret them itself if needed).
The value
argument should be ignored only if
escape cannot be interpreted by text
and
txExt
. Otherwise value
indicates
whether text
and txExt
should
interpret escape sequences in subsequent calls.
If value
is non-zero then escape sequences
should be interpreted by text
and txExt
.
Otherwise they should be drawn as literal text.
See GrfEscape
for more information
about escape sequences.
public float[] scales()
Grf
Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.