Results 1 to 1 of 1
-
Displaying a Button with Varying Icons
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.Polygon; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; public class ChangingButtonIconTest { static class TriangleIcon implements Icon { String name; static class State { public static final int NORMAL = 0; public static final int PRESSED = 1; public static final int ROLLOVER = 2; private State() { } } int state; Color color; public TriangleIcon(Color c, int state) { color = c; this.state = state; } public int getIconWidth() { return 20; } public int getIconHeight() { return 20; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(color); Polygon p = new Polygon(); if (state == State.NORMAL) { p.addPoint(x + (getIconWidth() / 2), y); p.addPoint(x, y + getIconHeight() - 1); p.addPoint(x + getIconWidth() - 1, y + getIconHeight() - 1); } else if (state == State.PRESSED) { p.addPoint(x, y); p.addPoint(x + getIconWidth() - 1, y); p.addPoint(x + (getIconWidth() / 2), y + getIconHeight() - 1); } else { p.addPoint(x, y); p.addPoint(x, y + getIconHeight() - 1); p.addPoint(x + getIconWidth() - 1, y + (getIconHeight() / 2)); } g.fillPolygon(p); } } public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); Icon normalIcon = new TriangleIcon(Color.red, TriangleIcon.State.NORMAL); Icon pressedIcon = new TriangleIcon(Color.red, TriangleIcon.State.PRESSED); Icon rolloverIcon = new TriangleIcon(Color.red, TriangleIcon.State.ROLLOVER); JButton b = new JButton("Button", normalIcon); b.setPressedIcon(pressedIcon); b.setRolloverIcon(rolloverIcon); b.setRolloverEnabled(true); contentPane.add(b, BorderLayout.NORTH); frame.setSize(300, 100); frame.show(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Displaying a Button with a Border
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:40 PM -
Displaying text in a JTextField after pressing a button
By RLRExtra in forum New To JavaReplies: 5Last Post: 01-17-2008, 09:01 PM -
Moving icons on your desktop
By Leprechaun in forum New To JavaReplies: 3Last Post: 12-14-2007, 10:07 AM -
Images not displaying in JSP in IE7
By chadscc in forum Advanced JavaReplies: 0Last Post: 11-13-2007, 03:24 PM -
Java Next Button for displaying next row
By susan in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 08-07-2007, 04:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks