no
I need smth like this:
..................
ImageIcon oldValue = jButton1.getValue(Action.SMALL_ICON);
jButton1.putValue(Action.SMALL_ICON, striking(oldValue ));
...................
public ImageIcon striking(ImageIcon icon){
ImageIcon strikethroughIcon = icon;
// here you should strikethrough icon
return strikethroughIcon ;
}
As said above, the best way for this to work would be to have two images.
You load up the button with image 1 (the normal image), then you add an event handler to it.
In the event handler code you put:
button.setIcon( yourCrossedOutIcon );
Which would then make your button appear as image 2 (the crossed out image).
Skeleton:
class StrikeTIcon extends Icon {
Icon original;
public StrikeIcon(Icon original) {
this.original = original;
}
public void paintIcon(...) {
this.original.paintIcon(...);
Graphics g2 = (Graphics2D)g.create();
g2.setColor(Color.red);
g2.drawLine(0,0,width,height);
g2.dispose();
}
}
Change the stroke and the line coordinates to match your strikethrough design. Delegate all the other Icon methods to the original icon as well.
Unfotunatly - NO
your code
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Color.red);
//possibly here mistake
g2.drawLine(0, 0, icon.getIconWidth(), icon.getIconHeight());
g2.dispose();
icon.paintIcon(c, g, x, y);
}
do this:
http://www.picatom.com/3/img1-1.html
and of course without striking through it looks
http://www.picatom.com/3/img2-1.html
right answer:
public class StrikethroughIcon
extends ImageIcon {
public StrikethroughIcon(ImageIcon icon) {
super(icon.getImage());
}
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
try {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
} catch (Exception ignore) { }
g.setColor(Color.red);
g.fillPolygon(new int[]{x,x+3,x+getIconWidth(), x+getIconWidth()-3}
,new int[]{y+getIconHeight(), y+getIconHeight(),y, y},4);
}
}