Interface SmileUtilities
public interface SmileUtilities
A collection of utility methods primarily for performing common GUI-related tasks.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordThe small and large icon variants of an action, as Swing asks for them throughAction.SMALL_ICONandAction.LARGE_ICON_KEY. -
Field Summary
FieldsModifier and TypeFieldDescriptionThe icon sizes a window frame asks for, from the title bar (16) up to the task switcher and dock (256).static final intThe icon size Swing uses for large toolbar buttons.static final intThe icon size Swing uses for menu items and small toolbar buttons. -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic intgetLineOfOffset(JTextComponent editor, int offset) Returns the line number given a text offset.static intgetOffsetOfLine(JTextComponent editor, int line) Returns the start offset of a line.static StringgetWordAt(JTextComponent editor, int offset) Returns the word ending at the text offset.static SmileUtilities.ActionIconsloadActionIcons(Class<?> owner, String resource) Loads the small and large variants of an icon from a classpath resource.loadFrameIcons(Class<?> owner, String resource) Loads a window frame icon at every size the window system may ask for, forWindow.setIconImages(List).static ImageIconloadImageIcon(Class<?> owner, String resource, int size) Loads an icon from a classpath resource, scaled to the given size.static ImageIconscaleImageIcon(ImageIcon icon, int size) Scales an image icon to fit within the desired size, preserving the source aspect ratio.static JFrameShows the data frame in a window.static JFrameShows the data frame in a window.static JFrameShows the figure in a window.static JFrameshow(MultiFigurePane figure) Shows the figure in a window.static JFrameShows the matrix in a window.static JFrameShows the matrix in a window.static JFrameshow(SparseMatrix matrix) Shows the sparse matrix structure in a figure window.
-
Field Details
-
SMALL_ICON_SIZE
static final int SMALL_ICON_SIZEThe icon size Swing uses for menu items and small toolbar buttons.- See Also:
-
LARGE_ICON_SIZE
static final int LARGE_ICON_SIZEThe icon size Swing uses for large toolbar buttons.- See Also:
-
FRAME_ICON_SIZES
-
-
Method Details
-
getLineOfOffset
Returns the line number given a text offset.- Parameters:
editor- the text component.offset- the text offset.- Returns:
- the line number.
-
getOffsetOfLine
Returns the start offset of a line.- Parameters:
editor- the text component.line- the line number.- Returns:
- the start offset.
-
getWordAt
Returns the word ending at the text offset.- Parameters:
editor- the text component.offset- the text offset.- Returns:
- the word ending at the text offset.
- Throws:
BadLocationException- if the offset is invalid.
-
scaleImageIcon
Scales an image icon to fit within the desired size, preserving the source aspect ratio. A non-square source keeps its proportions (for example a 64×32 source scaled to 16 becomes 16×8) instead of being stretched to a square.Downscaling is done in progressive halving steps. A single bicubic step from a large source (for example a 512px PNG) straight to 16px aliases badly and makes thin strokes shimmer; halving until the image is within twice the target, then taking the final step, keeps the detail.
The returned icon carries a 2× resolution variant, so Swing picks the sharper bitmap on a HiDPI display instead of upscaling the 1× one.
- Parameters:
icon- the input image icon.size- the desired icon size in logical pixels.- Returns:
- the scaled image icon.
- Throws:
IllegalArgumentException- ifsizeis not positive.
-
loadImageIcon
Loads an icon from a classpath resource, scaled to the given size. A missing or undecodable resource is logged and yields an empty icon rather than an exception: a missing decoration must not take down the menu bar that is being built.- Parameters:
owner- the class whose package the resource path is relative to.resource- the resource path, for example"images/open.png".size- the desired icon size in logical pixels.- Returns:
- the icon, or an empty icon when the resource cannot be loaded.
-
loadActionIcons
Loads the small and large variants of an icon from a classpath resource. The small variant is what Swing shows in menu items and small toolbar buttons; the large one is what it shows in a large toolbar.- Parameters:
owner- the class whose package the resource path is relative to.resource- the resource path, for example"images/open.png".- Returns:
- the two icon variants.
-
loadFrameIcons
Loads a window frame icon at every size the window system may ask for, forWindow.setIconImages(List). Each size is scaled with the same progressive halving asscaleImageIcon(ImageIcon, int), so the small title-bar bitmap stays crisp instead of aliasing from a single large reduction.A missing or undecodable resource is logged and yields an empty list rather than an exception, so a missing decoration cannot stop the window from being created.
- Parameters:
owner- the class whose package the resource path is relative to.resource- the resource path, for example"images/smile.png".- Returns:
- the frame icon bitmaps, one per size, or an empty list when the resource cannot be loaded.
-
show
-
show
Shows the figure in a window.- Parameters:
figure- the figure to display.- Returns:
- a new JFrame that contains the figure.
-
show
-
show
-
show
-
show
-
show
Shows the sparse matrix structure in a figure window.- Parameters:
matrix- the matrix to display.- Returns:
- a new JFrame that displays the sparse matrix structure.
-