使用Grid
,您可以将鼠标悬停在像素上,以查看 alpha 分量如何在图标边缘滚动。
public Grid(String name) {
this.setBackground(new Color(0xFFFFFFC0));
Icon icon = null;
try {
icon = new ImageIcon(new URL("http://i.stack.imgur.com/1EZVZ.png"));
} catch (MalformedURLException e) {
e.printStackTrace(System.err);
}
...
}
这IconTest
显示了图标如何以不同的 alpha 和默认 AlphaComposite.SRC_OVER
规则呈现。
/*** @see https://stackoverflow.com/a/14432025/230513 */
public class IconTest {
private static final int N = 8;
private static final Random r = new Random();
public static void main(String[] args) throws MalformedURLException {
final URL url = new URL("http://i.stack.imgur.com/1EZVZ.png");
final Icon icon = new ImageIcon(url);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("IconTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(N, N));
for (int i = 0; i < N * N; i++) {
final JLabel label = new JLabel(icon);
label.setOpaque(true);
label.setBackground(new Color(0, 0, 255, i));
p.add(label);
}
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}