Java Code Examples for javax.swing.Icon#getIconWidth()

The following examples show how to use javax.swing.Icon#getIconWidth() . 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: ImageCombiner.java    From Spark with Apache License 2.0 9 votes vote down vote up
/**
 * Creates an Image from the specified Icon
 * 
 * @param icon
 *            that should be converted to an image
 * @return the new image
 */
public static Image iconToImage(Icon icon) {
    if (icon instanceof ImageIcon) {
        return ((ImageIcon) icon).getImage();
    } else {
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage image = gc.createCompatibleImage(w, h);
        Graphics2D g = image.createGraphics();
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        return image;
    }
}
 
Example 2
Source File: IGButton.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	g.setFont(getFont());
	g.setColor(getForeground());
	if (getModel().isPressed()) {
		largeButtonPressed.paintTo(g2, 0, 0, getWidth(), getHeight(), true, getText());
	} else {
		largeButton.paintTo(g2, 0, 0, getWidth(), getHeight(), false, getText());
	}
	Icon icon = getIcon();
	if (icon != null) {
		int w = (getWidth() - icon.getIconWidth()) / 2;
		int h = (getHeight() - icon.getIconHeight()) / 2;
		icon.paintIcon(this, g, w, h);
	}
	if (!isEnabled()) {
		RenderTools.fill(g2, 0, 0, getWidth(), getHeight(), disabledPattern);
	}
}
 
Example 3
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
    * Converts a File holding an Image into a Buffered Image
    * 
    * @param file
    *            {@link File}
    * @return {@link BufferedImage}
    */
   public static BufferedImage getBufferedImage(File file) {
// Why wasn't this using it's code that pulled from the file? Hrm.
   //Icon icon = SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32);
   Icon icon = null;

BufferedImage bi = new BufferedImage(icon.getIconWidth(),
	icon.getIconHeight(), BufferedImage.OPAQUE);
Graphics bg = bi.getGraphics();

ImageIcon i = (ImageIcon) icon;

bg.drawImage(i.getImage(), 0, 0, null);
bg.dispose();

return bi;
   }
 
Example 4
Source File: CustomScopePanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Image getIcon(int type) {
    Icon icon = data.getIcon();
    if (icon instanceof ImageIcon) {
        return ((ImageIcon) icon).getImage();
    } else {
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage image = gc.createCompatibleImage(w, h);
        Graphics2D g = image.createGraphics();
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        return image;
    }
}
 
Example 5
Source File: ImageCombiner.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates an Image from the specified Icon
 * 
 * @param icon
 *            that should be converted to an image
 * @return the new image
 */
public static Image iconToImage(Icon icon) {
    if (icon instanceof ImageIcon) {
        return ((ImageIcon) icon).getImage();
    } else {
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        BufferedImage image = gc.createCompatibleImage(w, h);
        Graphics2D g = image.createGraphics();
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        return image;
    }
}
 
Example 6
Source File: VariablesViewButtons.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static JButton createButton (String iconPath, String tooltip) {
    Icon icon = ImageUtilities.loadImageIcon(iconPath, false);
    final JButton button = new JButton(icon);
    // ensure small size, just for the icon
    Dimension size = new Dimension(icon.getIconWidth() + 8, icon.getIconHeight() + 8);
    button.setPreferredSize(size);
    button.setMargin(new Insets(1, 1, 1, 1));
    button.setBorder(new EmptyBorder(button.getBorder().getBorderInsets(button)));
    button.setToolTipText(tooltip);
    button.setFocusable(false);
    return button;
}
 
Example 7
Source File: VariablesViewButtons.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static JButton createButton (Icon icon, String tooltip) {
    final JButton button = new JButton(icon);
    // ensure small size, just for the icon
    Dimension size = new Dimension(icon.getIconWidth() + 8, icon.getIconHeight() + 8);
    button.setPreferredSize(size);
    button.setMargin(new Insets(1, 1, 1, 1));
    button.setBorder(new EmptyBorder(button.getBorder().getBorderInsets(button)));
    button.setToolTipText(tooltip);
    button.setFocusable(false);
    return button;
}
 
Example 8
Source File: SampleTreeCellRenderer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * paint is subclassed to draw the background correctly.  JLabel
 * currently does not allow backgrounds other than white, and it
 * will also fill behind the icon.  Something that isn't desirable.
 */
@Override
public void paint(Graphics g) {
    Color bColor;
    Icon currentI = getIcon();

    if (selected) {
        bColor = SELECTED_BACKGROUND_COLOR;
    } else if (getParent() != null) /* Pick background color up from parent (which will come from
    the JTree we're contained in). */ {
        bColor = getParent().getBackground();
    } else {
        bColor = getBackground();
    }
    g.setColor(bColor);
    if (currentI != null && getText() != null) {
        int offset = (currentI.getIconWidth() + getIconTextGap());

        if (getComponentOrientation().isLeftToRight()) {
            g.fillRect(offset, 0, getWidth() - 1 - offset,
                    getHeight() - 1);
        } else {
            g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
        }
    } else {
        g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
    super.paint(g);
}
 
Example 9
Source File: ReplaceDefaultMarkupItemAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ReplaceDefaultMarkupItemAction(VTController controller, boolean addToToolbar) {
	super(controller, "Apply (Replace Default Only)");

	Icon replacedIcon = VTPlugin.REPLACED_ICON;
	ImageIcon warningIcon = ResourceManager.loadImage("images/warning.png");
	warningIcon = ResourceManager.getScaledIcon(warningIcon, 12, 12);
	int warningIconWidth = warningIcon.getIconWidth();
	int warningIconHeight = warningIcon.getIconHeight();

	MultiIcon multiIcon = new MultiIcon(replacedIcon);
	int refreshIconWidth = replacedIcon.getIconWidth();
	int refreshIconHeight = replacedIcon.getIconHeight();

	int x = refreshIconWidth - warningIconWidth;
	int y = refreshIconHeight - warningIconHeight;

	TranslateIcon translateIcon = new TranslateIcon(warningIcon, x, y);
	multiIcon.addIcon(translateIcon);

	if (addToToolbar) {
		setToolBarData(new ToolBarData(multiIcon, MENU_GROUP));
	}
	MenuData menuData =
		new MenuData(new String[] { "Apply (Replace Default Only)" }, replacedIcon, MENU_GROUP);
	menuData.setMenuSubGroup("2");
	setPopupMenuData(menuData);
	setEnabled(false);
	setHelpLocation(new HelpLocation("VersionTrackingPlugin", "Replace_Default_Markup_Item"));
}
 
Example 10
Source File: FiltersManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JToggleButton createToggle (Map fStates, int index) {
    boolean isSelected = filtersDesc.isSelected(index);
    Icon icon = filtersDesc.getSelectedIcon(index);
    // ensure small size, just for the icon
    JToggleButton result = new JToggleButton(icon, isSelected);
    Dimension size = new Dimension(icon.getIconWidth() + 6, icon.getIconHeight() + 4);
    result.setPreferredSize(size);
    result.setMargin(new Insets(2,3,2,3));
    result.setToolTipText(filtersDesc.getTooltip(index));
    
    fStates.put(filtersDesc.getName(index), Boolean.valueOf(isSelected));
    
    return result;
}
 
Example 11
Source File: SampleTreeCellRenderer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * paint is subclassed to draw the background correctly.  JLabel
 * currently does not allow backgrounds other than white, and it
 * will also fill behind the icon.  Something that isn't desirable.
 */
@Override
public void paint(Graphics g) {
    Color bColor;
    Icon currentI = getIcon();

    if (selected) {
        bColor = SELECTED_BACKGROUND_COLOR;
    } else if (getParent() != null) /* Pick background color up from parent (which will come from
    the JTree we're contained in). */ {
        bColor = getParent().getBackground();
    } else {
        bColor = getBackground();
    }
    g.setColor(bColor);
    if (currentI != null && getText() != null) {
        int offset = (currentI.getIconWidth() + getIconTextGap());

        if (getComponentOrientation().isLeftToRight()) {
            g.fillRect(offset, 0, getWidth() - 1 - offset,
                    getHeight() - 1);
        } else {
            g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
        }
    } else {
        g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
    super.paint(g);
}
 
Example 12
Source File: CustomToolbar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void addButton(AbstractButton button) {
    Icon icon = button.getIcon();
    Dimension size = new Dimension(icon.getIconWidth() + 6, icon.getIconHeight() + 10);
    button.setPreferredSize(size);
    button.setMargin(new Insets(5, 4, 5, 4));
    toolbar.add(button);
}
 
Example 13
Source File: WindowsDPIWorkaroundIcon.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @param delegate an icon implementation from
 *        {@code com.sun.java.swing.plaf.windows.WindowsIconFactory}, in its initial state at
 *        the time of LAF initialization (before display configuration changes are likely to
 *        have happened)
 */
public WindowsDPIWorkaroundIcon(Icon delegate) {
    if (delegate == null) {
        throw new NullPointerException();
    }
    this.delegate = delegate;
    /* As part of bug JDK-8211715, icons in WindowsIconFactory may actually change their
    reported size if the display configuration is changed while the application is running.
    Assuming this workaround is applied early in the application's life cycle, before any
    display configuration changes have happened, the initial value is the correct one. */
    this.width = delegate.getIconWidth();
    this.height = delegate.getIconHeight();
}
 
Example 14
Source File: CompositeIcon.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
  int dx = 0;
  for (final Icon icon : icons) {
    icon.paintIcon(c, g, x + dx, y);
    dx += GAP;
    dx += icon.getIconWidth();
  }
}
 
Example 15
Source File: SampleTreeCellRenderer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * paint is subclassed to draw the background correctly.  JLabel
 * currently does not allow backgrounds other than white, and it
 * will also fill behind the icon.  Something that isn't desirable.
 */
@Override
public void paint(Graphics g) {
    Color bColor;
    Icon currentI = getIcon();

    if (selected) {
        bColor = SELECTED_BACKGROUND_COLOR;
    } else if (getParent() != null) /* Pick background color up from parent (which will come from
    the JTree we're contained in). */ {
        bColor = getParent().getBackground();
    } else {
        bColor = getBackground();
    }
    g.setColor(bColor);
    if (currentI != null && getText() != null) {
        int offset = (currentI.getIconWidth() + getIconTextGap());

        if (getComponentOrientation().isLeftToRight()) {
            g.fillRect(offset, 0, getWidth() - 1 - offset,
                    getHeight() - 1);
        } else {
            g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
        }
    } else {
        g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
    super.paint(g);
}
 
Example 16
Source File: DecoratedListUI.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
protected void paintCell(Graphics g, int row, Rectangle rowBounds,
		ListCellRenderer cellRenderer, ListModel dataModel,
		ListSelectionModel selModel, int leadIndex) {
	super.paintCell(g, row, rowBounds, cellRenderer, dataModel, selModel,
			leadIndex);

	Object value = dataModel.getElementAt(row);
	boolean cellHasFocus = list.hasFocus() && (row == leadIndex);
	boolean isSelected = selModel.isSelectedIndex(row);

	ListDecoration[] decorations = getDecorations(list);
	for (int a = 0; a < decorations.length; a++) {
		if (decorations[a].isVisible(list, value, row, isSelected,
				cellHasFocus)) {
			Point p = decorations[a].getLocation(list, value, row,
					isSelected, cellHasFocus);
			Icon icon = decorations[a].getIcon(list, value, row,
					isSelected, cellHasFocus, false, false);
			// we assume rollover/pressed icons are same dimensions as
			// default icon
			Rectangle iconBounds = new Rectangle(rowBounds.x + p.x,
					rowBounds.y + p.y, icon.getIconWidth(),
					icon.getIconHeight());
			if (armedDecoration != null && armedDecoration.value == value
					&& armedDecoration.decoration == decorations[a]) {
				icon = decorations[a].getIcon(list, value, row, isSelected,
						cellHasFocus, false, true);
			} else if (iconBounds.contains(mouseX, mouseY)) {
				icon = decorations[a].getIcon(list, value, row, isSelected,
						cellHasFocus, true, false);
			}
			Graphics g2 = g.create();
			try {
				icon.paintIcon(list, g2, iconBounds.x, iconBounds.y);
			} finally {
				g2.dispose();
			}
		}
	}
}
 
Example 17
Source File: HtmlLabelUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Actually paint the icon and text using our own html rendering engine. */
private void paintIconAndText(Graphics g, HtmlRendererImpl r) {
    Font f = font(r);
    g.setFont(f);

    FontMetrics fm = g.getFontMetrics();

    //Find out what height we need
    int txtH = fm.getMaxAscent() + fm.getMaxDescent();
    Insets ins = r.getInsets();

    //find out the available height less the insets
    int rHeight = r.getHeight();
    int availH = rHeight - (ins.top + ins.bottom);

    int txtY;

    if (availH >= txtH) {
        //Center the text if we have space
        txtY = (txtH + ins.top + ((availH / 2) - (txtH / 2))) - fm.getMaxDescent();
    } else if (r.getHeight() > txtH) {
        txtY = txtH + (rHeight - txtH) / 2 - fm.getMaxDescent();
    } else {
        //Okay, it's not going to fit, punt.
        txtY = fm.getMaxAscent();
    }
    
    int txtX = r.getIndent();

    Icon icon = r.getIcon();

    //Check the icon non-null and height (see TabData.NO_ICON for why)
    if ((icon != null) && (icon.getIconWidth() > 0) && (icon.getIconHeight() > 0)) {
        int iconY;

        if (availH > icon.getIconHeight()) {
            //add 2 to make sure icon top pixels are not cut off by outline
            iconY = ins.top + ((availH / 2) - (icon.getIconHeight() / 2)); // + 2;
        } else if (availH == icon.getIconHeight()) {
            //They're an exact match, make it 0
            iconY = ins.top;
        } else {
            //Won't fit; make the top visible and cut the rest off (option:
            //center it and clip it on top and bottom - probably even harder
            //to recognize that way, though)
            iconY = ins.top;
        }

        //add in the insets
        int iconX = ins.left + r.getIndent() + 1; //+1 to get it out of the way of the focus border

        try {
            //Diagnostic - the CPP module currently is constructing
            //some ImageIcon from a null image in Options.  So, catch it and at
            //least give a meaningful message that indicates what node
            //is the culprit
            icon.paintIcon(r, g, iconX, iconY);
        } catch (NullPointerException npe) {
            Exceptions.attachMessage(npe,
                                     "Probably an ImageIcon with a null source image: " +
                                     icon + " - " + r.getText()); //NOI18N
            Exceptions.printStackTrace(npe);
        }

        txtX = iconX + icon.getIconWidth() + r.getIconTextGap();
    } else {
        //If there's no icon, paint the text where the icon would start
        txtX += ins.left;
    }

    String text = r.getText();

    if (text == null) {
        //No text, we're done
        return;
    }

    //Get the available horizontal pixels for text
    int txtW = (icon != null)
        ? (r.getWidth() - (ins.left + ins.right + icon.getIconWidth() + r.getIconTextGap() + r.getIndent()))
        : (r.getWidth() - (ins.left + ins.right + r.getIndent()));

    Color background = getBackgroundFor(r);
    Color foreground = ensureContrastingColor(getForegroundFor(r), background);

    if (r.isHtml()) {
        HtmlRenderer._renderHTML(text, 0, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true, background, r.isSelected());
    } else {
        HtmlRenderer.renderPlainString(text, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true);
    }
}
 
Example 18
Source File: CenterTranslateIcon.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static int getCenterX(Icon icon, int baseIconSize) {
	return (baseIconSize / 2) - (icon.getIconWidth() / 2);
}
 
Example 19
Source File: LabelRenderer.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public void setIcon(Icon icon) {
    this.icon = icon;
    iconWidth = icon == null ? 0 : icon.getIconWidth();
    iconHeight = icon == null ? 0 : icon.getIconHeight();
    resetPreferredSize(true, false); // Icon likely won't change height
}
 
Example 20
Source File: GtkViewTabDisplayerUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void paintTabContent(Graphics g, int index, String text, int x,
                               int y, int width, int height) {
    // substract lower border
    height--;
    FontMetrics fm = getTxtFontMetrics();
    // setting font already here to compute string width correctly
    g.setFont(getTxtFont());
    int txtWidth = width;
    if (isSelected(index)) {
        Component buttons = getControlButtons();
        if( null != buttons ) {
            Dimension buttonsSize = buttons.getPreferredSize();
            if( width < buttonsSize.width+ICON_X_PAD ) {
                buttons.setVisible( false );
            } else {
                buttons.setVisible( true );
                txtWidth = width - (buttonsSize.width + ICON_X_PAD + TXT_X_PAD);
                buttons.setLocation(x + txtWidth + TXT_X_PAD, y + (height - buttonsSize.height)/2 + (TXT_Y_PAD / 2));
            }
        }
    } else {
        txtWidth = width - 2 * TXT_X_PAD;
    }
    // draw bump (dragger)
    drawBump(g, index, x + 4, y + 6, BUMP_WIDTH, height - 8);
    
    boolean slidedOut = false;
    WinsysInfoForTabbedContainer winsysInfo = displayer.getContainerWinsysInfo();
    if( null != winsysInfo && winsysInfo.isSlidedOutContainer() )
        slidedOut = false;
    if( isTabBusy( index ) && !slidedOut ) {
        Icon busyIcon = BusyTabsSupport.getDefault().getBusyIcon( isSelected( index ) );
        txtWidth -= busyIcon.getIconWidth() - 3 - TXT_X_PAD;
        busyIcon.paintIcon( displayer, g, x+TXT_X_PAD, y+(height-busyIcon.getIconHeight())/2);
        x += busyIcon.getIconWidth() + 3;
    }
    // draw text in right color
    Color txtC = UIManager.getColor("textText"); //NOI18N
    HtmlRenderer.renderString(text, g, x + TXT_X_PAD, y + fm.getAscent()
        + TXT_Y_PAD,
        txtWidth, height, getTxtFont(),
        txtC,
        HtmlRenderer.STYLE_TRUNCATE, true);
}