Example usage for javax.swing Icon paintIcon

List of usage examples for javax.swing Icon paintIcon

Introduction

In this page you can find the example usage for javax.swing Icon paintIcon.

Prototype

void paintIcon(Component c, Graphics g, int x, int y);

Source Link

Document

Draw the icon at the specified location.

Usage

From source file:RGBGrayFilter.java

/**
 * Returns an icon with a disabled appearance. This method is used
 * to generate a disabled icon when one has not been specified.
 *
 * @param component the component that will display the icon, may be null.
 * @param icon the icon to generate disabled icon from.
 * @return disabled icon, or null if a suitable icon can not be generated.
 *///from www .  j a  v  a 2 s .c  om
public static Icon getDisabledIcon(JComponent component, Icon icon) {
    if ((icon == null) || (component == null) || (icon.getIconWidth() == 0) || (icon.getIconHeight() == 0)) {
        return null;
    }
    Image img;
    if (icon instanceof ImageIcon) {
        img = ((ImageIcon) icon).getImage();
    } else {
        img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
    }

    ImageProducer producer = new FilteredImageSource(img.getSource(), new RGBGrayFilter());

    return new ImageIcon(component.createImage(producer));
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image iconToImage(@Nonnull Component context, @Nullable final Icon icon) {
    if (icon instanceof ImageIcon) {
        return ((ImageIcon) icon).getImage();
    }// w ww.ja va  2  s  .c o m
    final int width = icon == null ? 16 : icon.getIconWidth();
    final int height = icon == null ? 16 : icon.getIconHeight();
    final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    if (icon != null) {
        final Graphics g = image.getGraphics();
        try {
            icon.paintIcon(context, g, 0, 0);
        } finally {
            g.dispose();
        }
    }
    return image;
}

From source file:SysTray.java

private Image getImage() throws HeadlessException {
    Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
    Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(),
            BufferedImage.TYPE_4BYTE_ABGR);
    defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
    return img;/*from  www .  j  av  a 2  s .  c  o m*/
}

From source file:com.devbury.mkremote.server.QuickLaunchServiceImpl.java

public ArrayList<LaunchItem> list(String dir) {
    Base64 base64 = new Base64();
    JFileChooser chooser = new JFileChooser();
    File new_dir = newFileDir(dir);
    logger.debug("Looking for files in {}", new_dir.getAbsolutePath());
    ArrayList<LaunchItem> items = new ArrayList<LaunchItem>();
    if (isSupported()) {
        if (new_dir.isDirectory()) {
            FilenameFilter filter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return !name.startsWith(".");
                }/*from   w w w . j  a  v  a  2s  . c  o m*/
            };
            for (File f : new_dir.listFiles(filter)) {
                if (!f.isHidden()) {
                    LaunchItem item = new LaunchItem();
                    item.setName(f.getName());
                    item.setPath(dir);
                    if (f.isDirectory()) {
                        if (isMac() && f.getName().endsWith(".app")) {
                            item.setType(LaunchItem.FILE_TYPE);
                            item.setName(f.getName().substring(0, f.getName().length() - 4));
                        } else {
                            item.setType(LaunchItem.DIR_TYPE);
                        }
                    } else {
                        item.setType(LaunchItem.FILE_TYPE);
                    }
                    Icon icon = chooser.getIcon(f);
                    BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                            BufferedImage.TYPE_INT_RGB);
                    icon.paintIcon(null, bi.createGraphics(), 0, 0);
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    try {
                        ImageIO.write(bi, "png", os);
                        item.setIcon(base64.encodeToString(os.toByteArray()));
                    } catch (IOException e) {
                        logger.debug("could not write image {}", e);
                        item.setIcon(null);
                    }
                    logger.debug("Adding LaunchItem : {}", item);
                    items.add(item);
                } else {
                    logger.debug("Skipping hidden file {}", f.getName());
                }
            }
        }
    } else {
        new Thread() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null,
                        "We are sorry but quick launch is not supported on your platform",
                        "Quick Launch Not Supported", JOptionPane.ERROR_MESSAGE);
            }
        }.start();
    }
    return items;
}

From source file:edu.ku.brc.ui.GradiantButton.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int w = this.getWidth();
    int h = this.getHeight();

    drawButtonBody(g2, w, h, getForeground());

    if (pressed) {
        g2.translate(1, 1);//w  w w . ja v a2s .  c o  m
    }

    String text = getText();
    if (isNotEmpty(text)) {
        drawText(g2, w, h, getText());
    }

    Icon roIcon = getRolloverIcon();
    Icon paintedIcon = isHover && roIcon != null ? roIcon : icon;
    if (paintedIcon != null) {
        //Graphics2D g2 = (Graphics2D) g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, iconAlpha));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        paintedIcon.paintIcon(this, g2, (w - icon.getIconWidth()) / 2, (h - icon.getIconHeight()) / 2);
    }
}

From source file:com.cburch.logisim.instance.InstanceFactory.java

@Override
public final void paintIcon(ComponentDrawContext context, int x, int y, AttributeSet attrs) {
    InstancePainter painter = context.getInstancePainter();
    painter.setFactory(this, attrs);
    Graphics g = painter.getGraphics();
    g.translate(x, y);//from www.ja v a 2 s .c o  m
    paintIcon(painter);
    g.translate(-x, -y);

    if (painter.getFactory() == null) {
        Icon i = icon;
        if (i == null) {
            String n = iconName;
            if (n != null) {
                i = Icons.getIcon(n);
                if (i == null)
                    n = null;
            }
        }
        if (i != null) {
            i.paintIcon(context.getDestination(), g, x + 2, y + 2);
        } else {
            super.paintIcon(context, x, y, attrs);
        }
    }
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided icon.
 */// w w w  .ja v a  2s.  co  m
private void setupIconPainter(Icon texture) {
    if (texture == null) {
        ourPainter = null;
        return;
    }

    int w = texture.getIconWidth();
    int h = texture.getIconHeight();

    if (w <= 0 || h <= 0) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    texture.paintIcon(this, g2, 0, 0);
    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, w, h));

    g2.dispose();
}

From source file:com.hexidec.ekit.component.RelativeImageView.java

/**
  * Paints the image./*from w ww  . ja va 2s  . com*/
  *
  * @param g the rendering surface to use
  * @param a the allocated region to render into
  * @see View#paint
  */
public void paint(Graphics g, Shape a) {
    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}

From source file:gdt.jgui.entity.webset.JWeblinkEditor.java

/**
 * Get the context locator./*  w  ww.  j av  a2s .c  o  m*/
 * @return the context locator.
 */
@Override
public String getLocator() {
    try {
        Properties locator = new Properties();
        locator.setProperty(BaseHandler.HANDLER_CLASS, getClass().getName());
        locator.setProperty(BaseHandler.HANDLER_SCOPE, JConsoleHandler.CONSOLE_SCOPE);
        locator.setProperty(JContext.CONTEXT_TYPE, getType());
        locator.setProperty(Locator.LOCATOR_TITLE, getTitle());
        if (entityLabel$ != null) {
            locator.setProperty(EntityHandler.ENTITY_LABEL, entityLabel$);
        }
        if (entityKey$ != null)
            locator.setProperty(EntityHandler.ENTITY_KEY, entityKey$);
        if (entihome$ != null)
            locator.setProperty(Entigrator.ENTIHOME, entihome$);
        if (webLinkKey$ != null)
            locator.setProperty(JWeblinksPanel.WEB_LINK_KEY, webLinkKey$);
        String icon$ = null;
        try {
            Icon icon = iconIcon.getIcon();
            int type = BufferedImage.TYPE_INT_RGB;
            BufferedImage out = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), type);
            Graphics2D g2 = out.createGraphics();
            icon.paintIcon(new JPanel(), out.getGraphics(), 0, 0);
            g2.dispose();
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            ImageIO.write(out, "png", b);
            b.close();
            byte[] ba = b.toByteArray();
            icon$ = Base64.encodeBase64String(ba);
        } catch (Exception ee) {
        }
        if (icon$ == null)
            icon$ = Support.readHandlerIcon(null, JEntitiesPanel.class, "edit.png");
        locator.setProperty(Locator.LOCATOR_ICON, icon$);
        return Locator.toString(locator);
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).severe(e.toString());
        return null;
    }

}

From source file:Main.java

public void paint(Graphics g, JComponent c) {

    JLabel label = (JLabel) c;
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return;/*from  w w  w  . j  av a  2  s  .  co  m*/
    }

    FontMetrics fm = g.getFontMetrics();
    paintViewInsets = c.getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;

    // Use inverted height & width
    paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform tr = g2.getTransform();
    if (clockwise) {
        g2.rotate(Math.PI / 2);
        g2.translate(0, -c.getWidth());
    } else {
        g2.rotate(-Math.PI / 2);
        g2.translate(-c.getHeight(), 0);
    }

    if (icon != null) {
        icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
    }

    if (text != null) {
        int textX = paintTextR.x;
        int textY = paintTextR.y + fm.getAscent();

        if (label.isEnabled()) {
            paintEnabledText(label, g, clippedText, textX, textY);
        } else {
            paintDisabledText(label, g, clippedText, textX, textY);
        }
    }

    g2.setTransform(tr);
}