com.alee.utils.ImageUtils Java Examples

The following examples show how to use com.alee.utils.ImageUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImagePainter.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns preview image for specified insets.
 *
 * @param insets image component insets
 * @param image  image to preview
 * @return preview image
 */
@NotNull
protected BufferedImage getPreviewImage ( @NotNull final BufferedImage image, @NotNull final Insets insets )
{
    final BufferedImage preview;
    final Dimension size = SwingUtils.shrink ( component.getSize (), insets );
    if ( image.getWidth () > size.width || image.getHeight () > size.height )
    {
        if ( lastPreviewImage == null || lastDimension != null && !lastDimension.equals ( size ) )
        {
            if ( lastPreviewImage != null )
            {
                lastPreviewImage.flush ();
                lastPreviewImage = null;
            }
            lastPreviewImage = ImageUtils.createImageThumbnail ( image, size );
            lastDimension = size;
        }
        preview = lastPreviewImage;
    }
    else
    {
        preview = image;
    }
    return preview;
}
 
Example #2
Source File: WebImageDrop.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Updates image preview.
 */
protected void updatePreview ()
{
    if ( image != null )
    {
        // Creating image preview
        image = ImageUtils.createImageThumbnail ( actualImage, width, height );

        // Restore decoration
        final BufferedImage f = ImageUtils.createCompatibleImage ( image.getWidth (), image.getHeight (), Transparency.TRANSLUCENT );
        final Graphics2D g2d = f.createGraphics ();
        GraphicsUtils.setupAntialias ( g2d );
        g2d.setPaint ( Color.WHITE );
        g2d.fillRoundRect ( 0, 0, image.getWidth (), image.getHeight (), round * 2, round * 2 );
        g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
        g2d.drawImage ( image, 0, 0, null );
        g2d.dispose ();

        image.flush ();
        image = f;
    }
}
 
Example #3
Source File: GalleryTransferHandler.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean filesDropped ( @NotNull final List<File> files )
{
    boolean added = false;
    if ( gallery != null )
    {
        for ( final File file : files )
        {
            if ( ImageUtils.isImageSupported ( file.getName () ) )
            {
                gallery.addImage ( new ImageIcon ( file.getAbsolutePath () ) );
                added = true;
            }
        }
    }
    return added;
}
 
Example #4
Source File: ThumbnailGenerator.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Applies generated thumbnail to the element.
 *
 * @param thumbnail thumbnail icon
 */
protected void applyThumbnail ( @NotNull final Icon thumbnail )
{
    if ( !aborted )
    {
        // Applying custom generated thumbnail icon
        synchronized ( element.getLock () )
        {
            if ( element.isThumbnailQueued () )
            {
                // We had to check that queue wasn't cancelled from outside
                element.setEnabledThumbnail ( thumbnail );
            }
        }
        if ( !aborted && disabled )
        {
            // Re-using enabled state thumbnail to generate disabled state one
            final Icon disabledThumbnail = ImageUtils.createDisabledCopy ( thumbnail );
            if ( !aborted && element.isDisabledThumbnailQueued () )
            {
                // We had to check that queue wasn't cancelled from outside
                element.setDisabledThumbnail ( disabledThumbnail );
            }
        }
    }
}
 
Example #5
Source File: ThumbnailGenerator.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns thumbnail for image from the specified {@link File}.
 * Note that this method will generate {@link Icon} with a static image, so any GIF or other animated images will be static.
 *
 * @param file file to generate thumbnail for
 * @return thumbnail for image from the specified {@link File}
 */
@Nullable
protected Icon createThumbnailIcon ( @NotNull final File file )
{
    Icon thumbnail;
    try
    {
        final BufferedImage bufferedImage = ImageUtils.loadBufferedImage ( new FileResource ( file ) );
        final BufferedImage previewImage = ImageUtils.createImageThumbnail ( bufferedImage, Math.min ( size.width, size.height ) );
        final ImageIcon previewIcon = new ImageIcon ( previewImage );
        previewIcon.setDescription ( previewImage.getWidth () + "x" + previewImage.getHeight () );
        thumbnail = previewIcon;
    }
    catch ( final Exception e )
    {
        thumbnail = null;
    }
    return thumbnail;
}
 
Example #6
Source File: AlphaLayerBackground.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns new {@link BufferedImage} containg alpha background texture of the specified size and colors.
 *
 * @param size       {@link Dimension} of a single cell, texture size will be double of this
 * @param darkColor  dark cells {@link Color}
 * @param lightColor light cells {@link Color}
 * @return new {@link BufferedImage} containg alpha background texture of the specified size and colors
 */
@NotNull
public static BufferedImage createAlphaBackgroundTexture ( @NotNull final Dimension size, @NotNull final Color darkColor,
                                                           @NotNull final Color lightColor )
{
    final BufferedImage image = ImageUtils.createCompatibleImage ( size.width * 2, size.height * 2, Transparency.OPAQUE );
    final Graphics2D g2d = image.createGraphics ();

    g2d.setPaint ( darkColor );
    g2d.fillRect ( 0, 0, size.width, size.height );
    g2d.fillRect ( size.width, size.height, size.width, size.height );

    g2d.setPaint ( lightColor );
    g2d.fillRect ( size.width, 0, size.width, size.height );
    g2d.fillRect ( 0, size.height, size.width, size.height );

    g2d.dispose ();
    return image;
}
 
Example #7
Source File: WebShadow.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns shadow image based on provided shape.
 *
 * @param bounds  shadow bounds
 * @param width   shadow width
 * @param opacity shadow opacity
 * @param color   shadow color
 * @param shape   shadow shape
 * @return shadow image based on provided shape
 */
@NotNull
public static BufferedImage createShadowImage ( @NotNull final Rectangle bounds, final int width, final float opacity,
                                                @NotNull final Color color, @NotNull final Shape shape )
{
    // Creating template image
    final BufferedImage bi = ImageUtils.createCompatibleImage ( bounds.width, bounds.height, Transparency.TRANSLUCENT );
    final Graphics2D ig = bi.createGraphics ();
    GraphicsUtils.setupAntialias ( ig );
    ig.translate ( -bounds.x, -bounds.y );
    ig.setPaint ( Color.BLACK );
    ig.fill ( shape );
    ig.dispose ();

    // Creating shadow image
    final ShadowFilter sf = new ShadowFilter ( width, 0, 0, opacity );
    sf.setShadowColor ( Color.BLACK.getRGB () );
    final BufferedImage shadow = sf.filter ( bi, null );

    // Clipping shadow image
    final Graphics2D g2d = shadow.createGraphics ();
    GraphicsUtils.setupAntialias ( g2d );
    g2d.translate ( -bounds.x, -bounds.y );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.setPaint ( ColorUtils.transparent () );
    g2d.fill ( shape );
    g2d.setPaint ( color );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.fillRect ( 0, 0, bounds.width, bounds.height );
    g2d.dispose ();

    return shadow;
}
 
Example #8
Source File: ImagePainter.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates cached disabled image.
 */
protected void calculateDisabledImage ()
{
    final BufferedImage image = component.getImage ();
    disabledImage = image != null ? ImageUtils.createDisabledCopy ( image ) : null;
    lastPreviewImage = null;
}
 
Example #9
Source File: ComponentUtils.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
private void chooseImage() {
    int state = mImgChooser.showOpenDialog(this);
    if (state != WebFileChooser.APPROVE_OPTION)
        return;

    File imgFile = mImgChooser.getSelectedFile();
    if (!imgFile.isFile())
        return;

    BufferedImage img = MediaUtils.readImage(imgFile).orElse(null);
    if (img == null)
        return;

    this.changeImage(ImageUtils.createPreviewImage(img, mSize));
}
 
Example #10
Source File: WebFileChooser.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets custom dialog icons.
 *
 * @param customIcons new custom dialog icons
 */
public void setDialogIcons ( @NotNull final List<? extends Icon> customIcons )
{
    this.customIcons = new ArrayList<Image> ( customIcons.size () );
    for ( final Icon icon : customIcons )
    {
        final Image image = ImageUtils.toBufferedImage ( icon );
        this.customIcons.add ( image );
    }
    updateWindowIcons ();
}
 
Example #11
Source File: MixedIcon.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns new {@link MixedIcon} with semi-transparent copies of originally mixed {@link Icon}s.
 *
 * @return new {@link MixedIcon} with semi-transparent copies of originally mixed {@link Icon}s
 */
@Override
@NotNull
public I createTransparentCopy ( final float opacity )
{
    final Icon[] transparent = new Icon[ icons.length ];
    for ( int i = 0; i < icons.length; i++ )
    {
        transparent[ i ] = ImageUtils.getTransparentCopy ( icons[ i ], opacity );
    }
    return newInstance ( transparent );
}
 
Example #12
Source File: MixedIcon.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns new {@link MixedIcon} with disabled copies of originally mixed {@link Icon}s.
 *
 * @return new {@link MixedIcon} with disabled copies of originally mixed {@link Icon}s
 */
@Override
@NotNull
public I createDisabledCopy ()
{
    final Icon[] disabled = new Icon[ icons.length ];
    for ( int i = 0; i < icons.length; i++ )
    {
        disabled[ i ] = ImageUtils.getDisabledCopy ( icons[ i ] );
    }
    return newInstance ( disabled );
}
 
Example #13
Source File: ThumbnailGenerator.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Generates standard thumbnail for the specified {@link File}.
 *
 * @param file    {@link File} to generate standard thumbnail for
 * @param preview whether should create thumbnail for image from the specified {@link File}
 */
protected void createStandardThumbnail ( @NotNull final File file, final boolean preview )
{
    // Using either image thumbnails or default file extension icons
    if ( preview && ImageUtils.isImageSupported ( file.getName () ) )
    {
        // If thumbnail was already specified we should re-use it
        // It will save us a lot of time if we simply need to generate disabled state in addition to enabled one
        final Icon thumb = element.getEnabledThumbnail () != null ? element.getEnabledThumbnail () :
                createThumbnailIcon ( file );
        if ( thumb != null )
        {
            // Applying standard image thumbnail
            applyThumbnail ( thumb );
        }
        else
        {
            // Applying standard extension icon
            applyStandardIcon ( file );
        }
    }
    else
    {
        // Applying standard extension icon
        applyStandardIcon ( file );
    }
}
 
Example #14
Source File: WebShadow.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns inner shadow image based on provided shape.
 *
 * @param bounds  shadow bounds
 * @param width   shadow width
 * @param opacity shadow opacity
 * @param color   shadow color
 * @param shape   shadow shape
 * @return inner shadow image based on provided shape
 */
@NotNull
public static BufferedImage createInnerShadowImage ( @NotNull final Rectangle bounds, final int width, final float opacity,
                                                     @NotNull final Color color, @NotNull final Shape shape )
{
    final Rectangle b = new Rectangle ( bounds.x - width * 2, bounds.y - width * 2,
            bounds.width + width * 4, bounds.height + width * 4 );

    // Creating template image
    final BufferedImage bi = ImageUtils.createCompatibleImage ( b.width, b.height, Transparency.TRANSLUCENT );
    final Graphics2D ig = bi.createGraphics ();
    GraphicsUtils.setupAntialias ( ig );
    ig.translate ( -b.x, -b.y );
    final Area area = new Area ( new Rectangle ( b.x, b.y, b.width, b.height ) );
    area.exclusiveOr ( new Area ( shape ) );
    ig.setPaint ( Color.BLACK );
    ig.fill ( area );
    ig.dispose ();

    // Creating inner shadow image
    final ShadowFilter sf = new ShadowFilter ( width, 0, 0, opacity );
    sf.setShadowColor ( Color.BLACK.getRGB () );
    final BufferedImage shadow = sf.filter ( bi, null );

    // Clipping inner shadow image
    final Graphics2D g2d = shadow.createGraphics ();
    GraphicsUtils.setupAntialias ( g2d );
    g2d.translate ( -b.x, -b.y );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.setPaint ( ColorUtils.transparent () );
    g2d.fill ( area );
    g2d.setPaint ( color );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.fillRect ( 0, 0, b.width, b.height );
    g2d.dispose ();

    return shadow.getSubimage ( width * 2, width * 2, b.width - width * 4, b.height - width * 4 );
}
 
Example #15
Source File: ExpandingShadow.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns newly created shadow {@link NinePatchIcon}.
 *
 * @param bounds  {@link NinePatchIcon} bounds
 * @param width   shadow width
 * @param opacity shadow opacity
 * @return newly created shadow {@link NinePatchIcon}
 */
@NotNull
protected NinePatchIcon createShadowIcon ( @NotNull final Rectangle bounds, final int width, final float opacity )
{
    // Creating shadow pattern
    final GeneralPath gp = new GeneralPath ( GeneralPath.WIND_EVEN_ODD );
    gp.moveTo ( bounds.x + width * 1.2, bounds.y + width * 1.2 );
    gp.lineTo ( bounds.x + bounds.width - width * 1.2, bounds.y + width * 1.2 );
    gp.lineTo ( bounds.x + bounds.width - width, bounds.y + bounds.height - width * 0.5 );
    gp.quadTo ( bounds.x + bounds.width / 2, bounds.y + bounds.height - width * 1.9,
            bounds.x + width, bounds.y + bounds.height - width * 0.5 );
    gp.closePath ();

    // Creating shadow image
    final BufferedImage shadowImage = ImageUtils.createShadowImage ( bounds.width, bounds.height, gp, width, opacity, false );

    // Creating nine-patch icon based on shadow image
    final int w = shadowImage.getWidth ();
    final int inner = width / 2;
    final NinePatchIcon ninePatchIcon = new NinePatchIcon ( shadowImage, false );
    ninePatchIcon.addHorizontalStretch ( 0, width + inner, true );
    ninePatchIcon.addHorizontalStretch ( width + inner + 1, w - width - inner - 1, false );
    ninePatchIcon.addHorizontalStretch ( w - width - inner, w, true );
    ninePatchIcon.addVerticalStretch ( 0, width + inner, true );
    ninePatchIcon.addVerticalStretch ( width + inner + 1, w - width - inner - 1, false );
    ninePatchIcon.addVerticalStretch ( w - width - inner, w, true );
    ninePatchIcon.setMargin ( width );
    return ninePatchIcon;
}
 
Example #16
Source File: PaletteColorChooser.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
private Cursor createLoopCursor ()
{
    final Dimension dimension = Toolkit.getDefaultToolkit ().getBestCursorSize ( 14, 14 );

    final BufferedImage bufferedImage =
            ImageUtils.createCompatibleImage ( dimension.width, dimension.height, Transparency.TRANSLUCENT );

    final Graphics2D g2d = bufferedImage.createGraphics ();
    g2d.drawImage ( LOOP_ICON.getImage (), 0, 0, LOOP_ICON.getImageObserver () );
    g2d.dispose ();

    return Toolkit.getDefaultToolkit ().createCustomCursor ( bufferedImage, new Point ( 7, 7 ), "Loop Cursor" );
}
 
Example #17
Source File: GifFrame.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns copy of this {@link GifFrame} with {@link #bufferedImage} made look disabled.
 *
 * @return copy of this {@link GifFrame} with {@link #bufferedImage} made look disabled
 */
@NotNull
@Override
public GifFrame createDisabledCopy ()
{
    return new GifFrame ( ImageUtils.createDisabledCopy ( bufferedImage ), delay );
}
 
Example #18
Source File: GifFrame.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns copy of this {@link GifFrame} with {@link #bufferedImage} made semi-transparent.
 *
 * @return copy of this {@link GifFrame} with {@link #bufferedImage} made semi-transparent
 */
@NotNull
@Override
public GifFrame createTransparentCopy ( final float opacity )
{
    return new GifFrame ( ImageUtils.createTransparentCopy ( bufferedImage, opacity ), delay );
}
 
Example #19
Source File: SimpleDragViewHandler.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
@Override
public BufferedImage getView ( @NotNull final T object, @NotNull final DragSourceDragEvent event )
{
    final Icon icon = getIcon ( object );
    final Color foreground = getForeground ( object );
    final String title = LM.get ( getText ( object ) );

    final FontMetrics fm = getFontMetrics ( object );
    final int tm = margin.left + ( icon != null ? icon.getIconWidth () + 4 : textSideSpacing );
    final int em = margin.right + textSideSpacing;
    final int w = tm + fm.stringWidth ( title ) + em;
    final int h = margin.top + Math.max ( icon != null ? icon.getIconHeight () : 0, fm.getHeight () ) + margin.bottom;

    final BufferedImage image = ImageUtils.createCompatibleImage ( w, h, Transparency.TRANSLUCENT );
    final Graphics2D g2d = image.createGraphics ();
    GraphicsUtils.setupAlphaComposite ( g2d, 0.8f );
    GraphicsUtils.setupFont ( g2d, fm.getFont () );
    SwingUtils.setupTextAntialias ( g2d, TextRasterization.subpixel );
    g2d.setPaint ( Color.WHITE );
    g2d.fillRect ( 0, 0, w, h );
    g2d.setPaint ( Color.LIGHT_GRAY );
    g2d.drawRect ( 0, 0, w - 1, h - 1 );
    if ( icon != null )
    {
        icon.paintIcon ( null, g2d, margin.left, margin.top );
    }
    g2d.setPaint ( foreground != null ? foreground : Color.BLACK );
    g2d.drawString ( title, tm, margin.top + ( h - margin.top - margin.bottom ) / 2 + LafUtils.getTextCenterShiftY ( fm ) );
    g2d.dispose ();
    return image;
}
 
Example #20
Source File: LazyIcon.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns semi-transparent copy of original {@link Icon}.
 *
 * @return semi-transparent copy of original {@link Icon}
 */
@NotNull
@Override
public Icon createTransparentCopy ( final float opacity )
{
    return ImageUtils.getTransparentCopy ( getIcon (), opacity );
}
 
Example #21
Source File: LazyIcon.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns disabled copy of original {@link Icon}.
 *
 * @return disabled copy of original {@link Icon}
 */
@NotNull
@Override
public Icon createDisabledCopy ()
{
    return ImageUtils.getDisabledCopy ( getIcon () );
}
 
Example #22
Source File: WebDecoratedImage.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public WebDecoratedImage ( @NotNull final Icon icon )
{
    this ( ImageUtils.toNonNullImageIcon ( icon ) );
}
 
Example #23
Source File: PaletteColorChooser.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public PaletteColorChooser ()
{
    super ();

    paletteColorChooserPaint = new PaletteColorChooserPaint ( 0, 0, 256, 256, sideColor );
    image = ImageUtils.createCompatibleImage ( 256, 256, Transparency.TRANSLUCENT );
    coordinate = new Point ( 2, 2 );
    repaintImage ();

    setLayout ( new TableLayout ( new double[][]{ { TableLayout.PREFERRED }, { 3, TableLayout.PREFERRED, 3 } } ) );

    colorChooser = new JComponent ()
    {
        @Override
        protected void paintComponent ( final Graphics g )
        {
            super.paintComponent ( g );

            final Graphics2D g2d = ( Graphics2D ) g;

            final Shape old = g2d.getClip ();
            final Area clip = new Area ( new Rectangle2D.Double ( 2, 2, getWidth () - 4, getHeight () - 4 ) );
            clip.intersect ( new Area ( old ) );
            g2d.setClip ( clip );

            g2d.drawImage ( image, 2, 2, null );
            g2d.drawImage ( LOOP_ICON.getImage (), coordinate.x - LOOP_ICON.getIconWidth () / 2,
                    coordinate.y - LOOP_ICON.getIconHeight () / 2, LOOP_ICON.getImageObserver () );

            g2d.setClip ( old );
        }
    };
    colorChooser.setBorder ( BorderFactory.createCompoundBorder ( BorderFactory.createLineBorder ( Color.GRAY, 1 ),
            BorderFactory.createLineBorder ( Color.WHITE, 1 ) ) );
    //        colorChooser.setBorder ( BorderFactory
    //                .createBevelBorder ( BevelBorder.LOWERED, Color.WHITE, Color.WHITE,
    //                        new Color ( 160, 160, 160 ), new Color ( 178, 178, 178 ) ) );
    colorChooser.setPreferredSize ( new Dimension ( 260, 260 ) );

    final ColorChooserMouseAdapter adapter = new ColorChooserMouseAdapter ();
    colorChooser.addMouseListener ( adapter );
    colorChooser.addMouseMotionListener ( adapter );

    add ( colorChooser, "0,1" );
}
 
Example #24
Source File: ComponentInstantiationTest.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns {@link RenderedImage} that can be used for tests.
 *
 * @return {@link RenderedImage} that can be used for tests
 */
@NotNull
private static RenderedImage renderedImage16 ()
{
    return ImageUtils.toNonNullBufferedImage ( WebLookAndFeel.getIcon ( 16 ) );
}
 
Example #25
Source File: ComponentInstantiationTest.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns {@link BufferedImage} that can be used for tests.
 *
 * @return {@link BufferedImage} that can be used for tests
 */
@NotNull
private static BufferedImage bufferedImage16 ()
{
    return ImageUtils.toNonNullBufferedImage ( WebLookAndFeel.getIcon ( 16 ) );
}
 
Example #26
Source File: NinePatchEditorFrame.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
protected BufferedImage getSampleImage ()
{
    return ImageUtils.loadBufferedImage ( new ClassResource ( NinePatchEditorFrame.class, "icons/example.png" ) );
}
 
Example #27
Source File: ImageFilesFilter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean accept ( @NotNull final File file )
{
    return ImageUtils.isImageSupported ( file.getName () );
}
 
Example #28
Source File: WebDecoratedImage.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public WebDecoratedImage ( @NotNull final Image image )
{
    this ( ImageUtils.toNonNullImageIcon ( image ) );
}
 
Example #29
Source File: ImageIconSource.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
@Override
public ImageIcon loadIcon ( @NotNull final Resource resource )
{
    return ImageUtils.loadImageIcon ( resource );
}
 
Example #30
Source File: NinePatchIcon.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructs new NinePatchIcon using the specified nine-patch image.
 *
 * @param image        {@link BufferedImage}
 * @param parsePatches whether or not information about patches should be parsed from the image
 */
public NinePatchIcon ( @NotNull final BufferedImage image, final boolean parsePatches )
{
    // Parsing patches or creating new 9-patch icon
    if ( parsePatches )
    {
        // Incorrect image
        if ( image.getWidth () < 3 || image.getHeight () < 3 )
        {
            throw new IllegalArgumentException ( "Buffered image must be atleast 3x3 pixels size" );
        }

        // Creating actual image in a compatible format
        final int w = image.getWidth () - 2;
        final int h = image.getHeight () - 2;
        rawImage = ImageUtils.createCompatibleImage ( w, h, image.getTransparency () );
        final Graphics2D g2d = rawImage.createGraphics ();
        g2d.drawImage ( image, 0, 0, w, h, 1, 1, image.getWidth () - 1, image.getHeight () - 1, null );
        g2d.dispose ();

        // Parsing stretch variables
        horizontalStretch = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.horizontalStretch );
        verticalStretch = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.verticalStretch );

        // Incorrect image
        if ( !( ( horizontalStretch.size () > 1 || horizontalStretch.size () == 1 && !horizontalStretch.get ( 0 ).isPixel () ) &&
                ( verticalStretch.size () > 1 || verticalStretch.size () == 1 && !verticalStretch.get ( 0 ).isPixel () ) ) )
        {
            throw new IllegalArgumentException ( "There must be stretch constraints specified on image" );
        }

        // Parsing content margins
        final List<NinePatchInterval> vc = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.verticalContent );
        final List<NinePatchInterval> hc = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.horizontalContent );
        final int top = vc.size () == 0 ? 0 : vc.get ( 0 ).getStart ();
        final int bottom = vc.size () == 0 ? 0 : rawImage.getHeight () - vc.get ( 0 ).getEnd () - 1;
        final int left = hc.size () == 0 ? 0 : hc.get ( 0 ).getStart ();
        final int right = hc.size () == 0 ? 0 : rawImage.getWidth () - hc.get ( 0 ).getEnd () - 1;
        margin = new Insets ( top, left, bottom, right );

        // Forcing cached data calculation on initialization
        getFixedPixelsWidth ( true );
        getFixedPixelsWidth ( false );
        getFixedPixelsHeight ( true );
        getFixedPixelsHeight ( false );
    }
    else
    {
        // Actual image
        this.rawImage = image;

        // Stretch variables
        horizontalStretch = new ArrayList<NinePatchInterval> ();
        verticalStretch = new ArrayList<NinePatchInterval> ();

        // Empty margin
        margin = new Insets ( 0, 0, 0, 0 );
    }
}