下面列出了javax.swing.JLabel#paint ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static HashSet getAntialiasedColors(Object aaHint, int lcdContrast) {
JLabel label = new JLabel("ABCD");
label.setSize(label.getPreferredSize());
label.putClientProperty(KEY_TEXT_ANTIALIASING, aaHint);
label.putClientProperty(KEY_TEXT_LCD_CONTRAST, lcdContrast);
int w = label.getWidth();
int h = label.getHeight();
BufferedImage buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImage.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
label.paint(g);
g.dispose();
HashSet<Color> colors = new HashSet<>();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Color color = new Color(buffImage.getRGB(i, j));
colors.add(color);
}
}
return colors;
}
private static Icon centeredIcon(final Icon icon, final int width, final int height) {
JLabel l = new JLabel(icon);
l.setIconTextGap(0);
l.setBorder(null);
l.setSize(width, height);
BufferedImage img = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_ARGB);
l.paint(img.getGraphics());
return new ImageIcon(img);
}
public Image getThumbNailText(Image img, String text) {
img = getThumbNail(img);
BufferedImage imgtext = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = imgtext.createGraphics();
// The text
JLabel label = new JLabel();
label.setOpaque(false);
label.setFont(label.getFont().deriveFont((float)m_font_size));
//label.setText(text);
label.setText("<html>" + text + "</html>");
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
label.setVerticalAlignment(javax.swing.SwingConstants.TOP);
Dimension d = label.getPreferredSize();
//label.setBounds(0, 0, imgtext.getWidth(), d.height);
label.setBounds(0, 0, imgtext.getWidth(), imgtext.getHeight());
// The background
Color c1 = new Color(0xff, 0xff, 0xff, 0x40);
Color c2 = new Color(0xff, 0xff, 0xff, 0xd0);
// Point2D center = new Point2D.Float(imgtext.getWidth() / 2, label.getHeight());
// float radius = imgtext.getWidth() / 3;
// float[] dist = {0.1f, 1.0f};
// Color[] colors = {c2, c1};
// Paint gpaint = new RadialGradientPaint(center, radius, dist, colors);
Paint gpaint = new GradientPaint(new Point(0,0), c1, new Point(label.getWidth() / 2, 0), c2, true);
g2d.drawImage(img, 0, 0, null);
g2d.translate(0, imgtext.getHeight() - label.getHeight());
g2d.setPaint(gpaint);
g2d.fillRect(0 , 0, imgtext.getWidth(), label.getHeight());
label.paint(g2d);
g2d.dispose();
return imgtext;
}
@Override
public void paintBorder(Component c, java.awt.Graphics g, int x, int y, int width, int height) {
DefaultOutlineCellRenderer ren = (DefaultOutlineCellRenderer)
((JComponent) c).getClientProperty(DefaultOutlineCellRenderer.class);
if (ren == null) {
ren = (DefaultOutlineCellRenderer) c;
}
if (ren.isShowHandle() && !ren.isLeaf()) {
Icon icon = ren.isExpanded() ? getExpandedIcon() : getCollapsedIcon();
int iconY;
int iconX = ren.getNestingDepth() * getNestingWidth();
if (icon.getIconHeight() < height) {
iconY = (height / 2) - (icon.getIconHeight() / 2);
} else {
iconY = 0;
}
if (isNimbus) {
iconX += icon.getIconWidth()/3; // To look good
}
if (isGtk) {
JLabel lbl = ren.isExpanded () ? lExpandedIcon : lCollapsedIcon;
lbl.setSize (Math.max (getExpansionHandleWidth (), iconX + getExpansionHandleWidth ()), height);
lbl.paint (g);
} else {
icon.paintIcon(c, g, iconX, iconY);
}
}
JCheckBox chBox = ren.getCheckBox();
if (chBox != null) {
int chBoxX = getExpansionHandleWidth() + ren.getNestingDepth() * getNestingWidth();
Rectangle bounds = chBox.getBounds();
int chBoxY;
if (bounds.getHeight() < height) {
chBoxY = (height / 2) - (((int) bounds.getHeight()) / 2);
} else {
if (isNimbus) {
chBoxY = 1;
} else {
chBoxY = 0;
}
}
Dimension chDim = chBox.getSize();
java.awt.Graphics gch = g.create(chBoxX, chBoxY, chDim.width, chDim.height);
chBox.paint(gch);
}
}