Java GUI builder - How to make an irregular shaped button -
I am working on Netbeans GUI builder and all I want to do is a picture of it (say a stick figure ) And a verb occurs only when I click the stick figure, not just its entire rectangle
Edit: Mainly the question is how do I ignore transparent pixels on the button?
package my.usefulness; Public class utility javax.swing.JFrame {expansion in public utility ()) (initComponents ();} @SuppressWarnings ("uncheck") //
I was curious about this, so I tested to create a new component type. I have considered this approach when you click on this component to see the mouse event location with this image pixel data, if the pixel is transparent then the mouse event should not be registered. This implementation only applies the mouse to the events below and the class is completely away but it should give you a good start! I put it there in TODO tag public class transparency imagebutton implements JComponent MouseListener {Private BufferedImage image = null; Private file image file; Private list & amp; Lt; Executioner & gt; Listeners; Public Transparent Image Baton (file image file) throws IOException {this.imageFile = imageFile; This.image = ImageIO.read (imageFile); This.addMouseListener (this); This.listeners = New ArrayList & lt; ActionListener & gt; (); } Public Zero addActionListener (ActionListener Listener) {listeners.add (listener); } @ Override Protected Zero Paint Comonant (Graphics G) {SuperPaintConant (G); Rectangle = getImageBounds (); G.drawImage (Image, R. X, R.I., R. Width, R. Heights, This); } Personal Rectangle getImageBounds () {// Todo if the size of the component in the appropriate size & lt; image size. Return a new rectangle (int) (getbounds) Width-image .getWidth ()) / 2), (getbounds). Height-image.getHeight ()) / 2), image.getWidth (), Image.getHeight ()); } @ Override Public Wide Mouseclosure (// TODO) Add more action events? } @ Override Public Zero Mouse Mouse (MouseEver E) {Rectangle Boundary = Millimized (); If (bounds.contains (e.getPoint ())) {int ix = e.getX () - bounds.x; Int iy = e.getY () - bounds.y; Int [] arr = image.getData () GetPixel (ix, iy, new int [4]); // Get the alpha for the current pixel {// is not transparent ActionEvent newActionEvent = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, imageFile.getName (), e.getWhen (), E ([3]! = 0 arrival). GetModifiers ()); For (exclamation listener: listeners) {listenener.actionPerformed (newActionEvent); }}} And {}} @ Override Public Wide Mouse Released (Mouse Event E) {// Add Tudo and Action Events? } @ Override Public Wide Mouseender (Mouse Event E) {// Add Toodo and Action Events? } @ Override Public Wide Mouse Acceded (Mouse Event E) {// Add Todo and Action Events? }} I tested this code using this code:
public static void (String [] args) FileNotFoundException, IOException {JFrame frame = new JFrame Throws (); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.Setset (500, 400); Frame.setLocationByPlatform (true); Transparent image button btn = new transchange image button (new file ("c: \\ icon.png")); Btn.addActionListener (New Exhibitist) {@ Override Public Voice ActionPerfed (Action Event e) {System.out.printLine ("click !!")}}}); . Add frame.getContentPane () (btn); Frame.setVisible (true); } and used this image:
Comments
Post a Comment