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

The following examples show how to use javax.swing.Icon#paintIcon() . 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: ButtonOverlayControl.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private 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 3
Source File: AddTool.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paintIcon(ComponentDrawContext c, int x, int y) {
	FactoryDescription desc = description;
	if (desc != null && !desc.isFactoryLoaded()) {
		Icon icon = desc.getIcon();
		if (icon != null) {
			icon.paintIcon(c.getDestination(), c.getGraphics(), x + 2, y + 2);
			return;
		}
	}

	ComponentFactory source = getFactory();
	if (source != null) {
		AttributeSet base = getBaseAttributes();
		source.paintIcon(c, x, y, base);
	}
}
 
Example 4
Source File: UiUtils.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static Image iconToImage(@Nonnull Component context, @Nullable final Icon icon) {
  if (icon instanceof ImageIcon) {
    return ((ImageIcon) icon).getImage();
  }
  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;
}
 
Example 5
Source File: ControlledBuffer.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paintIcon(InstancePainter painter) {
	Graphics g = painter.getGraphics();
	Icon icon = isInverter ? ICON_INVERTER : ICON_BUFFER;
	if (icon != null) {
		icon.paintIcon(painter.getDestination(), g, 2, 2);
	} else {
		int x = isInverter ? 0 : 2;
		g.setColor(Color.BLACK);
		int[] xp = new int[] { x + 15, x + 1, x + 1, x + 15 };
		int[] yp = new int[] { 10, 3, 17, 10 };
		g.drawPolyline(xp, yp, 4);
		if (isInverter)
			g.drawOval(x + 13, 8, 4, 4);
		g.setColor(Value.FALSE_COLOR);
		g.drawLine(x + 8, 14, x + 8, 18);
	}
}
 
Example 6
Source File: PropertySheetTable.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y, int width,
    int height) {      
  if (!isProperty) {
    Color oldColor = g.getColor();      
    g.setColor(c.getBackground());
    g.fillRect(x, y, x + HOTSPOT_SIZE - 2, y + height);
    g.setColor(oldColor);
  }
  
  if (showToggle) {
    Icon drawIcon = (toggleState ? expandedIcon : collapsedIcon);
    drawIcon.paintIcon(c, g,
      x + indentWidth + (HOTSPOT_SIZE - 2 - drawIcon.getIconWidth()) / 2,
      y + (height - drawIcon.getIconHeight()) / 2);
  }
}
 
Example 7
Source File: Test6657026.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void test() {
    MyGraphics mg = new MyGraphics();
    Icon icon = bumps;
    icon.paintIcon(mg.component, mg, 0, 0);
    if (mg.image != null) {
        boolean failed = true;
        int value = mg.image.getRGB(0, 0);
        for (int x = 0; x < mg.image.getWidth(); x++) {
            for (int y = 0; y < mg.image.getHeight(); y++) {
                int current = mg.image.getRGB(x, y);
                if (current != value) {
                    mg.image.setRGB(x, y, value);
                    failed = false;
                }

            }
        }
        if (failed) {
            throw new Error("shared metal bumps");
        }
    }
}
 
Example 8
Source File: Test6657026.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void test() {
    MyGraphics mg = new MyGraphics();
    Icon icon = bumps;
    icon.paintIcon(mg.component, mg, 0, 0);
    if (mg.image != null) {
        boolean failed = true;
        int value = mg.image.getRGB(0, 0);
        for (int x = 0; x < mg.image.getWidth(); x++) {
            for (int y = 0; y < mg.image.getHeight(); y++) {
                int current = mg.image.getRGB(x, y);
                if (current != value) {
                    mg.image.setRGB(x, y, value);
                    failed = false;
                }

            }
        }
        if (failed) {
            throw new Error("shared metal bumps");
        }
    }
}
 
Example 9
Source File: Test6657026.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void test() {
    MyGraphics mg = new MyGraphics();
    Icon icon = bumps;
    icon.paintIcon(mg.component, mg, 0, 0);
    if (mg.image != null) {
        boolean failed = true;
        int value = mg.image.getRGB(0, 0);
        for (int x = 0; x < mg.image.getWidth(); x++) {
            for (int y = 0; y < mg.image.getHeight(); y++) {
                int current = mg.image.getRGB(x, y);
                if (current != value) {
                    mg.image.setRGB(x, y, value);
                    failed = false;
                }

            }
        }
        if (failed) {
            throw new Error("shared metal bumps");
        }
    }
}
 
Example 10
Source File: GuiUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Concatenates 2 icons next to each other (in 1 row).
 * @param icon1 first icon to be concatenated (this will be on the left side)
 * @param icon2 second icon to be concatenated (this will be on the right side)
 * @return an icon that is the result of the concatenation of the 2 specified icons
 */
public static Icon concatenateIcons( final Icon icon1, final Icon icon2 ) {
	return new Icon() {
		@Override
		public void paintIcon( final Component c, final Graphics g, final int x, final int y ) {
			icon1.paintIcon( c, g, x, y );
			icon2.paintIcon( c, g, x + icon1.getIconWidth(), y );
		}
		@Override
		public int getIconWidth() {
			return icon1.getIconWidth() + icon2.getIconWidth();
		}
		@Override
		public int getIconHeight() {
			return Math.max( icon1.getIconHeight(), icon2.getIconHeight() );
		}
	};
}
 
Example 11
Source File: ThumbnailLabelUI.java    From pumpernickel with MIT License 5 votes vote down vote up
protected void paintIcon(Graphics2D g, JLabel label, boolean isSelected,
		boolean isIndicated) {
	Icon icon = label.getIcon();
	if (icon == null)
		return;

	icon.paintIcon(label, g,
			iconRect.x + iconRect.width / 2 - icon.getIconWidth() / 2,
			iconRect.y + iconRect.height / 2 - icon.getIconHeight() / 2);
}
 
Example 12
Source File: ETableHeader.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 13
Source File: HtmlLabelUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void paintIconAndTextCentered(Graphics g, HtmlRendererImpl r) {
    Insets ins = r.getInsets();
    Icon ic = r.getIcon();
    int w = r.getWidth() - (ins.left + ins.right);
    int txtX = ins.left;
    int txtY = 0;

    if ((ic != null) && (ic.getIconWidth() > 0) && (ic.getIconHeight() > 0)) {
        int iconx = (w > ic.getIconWidth()) ? ((w / 2) - (ic.getIconWidth() / 2)) : txtX;
        int icony = 0;
        ic.paintIcon(r, g, iconx, icony);
        txtY += (ic.getIconHeight() + r.getIconTextGap());
    }

    int txtW = r.getPreferredSize().width;
    txtX = (txtW < r.getWidth()) ? ((r.getWidth() / 2) - (txtW / 2)) : 0;

    int txtH = r.getHeight() - txtY;

    Font f = font(r);
    g.setFont(f);

    FontMetrics fm = g.getFontMetrics(f);
    txtY += fm.getMaxAscent();

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

    if (r.isHtml()) {
        HtmlRenderer._renderHTML(
            r.getText(), 0, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true, background, r.isSelected()
        );
    } else {
        HtmlRenderer.renderString(
            r.getText(), g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true
        );
    }
}
 
Example 14
Source File: ZoomedImagePainter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static Image createIconImage( Icon icon ) {
    BufferedImage buffImage = new BufferedImage( icon.getIconWidth(), 
        icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB );
    Graphics graphics = buffImage.getGraphics();
    icon.paintIcon( null, graphics, 0, 0 );
    graphics.dispose();
    return buffImage;
}
 
Example 15
Source File: ActionsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks colors on coordinates X,Y of the icon and compares them
 * to expectedResult.
 */
private void checkIfIconOk(Icon icon, Component c, int pixelX, int pixelY, int[] expectedResult, String nameOfIcon) {
    BufferedImage bufImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    icon.paintIcon(c, bufImg.getGraphics(), 0, 0);
    int[] res = bufImg.getData().getPixel(pixelX, pixelY, (int[])null);
    log("Icon height is " + icon.getIconHeight());
    log("Icon width is " + icon.getIconWidth());
    for (int i = 0; i < res.length; i++) {
        // Huh, Ugly hack. the sparc returns a fuzzy values +/- 1 unit e.g. 254 for Black instead of 255 as other OSs do
        // this hack doesn't broken the functionality which should testing
        assertTrue(nameOfIcon + ": Color of the ["+pixelX+","+pixelY+"] pixel is " + res[i] + ", expected was " + expectedResult[i], Math.abs(res[i] - expectedResult[i]) < 10);
    }
}
 
Example 16
Source File: VerticalLabelUI.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
@Override
   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;
       }

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

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

   	// Use inverted height &amp; 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 );
   }
 
Example 17
Source File: VerticalLabelUI.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
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;
 
   FontMetrics  fm = g.getFontMetrics();
   paintViewInsets = c.getInsets(paintViewInsets);
   
   paintViewR.x      = paintViewInsets.top;
   paintViewR.y      = paintViewInsets.left;
   paintViewR.width  = c.getHeight() - (paintViewInsets.top  + paintViewInsets.bottom);
   paintViewR.height = c.getWidth()  - (paintViewInsets.left + paintViewInsets.right);
   
   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, g2, paintIconR.x, paintIconR.y);
   }
   
   if (text != null) {
     View v = (View) c.getClientProperty(BasicHTML.propertyKey);
     if (v != null) {
v.paint(g2, paintTextR);
     } else {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();

if (label.isEnabled())
  paintEnabledText(label, g2, clippedText, textX, textY);
else
  paintDisabledText(label, g2, clippedText, textX, textY);
     }
   }
 }
 
Example 18
Source File: GraphicCache.java    From pumpernickel with MIT License 4 votes vote down vote up
public void run() {
	while (true) {
		IOLocation loc = null;
		synchronized (requestIconList) {
			if (requestIconList.size() == 0)
				return;

			loc = requestIconList.remove(requestIconList.size() - 1);
		}
		Cancellable myCancellable = cancellable;
		Icon icon = null;
		try {
			icon = loc.isDirectory() ? IOLocation.FOLDER_ICON
					: IOLocation.FILE_ICON; // loc.getIcon(myCancellable);
		} catch (Throwable e) {
			handleUncaughtException(e);
		}
		if (icon != null) {
			/**
			 * Unfortunately there's more. Macs would still often jam up
			 * in the event dispatch thread with this stack trace:
			 * AWT-EventQueue-0 (id = 13)
			 * apple.awt.CImage.getNativeFileSystemIconFor(Native
			 * Method) apple.awt.CImage.access$300(CImage.java:12)
			 * apple.
			 * awt.CImage$Creator.createImageOfFile(CImage.java:90)
			 * com.apple
			 * .laf.AquaIcon$FileIcon.createImage(AquaIcon.java:230)
			 * com.
			 * apple.laf.AquaIcon$CachingScalingIcon.getOptimizedImage
			 * (AquaIcon.java:133)
			 * com.apple.laf.AquaIcon$CachingScalingIcon
			 * .getImage(AquaIcon.java:126)
			 * com.apple.laf.AquaIcon$CachingScalingIcon
			 * .paintIcon(AquaIcon.java:168)
			 * 
			 * I'm interpreting this to mean: the Icon object exists,
			 * but the underlying mechanism still isn't ready to paint
			 * it. Which completely defeats the purpose of creating this
			 * object in a separate thread. So let's try to force the
			 * AquaIcon to prep itself:
			 */
			Graphics2D g = scratchImage.createGraphics();
			icon.paintIcon(null, g, 0, 0);
			g.dispose();

			icons.put(loc, icon);
			firePropertyChangeListener(ICON_PROPERTY, loc, null, icon);
		} else if (myCancellable.isCancelled() == false) {
			noIcons.add(loc.toString());
		}
	}
}
 
Example 19
Source File: TimelineIconPainter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void paint(XYItem item, List<ItemSelection> highlighted,
                     List<ItemSelection> selected, Graphics2D g,
                     Rectangle dirtyArea, SynchronousXYChartContext
                     context) {

    if (context.getViewWidth() == 0) return;
    
    int[][] visibleBounds = context.getVisibleBounds(dirtyArea);

    int firstFirst = visibleBounds[0][0];
    int firstIndex = firstFirst;
    if (firstIndex == -1) firstIndex = visibleBounds[0][1];
    if (firstIndex == -1) return;

    int minX = dirtyArea.x - ICON_EXTENT;
    while (context.getViewX(item.getXValue(firstIndex)) > minX && firstIndex > 0) firstIndex--;

    int endIndex = item.getValuesCount() - 1;
    int lastFirst = visibleBounds[1][0];
    int lastIndex = lastFirst;
    if (lastIndex == -1) lastIndex = visibleBounds[1][1];
    if (lastIndex == -1) lastIndex = endIndex;

    int maxX = dirtyArea.x + dirtyArea.width + ICON_EXTENT;
    while (context.getViewX(item.getXValue(lastIndex)) < maxX && lastIndex < endIndex) lastIndex++;

    g.setColor(color);

    for (int index = firstIndex; index <= lastIndex; index++) {
        long dataY = item.getYValue(index);
        if (dataY == 0) continue;

        long dataX = item.getXValue(index);
        int  viewX = Utils.checkedInt(context.getViewX(dataX));
        Icon icon = snapshot.getLogInfoForValue(dataY).getIcon();
        if (icon == null) icon = ICON;
        int iconWidth = icon.getIconWidth();
        int iconHeight = icon.getIconHeight();
        icon.paintIcon(null, g, viewX - iconWidth / 2, (context.getViewportHeight() - iconHeight) / 2);
    }
}
 
Example 20
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();
			}
		}
	}
}