javax.swing.Icon#paintIcon ( )源码实例Demo

下面列出了javax.swing.Icon#paintIcon ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Spark   文件: ImageCombiner.java
/**
 * 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;
    }
}
 
源代码2 项目: Logisim   文件: AddTool.java
@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);
	}
}
 
源代码3 项目: netbeans-mmd-plugin   文件: UiUtils.java
@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;
}
 
源代码4 项目: openjdk-8-source   文件: Test6657026.java
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");
        }
    }
}
 
源代码5 项目: jdk8u-jdk   文件: Test6657026.java
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");
        }
    }
}
 
源代码6 项目: sc2gears   文件: GuiUtils.java
/**
 * 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() );
		}
	};
}
 
源代码7 项目: jdk8u-dev-jdk   文件: Test6657026.java
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");
        }
    }
}
 
源代码8 项目: CodenameOne   文件: PropertySheetTable.java
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);
  }
}
 
源代码9 项目: Logisim   文件: ControlledBuffer.java
@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);
	}
}
 
源代码10 项目: snap-desktop   文件: ButtonOverlayControl.java
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;
    }
}
 
源代码11 项目: netbeans   文件: ActionsTest.java
/**
 * 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);
    }
}
 
源代码12 项目: ghidra   文件: ZoomedImagePainter.java
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;
}
 
源代码13 项目: pumpernickel   文件: ThumbnailLabelUI.java
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);
}
 
源代码14 项目: netbeans   文件: HtmlLabelUI.java
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
        );
    }
}
 
源代码15 项目: netbeans   文件: ETableHeader.java
/**
 * 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);
}
 
源代码16 项目: mars-sim   文件: VerticalLabelUI.java
@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 );
   }
 
源代码17 项目: pumpernickel   文件: DecoratedListUI.java
@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();
			}
		}
	}
}
 
源代码18 项目: netbeans   文件: TimelineIconPainter.java
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);
    }
}
 
源代码19 项目: pdfxtk   文件: VerticalLabelUI.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;
 
   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);
     }
   }
 }
 
源代码20 项目: pumpernickel   文件: GraphicCache.java
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());
		}
	}
}