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

The following examples show how to use javax.swing.Icon#getIconHeight() . 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: ChartPainter.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Draws a statistics bar.
 * @param toolTipList  reference to the list of tool tips (where to add new tool tip)
 * @param icon         icon of the bar
 * @param entity       entity to be drawn (will be used to create a tool tip text)
 * @param x            x coordinate of the bar
 * @param maxBarHeight maximum allowed height of the bar
 * @param count        count to calculate and draw bar for
 * @param maxCount     maximum count belonging to the maximum bar height
 * @param y2           bottom y coordinate of the bar
 * @param label        optional label, if provided this will be displayed on top of the bar, if <code>null</code>, the count will be displayed
 */
private void drawStatBar( final List< Pair< Rectangle, String > > toolTipList, final Icon icon, final Object entity, final IntHolder x, final int maxBarHeight, final int count, final int maxCount, final int y2, String label ) {
	final int barWidth = icon.getIconWidth();
	icon.paintIcon( params.chartCanvas, g2, x.value, y2 - icon.getIconHeight() + 1 );
	
	final int bottomY = y2 - icon.getIconHeight();
	final int height_ = maxBarHeight * count / maxCount;
	// Passing 0 as the height for the fill3dRect results in a 2 pixel height rectangle :S
	final int height = height_ < 1 ? 1 : height_;
	
	g2.fill3DRect( x.value, bottomY - height, barWidth, height, true );
	
	final Color storedColor = g2.getColor();
	g2.setColor( COLOR_PLAYER_DEFAULT );
	if ( label == null )
		label = Integer.toString( count );
	g2.drawString( label, x.value + ( ( barWidth - g2.getFontMetrics().stringWidth( label ) ) >> 1 ), bottomY - height - 1 );
	g2.setColor( storedColor );
	
	toolTipList.add( new Pair< Rectangle, String >( new Rectangle( x.value, bottomY - height, barWidth, y2 - bottomY + height ), entity.toString() ) );
	
	x.value += barWidth;
}
 
Example 3
Source File: Icons.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
public static void paintRotated(Graphics g, int x, int y, Direction dir, Icon icon, Component dest) {
	if (!(g instanceof Graphics2D) || dir == Direction.EAST) {
		icon.paintIcon(dest, g, x, y);
		return;
	}

	Graphics2D g2 = (Graphics2D) g.create();
	double cx = x + icon.getIconWidth() / 2.0;
	double cy = y + icon.getIconHeight() / 2.0;
	if (dir == Direction.WEST) {
		g2.rotate(Math.PI, cx, cy);
	} else if (dir == Direction.NORTH) {
		g2.rotate(-Math.PI / 2.0, cx, cy);
	} else if (dir == Direction.SOUTH) {
		g2.rotate(Math.PI / 2.0, cx, cy);
	} else {
		g2.translate(-x, -y);
	}
	icon.paintIcon(dest, g2, x, y);
	g2.dispose();
}
 
Example 4
Source File: Canvas.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
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 5
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 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: ETableColumn.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method merging 2 icons.
 */
private static Icon mergeIcons(Icon icon1, Icon icon2, int x, int y, Component c) {
    int w = 0, h = 0;
    if (icon1 != null) {
        w = icon1.getIconWidth();
        h = icon1.getIconHeight();
    }
    if (icon2 != null) {
        w = icon2.getIconWidth()  + x > w ? icon2.getIconWidth()   + x : w;
        h = icon2.getIconHeight() + y > h ? icon2.getIconHeight()  + y : h;
    }
    if (w < 1) w = 16;
    if (h < 1) h = 16;
    
    java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ().
                                      getDefaultScreenDevice ().getDefaultConfiguration ().
                                      getColorModel (java.awt.Transparency.BITMASK);
    java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model,
         model.createCompatibleWritableRaster (w, h), model.isAlphaPremultiplied (), null);
    
    java.awt.Graphics g = buffImage.createGraphics ();
    if (icon1 != null) {
        icon1.paintIcon(c, g, 0, 0);
    }
    if (icon2 != null) {
        icon2.paintIcon(c, g, x, y);
    }
    g.dispose();
    
    return new ImageIcon(buffImage);
}
 
Example 8
Source File: TerminalSettingsAction.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 9
Source File: FilteredIcon.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static Icon create(RGBImageFilter filter, Icon delegate) {
    final int width = delegate.getIconWidth();
    final int height = delegate.getIconHeight();
    if (width < 0 || height < 0) {
        /* This case was once observed in NETBEANS-3671. I'm not sure where the offending Icon
        came from. Log some more information for future debugging, and fall back gracefully. */
        Object url = ImageUtilities.icon2Image(delegate).getProperty("url", null);
        LOG.log(Level.WARNING,
                "NETBEANS-3671: FilteredIcon.create got {0} of invalid dimensions {1}x{2} with URL={3}",
                new Object[] { delegate.getClass().getName(), width, height, url });
        return delegate;
    }
    return new FilteredIcon(filter, delegate);
}
 
Example 10
Source File: RotatedIcon.java    From pumpernickel with MIT License 5 votes vote down vote up
public RotatedIcon(Icon icon, float radians) {
	this.rotation = radians;
	this.icon = icon;

	int h = icon.getIconHeight();
	int w = icon.getIconWidth();
	double k = Math.sqrt(h * h + w * w);
	size = (int) Math.ceil(k);
}
 
Example 11
Source File: FlashingIcon.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/** 
 * Creates a new instance of FlashingIcon 
 *
 * @param icon The icon that will be flashing (blinking)
 */
protected FlashingIcon( Icon icon ) {
    this.icon = icon;
    Dimension d = new Dimension( icon.getIconWidth(), icon.getIconHeight() );
    setMinimumSize( d );
    setMaximumSize( d );
    setPreferredSize( d );
    setVisible (false);
    
    addMouseListener( this );
}
 
Example 12
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 13
Source File: SystemActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Image icon2Image(Icon ico) {
    int w = ico.getIconWidth();
    int h = ico.getIconHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    ico.paintIcon(new JButton(), img.getGraphics(), 0, 0);
    return img;
}
 
Example 14
Source File: UnitLabelMapLayer.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the label draw position on map panel.
 * 
 * @param unitPosition the unit display position.
 * @param unitIcon     unit's map image icon.
 * @return draw position for unit label.
 */
private IntPoint getLabelLocation(IntPoint unitPosition, Icon unitIcon) {

	int unitX = unitPosition.getiX();
	int unitY = unitPosition.getiY();
	int iconHeight = unitIcon.getIconHeight();
	int iconWidth = unitIcon.getIconWidth();

	return new IntPoint(unitX + (iconWidth / 2) + LABEL_HORIZONTAL_OFFSET, unitY + (iconHeight / 2));
}
 
Example 15
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
protected void drawCentered(Component c, Graphics graphics, Icon icon,
                        int x, int y) {
    int w = icon.getIconWidth();
    int h = icon.getIconHeight();
    image[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    super.drawCentered(c, image[0].getGraphics(), icon, w / 2, h / 2);
}
 
Example 16
Source File: FilterOptions.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Icon getFilterStateIcon() {
	Icon icon = getIcon(textFilterStrategy);
	if (inverted) {
		int width = icon.getIconWidth();
		int height = icon.getIconHeight();
		int notWidth = NOT_ICON.getIconWidth();
		int notHeight = NOT_ICON.getIconHeight();
		icon = new MultiIcon(icon,
			new TranslateIcon(NOT_ICON, width - notWidth / 2, height - notHeight / 2));
	}
	return icon;
}
 
Example 17
Source File: Swing.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
public static BufferedImage iconToImage(Icon icon) {
  BufferedImage result = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
  icon.paintIcon(emptyCmp, result.getGraphics(), 0, 0);
  return result;
}
 
Example 18
Source File: EditableDisplayerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testPropertyMarking() throws Exception{
    if (!canRun) return;
    
    if (!checkGraphicsEnvironment()) {
        System.err.println("  Cannot run this test in a < 16 bit graphics environment");
    }
    custRen.setUpdatePolicy(custRen.UPDATE_ON_CONFIRMATION);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                throwMe = null;
                custRen.getProperty().setValue("Value");
                custRen.refresh();
            } catch (Exception e) {
                throwMe = e;
            }
        }
    });
    
    if (throwMe != null) {
        Exception exc = throwMe;
        throwMe = null;
        throw exc;
    }
    
    
    requestFocus(custRen);
    
    typeKey(custRen, KeyEvent.VK_S);
    typeKey(custRen, KeyEvent.VK_N);
    typeKey(custRen, KeyEvent.VK_O);
    typeKey(custRen, KeyEvent.VK_R);
    typeKey(custRen, KeyEvent.VK_K);
    typeKey(custRen, KeyEvent.VK_E);
    typeKey(custRen, KeyEvent.VK_L);
    
    //The property marking image
    Image i = ImageUtilities.loadImage("org/openide/resources/propertysheet/invalid.gif");
    ImageIcon icon = new ImageIcon(i);
    int yOffset = (custRen.getHeight() / 2) - (icon.getIconHeight()/2);
    
    //        assertPixelFromImage(i, custRen, 7, 7, 7, yOffset + 7);
    assertImageMatch("Error icon should be painted for invalid value", i, custRen, 0, yOffset);
    
    requestFocus(custRen);
    
    //        SLEEP_LENGTH=1000;
    sleep();
    typeKey(custRen, KeyEvent.VK_M);
    typeKey(custRen, KeyEvent.VK_R);
    typeKey(custRen, KeyEvent.VK_F);
    typeKey(custRen, KeyEvent.VK_ENTER);
    pressKey(custRen, KeyEvent.VK_ENTER);
    pressKey(custRen, KeyEvent.VK_ENTER);
    custRen.commit();
    sleep();
    sleep();
    
    Icon icon2 = new ValueIcon();
    int yOffset2 = (custRen.getHeight() / 2) - (icon2.getIconHeight()/2);
    
    assertPixel("Supplied value icon should be drawn on panel, not the error marking icon, after committing a valid value.",
            custRen, Color.BLUE, icon2.getIconWidth() / 2, (icon2.getIconHeight() / 2) + yOffset2);
    
    requestFocus(custRen);
    
    typeKey(custRen, KeyEvent.VK_V);
    typeKey(custRen, KeyEvent.VK_A);
    typeKey(custRen, KeyEvent.VK_L);
    typeKey(custRen, KeyEvent.VK_U);
    typeKey(custRen, KeyEvent.VK_E);
    custRen.setEnteredValue("VALUE");
    pressKey(custRen, KeyEvent.VK_ENTER);
    custRen.commit();
    sleep();
    sleep();
    sleep();
    custRen.paintImmediately(0,0,custRen.getWidth(),custRen.getHeight());
    assertImageMatch("After reentering an invalid value, the icon should change back to the error icon", i, custRen, 0, yOffset);
}
 
Example 19
Source File: DefaultLoginCallback.java    From SmartIM with Apache License 2.0 4 votes vote down vote up
/**
 * Create the frame.
 */
@SuppressWarnings("serial")
public QRCodeFrame(final String filePath, final String title) {
    setBackground(Color.WHITE);
    this.setResizable(true);
    this.setTitle(title);
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    // this.setBounds(100, 100, 500, 400);
    this.contentPane = new JPanel();
    // contentPane.setBackground(new Color(102, 153, 255));
    this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    this.setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout());
    
    ImageIcon temp = new ImageIcon(filePath, filePath);
    if (temp.getImage() != null) {
        temp.getImage().flush();
    }
    
    final Icon icon = new ScaleIcon(new ImageIcon(filePath, filePath));
    
    JPanel qrcodePanel = new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            // 图片随窗体大小而变化
            int x = 0, y = 0;
            if (getWidth() > 0 && getWidth() > icon.getIconWidth()) {
                x = (getWidth() - icon.getIconWidth()) / 2;
            }
            if (getHeight() > 0 && getHeight() > icon.getIconHeight()) {
                y = (getHeight() - icon.getIconHeight()) / 2;
            }
            // g.drawImage(icon.getImage(), x, y, icon.getIconWidth(),
            // icon.getIconHeight(), this);
            icon.paintIcon(this, g, x, y);
        }
    };
    qrcodePanel.setToolTipText(filePath);
    qrcodePanel.setPreferredSize(new Dimension(192, 192));
    qrcodePanel.setOpaque(true);
    // qrcodePanel.setBackground(Color.WHITE);
    
    tfTip = new JLabel(tip == null ? title : tip);
    tfTip.setFont(new Font("微软雅黑", Font.PLAIN, 18));
    tfTip.setHorizontalAlignment(SwingConstants.CENTER);
    tfTip.setOpaque(true);
    tfTip.setBackground(new Color(102, 153, 255));
    
    tfError = new JTextArea();
    tfError.setEditable(false);
    tfError.setBorder(new EmptyBorder(0, 0, 0, 0));
    tfError.setCaretColor(Color.RED);
    tfError.setForeground(Color.RED);
    tfError.setLineWrap(true);
    
    contentPane.add(tfTip, BorderLayout.NORTH);
    contentPane.add(qrcodePanel, BorderLayout.CENTER);
    contentPane.add(tfError, BorderLayout.SOUTH);
    
    // this.setPreferredSize(new Dimension(400, 400));
    this.setMaximumSize(new Dimension(600, 600));
    
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    pack();
}
 
Example 20
Source File: BadgedIcon.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public BadgedIcon(Icon baseIcon, boolean enabled) {
	this(baseIcon, enabled, baseIcon.getIconWidth(), baseIcon.getIconHeight());
}