下面列出了javax.swing.ImageIcon#paintIcon ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Drag gesture recognized. Start the drag off if valid.
*
* @param evt
* Drag gesture event
*/
@Override
public void dragGestureRecognized(DragGestureEvent evt) {
DragEntry dragEntry = kseFrame.dragSelectedEntry();
if (dragEntry == null) {
return;
}
ImageIcon icon = dragEntry.getImage();
// Draw image as drag cursor
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight());
BufferedImage buffImage = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB_PRE);
icon.paintIcon(evt.getComponent(), buffImage.getGraphics(), 0, 0);
cursor = toolkit.createCustomCursor(buffImage, new Point(0, 0), "keystore-entry");
evt.startDrag(cursor, new KeyStoreEntryTransferable(dragEntry), this);
}
@Override
public void serialize(ImageIcon vi, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
synchronized (vi) {
BufferedImage v = new BufferedImage(
vi.getIconWidth(),
vi.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = v.createGraphics();
// paint the Icon to the BufferedImage.
vi.paintIcon(null, g, 0, 0);
g.dispose();
byte [] data = Images.encode(v);
jgen.writeStartObject();
jgen.writeStringField("type", "ImageIcon");
jgen.writeObjectField("imageData", data);
jgen.writeNumberField("width", v.getWidth());
jgen.writeNumberField("height", v.getHeight());
jgen.writeEndObject();
}
}
private void doPaintIcon(Component c, Graphics g, int x, int y, Dimension badgeSize) {
BufferedImage cached = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics gc = cached.getGraphics();
base.paintIcon(c, gc, x, y);
for (BadgePosition pos : BadgePosition.values()) {
if (!isBadgeVisible(pos)) {
continue;
}
MultiIcon icon = badgeMap.get(pos);
Icon scaled = new ScaledImageIconWrapper(icon, badgeSize.width, badgeSize.height);
Point badgePaintLoc = getBadgePaintLocation(pos, badgeSize);
int badgeX = x + badgePaintLoc.x;
int badgeY = y + badgePaintLoc.y;
scaled.paintIcon(c, gc, badgeX, badgeY);
if (!isBadgeEnabled(pos)) {
// Alpha blend to background
Color bgColor = c.getBackground();
gc.setColor(
new Color(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue(), 128));
gc.fillRect(badgeX, badgeY, badgeSize.width, badgeSize.height);
}
}
cachedThis = new ImageIcon(cached);
cachedThis.paintIcon(c, g, x, y);
}
@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;
}
@Override
public Shape render(Area b, Graphics2D g, ScreenTransform t) {
ImageIcon icon = null;
switch (b.getStandardURN()) {
case REFUGE:
icon = REFUGE;
break;
case GAS_STATION:
icon = GAS_STATION;
break;
case FIRE_STATION:
icon = FIRE_STATION;
break;
case AMBULANCE_CENTRE:
icon = AMBULANCE_CENTRE;
break;
case POLICE_OFFICE:
icon = POLICE_OFFICE;
break;
case HYDRANT:
icon = HYDRANT;
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;
}
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);
}
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 BufferedImage createDefaultPortrait()
{
ImageIcon defaultPortrait = Icons.DefaultPortrait.getImageIcon();
BufferedImage bufImage = new BufferedImage(defaultPortrait.getIconWidth(), defaultPortrait.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
defaultPortrait.paintIcon(this, bufImage.createGraphics(), 0, 0);
return bufImage;
}
private BufferedImage createDefaultPortrait()
{
ImageIcon defaultPortrait = Icons.DefaultPortrait.getImageIcon();
BufferedImage bufImage = new BufferedImage(defaultPortrait.getIconWidth(), defaultPortrait.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
defaultPortrait.paintIcon(this, bufImage.createGraphics(), 0, 0);
return bufImage;
}
public LogoNode() {
ImageIcon loadBlank = new ImageIcon(getClass().getResource(PGTUtil.EMPTY_LOGO_IMAGE));
BufferedImage image = new BufferedImage(
loadBlank.getIconWidth(),
loadBlank.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
loadBlank.paintIcon(null, g, 0, 0);
g.dispose();
logoGraph = image;
}
public static BufferedImage getBufferedImage(ImageIcon icon) {
BufferedImage bi = getCompatibleBufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TRANSLUCENT);
Graphics g = bi.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
return bi;
}
public static BufferedImage getConvertedIcon(final ImageIcon icon) {
final BufferedImage bi =
ImageHelper.getCompatibleBufferedImage(
icon.getIconWidth(), icon.getIconHeight(), Transparency.TRANSLUCENT);
final Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0, 0);
g.dispose();
return bi;
}
private void drawIcons(final Graphics2D g2d) {
if (!cardIcons.isEmpty()) {
final int BORDER_WIDTH = 2;
final BasicStroke BORDER_STROKE = new BasicStroke(BORDER_WIDTH);
final Stroke defaultStroke = g2d.getStroke();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// draw icons
int y = 10;
int x = 0;
final int ICON_WIDTH = 36;
final int ICON_HEIGHT = 32;
final int CORNER_ARC = 16;
final GradientPaint PAINT_COLOR = new GradientPaint(0, 0, GRADIENT_FROM_COLOR, ICON_WIDTH, 0, GRADIENT_TO_COLOR);
iconShapes.clear();
for (CardIcon cardIcon : cardIcons) {
// icon bounds should be relative to CardPopupPanel.
final Rectangle2D iconShapeRect = new Rectangle2D.Double((double)x, (double)y, (double)ICON_WIDTH, 32d);
iconShapes.add(iconShapeRect);
//
final Rectangle rect = new Rectangle(x, y, ICON_WIDTH, ICON_HEIGHT);
g2d.setPaint(PAINT_COLOR);
g2d.fillRoundRect(rect.x, rect.y, rect.width, rect.height, CORNER_ARC, CORNER_ARC);
g2d.setPaint(Color.BLACK);
g2d.setStroke(BORDER_STROKE);
g2d.drawRoundRect(rect.x, rect.y, rect.width, rect.height, CORNER_ARC, CORNER_ARC);
g2d.setStroke(defaultStroke);
//
final ImageIcon icon = cardIcon.getIcon();
final int iconOffsetX = (ICON_WIDTH / 2) - (icon.getIconWidth() / 2);
final int iconOffsetY = 16 - (icon.getIconHeight() / 2);
icon.paintIcon(this, g2d, x + iconOffsetX, y + iconOffsetY);
y += ICON_HEIGHT + 1;
}
}
}
public BufferedImage getBufferedImage( String key ) {
ImageIcon icon = getImage(key);
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
return bi;
}
@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();
}
}
}
}
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
SupportTableModel model = (SupportTableModel) table.getModel();
ImageIcon icon = model.getColumnIcon((String) value);
BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
Graphics2D g2d = i.createGraphics();
// setIcon(sortIcon);
if (icon != null) {
icon.paintIcon(this, g2d, 0, 0);
setText("");
if (sortIcon != null) {
g2d.setColor(getBackground());
g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
}
setIcon(new ImageIcon(i));
} else {
setIcon(sortIcon);
setText(value == null ? "" : value.toString());
}
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Icon sortIcon = null;
boolean isPaintingForPrint = false;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
Color fgColor = null;
Color bgColor = null;
if (hasFocus) {
fgColor = UIManager.getColor("TableHeader.focusCellForeground");
bgColor = UIManager.getColor("TableHeader.focusCellBackground");
}
if (fgColor == null) {
fgColor = header.getForeground();
}
if (bgColor == null) {
bgColor = header.getBackground();
}
setForeground(fgColor);
setFont(header.getFont());
isPaintingForPrint = header.isPaintingForPrint();
}
if (!isPaintingForPrint && table.getRowSorter() != null) {
if (!horizontalTextPositionSet) {
// There is a row sorter, and the developer hasn't
// set a text position, change to leading.
setHorizontalTextPosition(JLabel.LEADING);
}
java.util.List<? extends RowSorter.SortKey> sortKeys = table.getRowSorter().getSortKeys();
if (sortKeys.size() > 0
&& sortKeys.get(0).getColumn() == table.convertColumnIndexToModel(column)) {
switch (sortKeys.get(0).getSortOrder()) {
case ASCENDING:
sortIcon = UIManager.getIcon("Table.ascendingSortIcon");
break;
case DESCENDING:
sortIcon = UIManager.getIcon("Table.descendingSortIcon");
break;
case UNSORTED:
sortIcon = UIManager.getIcon("Table.naturalSortIcon");
break;
}
}
}
}
TroopsTableModel model = (TroopsTableModel) table.getModel();
ImageIcon icon = model.getColumnIcon((String) value);
BufferedImage i = ImageUtils.createCompatibleBufferedImage(18, 18, BufferedImage.BITMASK);
Graphics2D g2d = i.createGraphics();
// setIcon(sortIcon);
if (icon != null) {
icon.paintIcon(this, g2d, 0, 0);
setText("");
if (sortIcon != null) {
g2d.setColor(getBackground());
g2d.fillRect(18 - sortIcon.getIconWidth() - 2, 18 - sortIcon.getIconHeight() - 2, sortIcon.getIconWidth() + 2, sortIcon.getIconHeight() + 2);
sortIcon.paintIcon(this, g2d, 18 - sortIcon.getIconWidth() - 1, 18 - sortIcon.getIconHeight() - 1);
}
setIcon(new ImageIcon(i));
} else {
setIcon(sortIcon);
setText(value == null ? "" : value.toString());
}
Border border = null;
if (hasFocus) {
border = UIManager.getBorder("TableHeader.focusCellBorder");
}
if (border == null) {
border = UIManager.getBorder("TableHeader.cellBorder");
}
setBorder(border);
return this;
}
/**
* Paints a tiled icon.
*
* @param c The Component to paint onto
* @param g The Graphics to paint with
* @param icon The icon to paint
* @param sX The starting x location to paint the icon at
* @param sY The starting y location to paint the icon at
* @param width The width of the space that needs to be filled with
* the tiled icon
* @param height The height of the space that needs to be filled with
* the tiled icon
*/
private void paintTiledIcon(Component c, Graphics g, ImageIcon icon,
int sX, int sY, int width, int height){
int tileW = icon.getIconWidth();
int tileH = icon.getIconHeight();
width += sX;
height += sY;
for (int x = sX; x <= width; x += tileW) {
for (int y = sY; y <= height; y += tileH) {
icon.paintIcon(c, g, x, y);
}
}
}