下面列出了javax.swing.ImageIcon#getIconWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ArrayImageSource getImageSource( Image image ) throws IOException
{
ImageIcon imageIcon = new ImageIcon( image );
int w = imageIcon.getIconWidth( );
int h = imageIcon.getIconHeight( );
int[] pixels = new int[w * h];
try
{
PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, pixels, 0, w );
pg.grabPixels( );
if ( ( pg.getStatus( ) & ImageObserver.ABORT ) != 0 )
{
throw new IOException( "failed to load image contents" );
}
}
catch ( InterruptedException e )
{
throw new IOException( "image load interrupted" );
}
ArrayImageSource imageSource = new ArrayImageSource( w, h, pixels );
return imageSource;
}
/**
* Returns a scaled down image if the height or width is smaller than the
* image size.
*
* @param icon
* the image icon.
* @param newHeight
* the preferred height.
* @param newWidth
* the preferred width.
* @return the icon.
*/
public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight,
int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
if (height > newHeight) {
height = newHeight;
}
if (width > newWidth) {
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
return new ImageIcon(img);
}
/**
* Paints the given overlay icon over the base icon. This can be used for example to cross out
* icons. Provided the overlay icon has a transparent background, the base icon will still be
* visible.
*
* @param baseIcon
* @param overlayIcon
* @return
*/
public static ImageIcon createOverlayIcon(final ImageIcon baseIcon, final ImageIcon overlayIcon) {
if (baseIcon == null) {
throw new IllegalArgumentException("baseIcon must not be null!");
}
if (overlayIcon == null) {
throw new IllegalArgumentException("overlayIcon must not be null!");
}
BufferedImage bufferedImg = new BufferedImage(baseIcon.getIconWidth(), baseIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) bufferedImg.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform identityAT = new AffineTransform();
g2.drawImage(baseIcon.getImage(), identityAT, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
g2.drawImage(overlayIcon.getImage(), identityAT, null);
g2.dispose();
return new ImageIcon(bufferedImg);
}
/**
* Scale an image to fit in a square of the given size, preserving aspect
* ratio.
*
* @param icon
* @param newSize
* @return
*/
public static ImageIcon fitToSquare(ImageIcon icon, int newSize) {
if (newSize <= 0) {
return icon;
}
final int oldWidth = icon.getIconWidth();
final int oldHeight = icon.getIconHeight();
int newWidth;
int newHeight;
if (oldHeight >= oldWidth) {
newWidth = (int) ((float) oldWidth * newSize / oldHeight);
newHeight = newSize;
} else {
newWidth = newSize;
newHeight = (int) ((float) oldHeight * newSize / oldWidth);
}
final Image img = icon.getImage().getScaledInstance(newWidth,
newHeight, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/** Samples a trivial number of pixels from an image and compares them with
*pixels from a component to determine if the image is painted on the
* component at the exact position specified */
private void assertImageMatch(String msg, Image i, JComponent comp, int xpos, int ypos) throws Exception {
ImageIcon ic = new ImageIcon(i);
int width = ic.getIconWidth();
int height = ic.getIconHeight();
for (int x=2; x < 5; x++) {
for (int y=2; y < 5; y++) {
int posX = width / x;
int posY = height / y;
System.err.println(" Check " + posX + "," + posY);
assertPixelFromImage(msg, i, comp, posX, posY, xpos + posX, ypos + posY);
}
}
}
public static void drawCallout(Graphics2D g, ImageIcon icon, String label, int x, int y) {
g = (Graphics2D)g.create();
g.setStroke(new BasicStroke(1.0f));
g.setFont(UI.getSmallFont());
Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(label, g);
int padding = 3;
int iconWidth = icon == null ? 0 : icon.getIconWidth();
int iconHeight = icon == null ? 0 : icon.getIconHeight();
int maxContentHeight = (int) Math.max(iconHeight, stringBounds.getHeight());
int calloutHeight = (maxContentHeight + padding * 2) - 1;
int calloutWidth = (int) (iconWidth + stringBounds.getWidth() + padding * 3);
int iconX = x + (icon == null ? 0 : padding);
int iconY = y + padding;
int stringX = iconX + iconWidth + padding;
int stringBaseline = y + (calloutHeight - padding - (calloutHeight - g.getFontMetrics().getHeight()) / 2);
g.setColor(UI.getPanelDarkColor());
g.fillRoundRect(x, y, calloutWidth, calloutHeight, UI.getRoundedness(), UI.getRoundedness());
g.setColor(UI.getControlBorderColor());
g.drawRoundRect(x, y, calloutWidth, calloutHeight, UI.getRoundedness(), UI.getRoundedness());
if(icon != null) icon.paintIcon(null, g, iconX, iconY);
g.setColor(UI.getPanelTextColor());
g.drawString(label, stringX, stringBaseline);
}
public static ImageIcon combine(ImageIcon icon1, ImageIcon icon2) {
Image img1 = icon1.getImage();
Image img2 = icon2.getImage();
int w = icon1.getIconWidth();
int h = icon1.getIconHeight();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img1, 0, 0, null);
g2.drawImage(img2, 0, 0, null);
g2.dispose();
return new ImageIcon(image);
}
public ImageIcon combineAccess(ImageIcon icon1, ImageIcon icon2, boolean right) {
Image img1 = icon1.getImage();
Image img2 = icon2.getImage();
int w = icon1.getIconWidth();
int h = icon1.getIconHeight();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img1, 0, 0, null);
g2.drawImage(img2, right ? w / 4 : w / -4, h / -4, null);
g2.dispose();
return new ImageIcon(image);
}
/**
* Constructs a new field for displaying an image.
* @param icon the image icon to display
* @param metrics the font metrics
* @param startX the starting x coordinate of the field.
* @param startY the starting y coordinate of the field.
* @param width the width of the field.
* @param center flag to center the image in the field.
*/
public SimpleImageField(ImageIcon icon, FontMetrics metrics, int startX, int startY, int width,
boolean center) {
this.heightAbove = metrics.getMaxAscent() + metrics.getLeading();
this.height = heightAbove + metrics.getMaxDescent();
this.icon = icon;
this.metrics = metrics;
this.startX = startX;
this.width = width;
this.center = center;
// The height is initially set to the font height.
// If the font height is less than the icon height and the provided width
// is the less than the icon width, then scale the height relative to the
// width. Otherwise, use the icon height.
//
if (icon != null) {
if (this.height < icon.getIconHeight()) {
if (this.width < icon.getIconWidth()) {
this.height = (width * icon.getIconHeight()) / icon.getIconWidth();
}
else {
this.height = icon.getIconHeight();
}
}
}
}
/**
* Adds the given icon as an overlay to the base icon, to the lower-right,
* scaled to the given width and height
*
* @param icon the icon
* @param w the desired width
* @param h the desired height
* @return this builder
*/
public MultiIconBuilder addLowerRightIcon(Icon icon, int w, int h) {
ImageIcon scaled = ResourceManager.getScaledIcon(icon, w, h);
int x = multiIcon.getIconWidth() - scaled.getIconWidth();
int y = multiIcon.getIconHeight() - scaled.getIconHeight();
TranslateIcon txIcon = new TranslateIcon(scaled, x, y);
multiIcon.addIcon(txIcon);
return this;
}
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"));
}
private void paintCorner(Component c, Graphics g, int x, int y,
ImageIcon icon) {
int tileW = icon.getIconWidth();
int tileH = icon.getIconHeight();
g = g.create(x, y, x+tileW, y+tileH);
icon.paintIcon(c,g,0,0);
g.dispose();
}
private static Cursor createRangeFinderCursor(ImageIcon cursorIcon) {
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
final String cursorName = "rangeFinder";
// this is necessary because on some systems the cursor is scaled but not the 'hot spot'
Dimension bestCursorSize = defaultToolkit.getBestCursorSize(cursorIcon.getIconWidth(), cursorIcon.getIconHeight());
Point hotSpot = new Point((7 * bestCursorSize.width) / cursorIcon.getIconWidth(),
(7 * bestCursorSize.height) / cursorIcon.getIconHeight());
return defaultToolkit.createCustomCursor(cursorIcon.getImage(), hotSpot, cursorName);
}
@Override
public Shape render(Building b, Graphics2D g, ScreenTransform t) {
ImageIcon icon = null;
switch (b.getStandardURN()) {
case REFUGE:
icon = REFUGE;
break;
case FIRE_STATION:
icon = FIRE_STATION;
break;
case AMBULANCE_CENTRE:
icon = AMBULANCE_CENTRE;
break;
case POLICE_OFFICE:
icon = POLICE_OFFICE;
break;
default:
break;
}
if (icon != null) {
// Draw an icon over the centre of the building
int x = t.xToScreen(b.getX()) - (icon.getIconWidth() / 2);
int y = t.yToScreen(b.getY()) - (icon.getIconHeight() / 2);
icon.paintIcon(null, g, x, y);
}
return null;
}
/**
* Rescales an icon.
* @param src the original icon.
* @param newMinSize the minimum size of the new icon. The width and height of
* the returned icon will be at least the width and height
* respectively of newMinSize.
* @param an ImageObserver.
* @return the rescaled icon.
*/
public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer) {
double widthRatio = newMinSize.getWidth() / (double) src.getIconWidth();
double heightRatio = newMinSize.getHeight() / (double) src.getIconHeight();
double scale = widthRatio > heightRatio ? widthRatio : heightRatio;
int w = (int) Math.round(scale * src.getIconWidth());
int h = (int) Math.round(scale * src.getIconHeight());
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = dst.createGraphics();
g2.drawImage(src.getImage(), 0, 0, w, h, observer);
g2.dispose();
return new ImageIcon(dst);
}
/**
* Fonte: https://stackoverflow.com/questions/21382966/colorize-a-picture-in-java MODIFICADO
*
* @param image
* @param color
* @return
*/
public static BufferedImage dye(ImageIcon image, Color color) {
int w = image.getIconWidth();
int h = image.getIconHeight();
BufferedImage dyed = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dyed.createGraphics();
g.drawImage(image.getImage(), 0, 0, null);
g.setComposite(AlphaComposite.SrcAtop);
g.setColor(color);
g.fillRect(0, 0, w, h);
g.dispose();
return dyed;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D theGraphics = (Graphics2D) g;
Dimension theSize = getSize();
FontMetrics theMetrics = getFontMetrics(getFont());
theGraphics.setColor(Color.blue);
theGraphics.drawRect(10, 10, theSize.width - 10, theSize.height - 10);
theGraphics.drawRect(10, 10, theSize.width - 10, 10 + theMetrics.getAscent());
GradientPaint thePaint = new GradientPaint(0, 0, Color.blue, theSize.width - 35, theSize.height,
Color.black, false);
theGraphics.setPaint(thePaint);
theGraphics.fillRect(11, 11, theSize.width - 10 - 1, 10 + theMetrics.getAscent() - 1);
thePaint = new GradientPaint(0, 0, new Color(90, 90, 90), theSize.width - 35, theSize.height,
Color.black, false);
theGraphics.setPaint(thePaint);
theGraphics.fillRect(11, 19 + theMetrics.getAscent(), theSize.width - 10 - 1, theSize.height - 32);
theGraphics.setColor(Color.white);
theGraphics.drawString(table.getName(), 15, 10 + theMetrics.getAscent());
int y = 18 + theMetrics.getAscent();
for (Attribute<Table> theAttriute : table.getAttributes()) {
g.setColor(Color.white);
boolean theInclude = true;
if (!fullMode) {
theInclude = theAttriute.isForeignKey() || !theAttriute.isNullable();
}
if (theInclude) {
String theText = theAttriute.getName();
if (fullMode) {
theText += ":";
theText += theAttriute.getLogicalDeclaration();
if (theAttriute.isNullable()) {
theGraphics.drawString("N", theSize.width - 10, y + theMetrics.getAscent());
}
}
if (theAttriute.isForeignKey()) {
g.setColor(Color.green);
}
if (theAttriute.isPrimaryKey()) {
// Primarx key has underline
AttributedString as = new AttributedString(theText);
as.addAttribute(TextAttribute.FONT, theGraphics.getFont());
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0,
theText.length());
theGraphics.drawString(as.getIterator(), 15, y + theMetrics.getAscent());
} else {
theGraphics.drawString(theText, 15, y + theMetrics.getAscent());
}
y += theMetrics.getAscent();
}
if (showSelfReference) {
ImageIcon theIcon = IconFactory.getSelfReferenceIcon();
int xp = theSize.width - theIcon.getIconWidth() - 4;
int yp = 14;
theIcon.paintIcon(this, theGraphics, xp, yp);
}
}
if (table.getIndexes().size() > 0 && fullMode) {
boolean lineDrawn = false;
for (Index theIndex : table.getIndexes()) {
if (theIndex.getIndexType() != IndexType.PRIMARYKEY) {
if (!lineDrawn) {
y += 3;
theGraphics.setColor(Color.blue);
theGraphics.drawLine(10, y, theSize.width, y);
lineDrawn = true;
}
String theName = theIndex.getName();
theGraphics.setColor(Color.white);
theGraphics.drawString(theName, 15, y + theMetrics.getAscent());
y += theMetrics.getAscent();
for (IndexExpression theExpression : theIndex.getExpressions()) {
theName = theExpression.toString();
theGraphics.drawString(theName, 20, y + theMetrics.getAscent());
y += theMetrics.getAscent();
}
}
}
}
}
public SplashWindow() {
window = new JFrame() {
/** default serial id. */
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
// Draw splash image and superimposed text
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.drawImage(splashImage, 0, 0, this);
g2d.setColor(Color.WHITE);
g2d.setFont(new Font("SansSerif", Font.PLAIN, 36));
g2d.drawString(MSP_STRING, 30, 60);
g2d.setFont(versionStringFont);
g2d.drawString(VERSION_STRING, splashImage.getWidth(this) - versionStringWidth - 16, 24);
}
};
splashImage = ImageLoader.getImage(IMAGE_NAME);
ImageIcon splashIcon = new ImageIcon(splashImage);
width = splashIcon.getIconWidth();
height = splashIcon.getIconHeight();
window.setSize(width, height);
// Center the splash window on the screen.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = new Dimension(width, height);
window.setLocation(((screenSize.width - windowSize.width) / 2), ((screenSize.height - windowSize.height) / 2));
window.setBackground(Color.black);
window.setUndecorated(true);
// Set icon image for window.
setIconImage();
// Set cursor style.
window.setCursor(new Cursor(Cursor.WAIT_CURSOR));
// Display the splash window.
window.setVisible(true);
}
/**
* Initialize witht he specified image icon.
* @param logoIcon an icon contain the imge of a logo.
*/
public ImageData(final ImageIcon logoIcon) {
img = logoIcon.getImage();
imgw = logoIcon.getIconWidth();
imgh = logoIcon.getIconHeight();
}
/**
* Render a completion item using the provided icon and left and right
* html texts.
*
* @param icon icon 16x16 that will be displayed on the left. It may be null
* which means that no icon will be displayed but the space for the icon
* will still be reserved (to properly align with other items
* that will provide an icon).
*
* @param leftHtmlText html text that will be displayed on the left side
* of the item's rendering area next to the icon.
* <br/>
* It may be null which indicates that no left text will be displayed.
* <br/>
* If there's not enough horizontal space in the rendering area
* the text will be shrinked and "..." will be displayed at the end.
*
* @param rightHtmlText html text that will be aligned to the right edge
* of the item's rendering area.
* <br/>
* It may be null which means that no right text will be displayed.
* <br/>
* The right text is always attempted to be fully displayed unlike
* the left text that may be shrinked if there's not enough rendering space
* in the horizontal direction.
* <br/>
* If there's not enough space even for the right text it will be shrinked
* and "..." will be displayed at the end of the rendered string.
* @param g non-null graphics through which the rendering happens.
* @param defaultFont non-null default font to be used for rendering.
* @param defaultColor non-null default color to be used for rendering.
* @param width >=0 available width for rendering.
* @param height >=0 available height for rendering.
* @param selected whether the item being rendered is currently selected
* in the completion's JList. If selected the foreground color is forced
* to be black for all parts of the rendered strings.
*/
public static void renderHtml(ImageIcon icon, String leftHtmlText, String rightHtmlText,
Graphics g, Font defaultFont, Color defaultColor,
int width, int height, boolean selected) {
if (icon != null) {
// The image of the ImageIcon should already be loaded
// so no ImageObserver should be necessary
g.drawImage(icon.getImage(), BEFORE_ICON_GAP, (height - icon.getIconHeight()) /2, null);
}
int iconWidth = BEFORE_ICON_GAP + (icon != null ? icon.getIconWidth() : ICON_WIDTH) + AFTER_ICON_GAP;
int rightTextX = width - AFTER_RIGHT_TEXT_GAP;
FontMetrics fm = g.getFontMetrics(defaultFont);
int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();
if (rightHtmlText != null && rightHtmlText.length() > 0) {
int rightTextWidth = (int)PatchedHtmlRenderer.renderHTML(rightHtmlText, g, 0, 0, Integer.MAX_VALUE, 0,
defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, false, true);
rightTextX = Math.max(iconWidth, rightTextX - rightTextWidth);
// Render right text
PatchedHtmlRenderer.renderHTML(rightHtmlText, g, rightTextX, textY, rightTextWidth, textY,
defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, true, selected);
rightTextX = Math.max(iconWidth, rightTextX - BEFORE_RIGHT_TEXT_GAP);
}
// Render left text
if (leftHtmlText != null && leftHtmlText.length() > 0 && rightTextX > iconWidth) { // any space for left text?
PatchedHtmlRenderer.renderHTML(leftHtmlText, g, iconWidth, textY, rightTextX - iconWidth, textY,
defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_TRUNCATE, true, selected);
}
}