how to restrict the mouse just on the icon on java - java

I have button on frame. On that button I put an image, which as you see that is a search icon. I remove the visible border on the button. But still I have two questions:
when I put the mouse on the button, the image and also the invisible button border is clickable, what I need is restrict it just on the icon.
I want to make the icon focusable by clicking like click on the simple button.
Search Icon:

What sort of image file format are you using? If it's a format with transparency support, you'll have to implement an algorithm that checks whether the click coordinate is inside of the non-transparent pixels of the icon you're using. You'll have to offset the rgb checks on the image pixels by the position of the image on your frame as well. Use this as a reference - Java Image Processing

Related

Java Swing : best way to set background image clickable

I try to find the best way to make an image clickable with different action depending on the area you click. For now, my window display an EFTPOS (image found on google image) using :
getContentPane().add(new JPanelBackground("./img/background.png", "./img/eftposAboveBackground.png", this.getWidth(), this.getHeight()));
Now, I would like to click on every EFTPOS's buttons. I was thinking of create some transparent buttons at the buttons's positions but :
First, I don't want to use an absolute layer
Second, the Swing GUI design editor (Netbeans) doesn't display my background image since I used the code above.
I'm pretty sure there is another way than setting my eftpos on the background, maybe using the ImageIcon class..
Any help would be greatly appreciated!
You can to create an ArrayList of Rectangles (or Shapes) to represent the area of each button on the image.
You need to add a MouseListener to the image.
In the mousePressed() event you need to iterate through all the Rectangle in the ArrayList using the Rectangle.contains(mouse point) method to see if the user clicked in a Rectangle.
When you find the Rectangle that was clicked you execute your custom code.

Howto paint a popup with image beside a component being positioned in a relative layout manager?

A have a list with small thumbnails and want to show a fancy tooltip with a larger version of the image of the thumbnail left to the thumbnail if one hovers with the mouse over the thumbnail. If one drags the mouse down to the next list element the old tooltip should disappear and a new tooltip next to the new cell should appear.
Unlike the existing text tooltip javax.swing.JComponent.setToolTipText(String) my fancy tooltip should always be displayed left of the current column. The problem is, that this is a very complex relative layout I cannot change for this. The fancy tooltip should appear above all other components like the real tooltip does too, i.e. I cannot reserve some free space for it.
I took a look in the implemenation of the real tooltip and think about something like getting the absolute coordinates of the mouse mouseEvent.getX() and subtracting the position inside the thumbnail to get the right corner of my fancy tooltip. Is it possible to get the coordinates of the mouse event inside the component where the listener is registered? Any better ideas for my needs?
I thought java.awt.event.MouseEvent.getX() is the absolute point, but thats java.awt.event.MouseEvent.getXOnScreen(), i.e. this is already the answer...

JButton with background image and backlighting

I'm writing checkers. So, every cell is JButton, it has image (ImageIcon).
I want JButton that has background image and backlighting (for example, blue of red), because I want to show user available turns. Is it possible?
Now if I create new JButton(Icon icon) and then setBackground(Color.BLUE);
background is ignored.
You should set its border color to BLUE or RED (I believe this is what you are looking for as an answer)
Try this statement:
chessButton.setBorder(BorderFactory.createLineBorder(Color.GREEN));
Painting JButton's border is not effected by image icon, therefore if you want to create a highlight effect and set border's color you should use BorderFactory. You can also have various effects for the border through BorderFactory
If you want complete background highlighted or some special effect then have two separate image of each cell i.e. one normal image and one highlighted image. When you want to show user its available turns, simply update the respective cells with their corresponding highlighted image

Android animation picture change

I want my program to do the following:
In frame there is a button and some image. When button is clicked I want this image move to the left until it will not be visible on frame (be outside of frame) and at the same time next image should come from outside of the frame to the center of it and stops there until the button is clicked again.
Should I inflate some ImageView everytime the button is clicked and change their x coord. though property animation? Or I can make two ImageView on frame and one of them would be transparent while other will be visible? And then change their positions and transparency level ("alpha") when button is clicked?
Which is the right way to do it?
A ViewFlipper might do what you want. From the javadoc:
Simple ViewAnimator that will animate between two or more views that have been
added to it. Only one child is shown at a time. If requested, can automatically
flip between each child at a regular interval.
Presumably, you can add two ImageView instances as children of the ViewFlipper and then animate between them with a slide animation. There are some answers on SO detailing this. Try searching for ViewFlipper.

JToolBar buttons with different size images-NetBeans

I'm having problem in my JToolBar. I'm using images of different size.
Tool bar is not looking good. How can I use different size images in the JToolbar buttons?
How can I show the button label below each image?
How can all the buttons be aligned from the top, left corner?
For example,
java.net.URL imageURL2 = cldr.getResource("Images/report2.jpg");
ImageIcon aceOfDiamonds1 = new ImageIcon(imageURL2);
btnReport = new JButton(aceOfDiamonds1);
btnReport.setMaximumSize(new Dimension(49, 43));
btnReport.addActionListener(this);
jToolBar1.add(btnReport);
I'm using images of different size.
Resize them. Either once at time of build (recommended), or at run-time.
And I want to show the button label below each image
This is close.
newJButton(String,Icon);
And i want to show the button label below to the each image?
With the setVerticalTextPosition and setHorizontalTextPosition methods of JButton:
// Place text below icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
How to show all this buttons align from the left top corner
As Andrew Thompson said, the images all have to be the same size.

Categories

Resources