Crossing out the Icon

HiI've got button with icon,after pressing it should start someaction and icon on button must become crossed out.how cross out the iconthanks
[191 byte] By [kpba] at [2007-11-26 21:58:35]
# 1
hi What you mean By Button icon Cross out? Do you want to change the Button's Shape or what you want to do with that?IDon't understand what you mean by cross out.Thank you
jofin123a at 2007-7-10 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 2
button.setIcon( yourCrossedOutIcon );
camickra at 2007-7-10 3:56:46 > top of Java-index,Desktop,Core GUI APIs...
# 3
I want that Icon, after pressing button,were crossed out (MABY BETTER VARIANT - strikethrough) by red(any color)line from left-upper corner to right-lower- must symbolize, that you can cancel started action
kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
As I said you need two iconsa) a normal iconb) a crossed out icon
camickra at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 5

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 ;

}

kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 6
can anybody helpme with this quastion?thank
kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 7

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).

allydma at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 8
> As said above, the best way for this to work would be to have two images.I can do 2 icons,but my task isSTRIKETHROUGH THE ICON (ANY ICON),so thats why I'm asking youthank you
kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 9

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.

kirillga at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 10
YES YES YESTHANK YOU THANK YOU THANK YOU
kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 11

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

kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 12
Then you'll have to debug it a little to see the offsets at runtime.
kirillga at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...
# 13

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);

}

}

kpba at 2007-7-10 3:56:47 > top of Java-index,Desktop,Core GUI APIs...