public class PaintUtil
extends java.lang.Object
It's assumed that the ScaleType.USR_SCALE
factor is already applied to the values (given in the user space)
passed to the methods of this class. So the user scale factor is not taken into account.
ScaleType
Modifier and Type | Class and Description |
---|---|
static class |
PaintUtil.ParityMode
Defines whether a value should be aligned with the nearest integer odd/even value in the device space.
|
static class |
PaintUtil.RoundingMode
Defines which of the
Math rounding method should be applied when converting to the device space. |
Constructor and Description |
---|
PaintUtil() |
Modifier and Type | Method and Description |
---|---|
static java.awt.Shape |
alignClipToInt(java.awt.Graphics2D g,
boolean alignH,
boolean alignV,
PaintUtil.RoundingMode xyRM,
PaintUtil.RoundingMode whRM)
Aligns the graphics rectangular clip to int coordinates if the graphics has fractional scale transform.
|
static double |
alignToInt(double usrValue,
java.awt.Graphics2D g) |
static double |
alignToInt(double usrValue,
java.awt.Graphics2D g,
PaintUtil.ParityMode pm) |
static double |
alignToInt(double usrValue,
java.awt.Graphics2D g,
PaintUtil.RoundingMode rm) |
static double |
alignToInt(double usrValue,
ScaleContext ctx) |
static double |
alignToInt(double usrValue,
ScaleContext ctx,
PaintUtil.RoundingMode rm,
PaintUtil.ParityMode pm)
Returns the value in the user space aligned with the integer value in the device space, applying the rounding and parity modes.
|
static java.awt.geom.AffineTransform |
alignTxToInt(java.awt.Graphics2D g,
java.awt.geom.Point2D offset,
boolean alignX,
boolean alignY,
PaintUtil.RoundingMode rm)
Aligns the x or/and y translate of the graphics to the integer coordinate if the graphics has fractional scale transform,
otherwise does nothing.
|
static double |
devPixel(java.awt.Graphics2D g)
Returns one device pixel converted to the user space.
|
static double |
devValue(double usrValue,
java.awt.Graphics2D g)
Converts the value from the user to the device space.
|
static double |
devValue(double usrValue,
ScaleContext ctx) |
static java.awt.geom.Point2D |
getFractOffsetInRootPane(javax.swing.JComponent comp)
Returns (in user space) the fractional part of the XY
comp 's offset relative to its JRootPane ancestor. |
static PaintUtil.ParityMode |
getParityMode(double usrValue,
java.awt.Graphics2D g) |
static PaintUtil.ParityMode |
getParityMode(double usrValue,
ScaleContext ctx,
PaintUtil.RoundingMode rm)
Returns parity of the value converted to the device space with the given rounding mode (
ROUND if null). |
static java.awt.geom.Point2D |
insets2offset(java.awt.Insets in) |
static boolean |
isFractionalScale(java.awt.geom.AffineTransform tx)
Returns true if the transform matrix contains fractional scale element.
|
static java.awt.geom.Point2D |
negate(java.awt.geom.Point2D pt)
Returns negated Point2D instance.
|
static void |
paintWithAA(java.awt.Graphics2D g,
java.lang.Object valueAA,
java.lang.Runnable paint)
Calls the
paint action with the provided antialiasing. |
public static PaintUtil.ParityMode getParityMode(double usrValue, java.awt.Graphics2D g)
public static PaintUtil.ParityMode getParityMode(double usrValue, ScaleContext ctx, PaintUtil.RoundingMode rm)
ROUND
if null).usrValue
- the value, given in the user spacectx
- the scale context (the user scale is ignored)rm
- the rounding mode to applypublic static double alignToInt(double usrValue, java.awt.Graphics2D g)
public static double alignToInt(double usrValue, java.awt.Graphics2D g, PaintUtil.RoundingMode rm)
public static double alignToInt(double usrValue, java.awt.Graphics2D g, PaintUtil.ParityMode pm)
public static double alignToInt(double usrValue, ScaleContext ctx, PaintUtil.RoundingMode rm, PaintUtil.ParityMode pm)
ROUND
is applied. If the parity mode is null - it's ignored, otherwise the value
converted to the device space is aligned with the nearest integer odd/even value by the provided rounding mode.
For instance, 2.1 would be floor'd to odd 1, ceil'd and round'ed to odd 3. Also, 1.9 would be floor'd to odd 1, ceil'd to
odd 3 and round'ed to odd 1.usrValue
- the value to align, given in the user spacectx
- the scale context, the user scale is ignoredrm
- the rounding mode to apply (ROUND
if null)pm
- the parity mode to apply (ignored if null)public static double alignToInt(double usrValue, ScaleContext ctx)
public static double devValue(double usrValue, java.awt.Graphics2D g)
usrValue
- the value in the user spaceg
- the graphicspublic static double devValue(double usrValue, ScaleContext ctx)
devValue(double, Graphics2D)
public static double devPixel(java.awt.Graphics2D g)
g
- the graphicspublic static java.awt.geom.AffineTransform alignTxToInt(java.awt.Graphics2D g, java.awt.geom.Point2D offset, boolean alignX, boolean alignY, PaintUtil.RoundingMode rm)
g
- the graphics to alignoffset
- x/y offset to take into account when provided (this may be e.g. insets left/top)alignX
- should the x-translate be alignedalignY
- should the y-translate be alignedpublic static java.awt.Shape alignClipToInt(java.awt.Graphics2D g, boolean alignH, boolean alignV, PaintUtil.RoundingMode xyRM, PaintUtil.RoundingMode whRM)
g
- the graphics to alignalignH
- should the x/width be alignedalignV
- should the y/height be alignedxyRM
- the rounding mode to apply to the clip's x/ywhRM
- the rounding mode to apply to the clip's width/heightpublic static java.awt.geom.Point2D getFractOffsetInRootPane(javax.swing.JComponent comp)
comp
's offset relative to its JRootPane
ancestor.
Used for repainting a JComponent
in a UI hierarchy via an image buffer on fractional scale graphics.
class MyPainter { void paintToBuffer() { JComponent myComp = getMyComp(); Point2D offset = getFractOffsetInRootPane(myComp); Image buffer = getBufferForMyComp(myComp); Graphics2D g2d = (Graphics2D)buffer.getGraphics(); // the fractional part of myComp's offset affects J2D rounding logic and so the final rasterization of myComp // the offset is set on g2d to have the same myComp's rasterization as in original JRootPane full repaint // otherwise pixel floating effect can be observed g2d.translate(offset.getX(), offset.getY()); myComp.paint(g2d); } void paint(Graphics2D g2d) { // g2d is translated to myComp's parent XY JComponent myComp = getMyComp(); Point2D offset = getFractOffsetInRootPane(myComp); Image buffer = getBufferForMyComp(myComp); // already painted buffer g2d.translate(myComp.getX() - offset.getX(), myComp.getY() - offset.getY()); // negate the fractional offset set above UIUtil.paintImage(g2d, buffer, 0, 0, null); } }
public static java.awt.geom.Point2D negate(java.awt.geom.Point2D pt)
public static boolean isFractionalScale(java.awt.geom.AffineTransform tx)
public static void paintWithAA(java.awt.Graphics2D g, java.lang.Object valueAA, java.lang.Runnable paint)
paint
action with the provided antialiasing.g
- the graphics to paint onvalueAA
- a value for the RenderingHints.KEY_ANTIALIASING
keypaint
- the paint actionpublic static java.awt.geom.Point2D insets2offset(java.awt.Insets in)