I create buttons using jlabels, so I can make a image into sort of a button. The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. Is there any fix for this, or another component that i could use?
Ex. If i click this on the corner where the circle is not showing, but the square is still there, then the event fires.
Any fixes/different components to use? Thanks!
If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes.
The ShapeComponent will only respond to mouse events within the bounds of the Shape.
Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent.
Related
I want my JPanel to be semi transparent. This panel is animated and has the motion of a dropdown list. I have used the Color(r,g,b,a) constructor to achieve the transparency, however the transparency only takes effect as the panel returns to its original position. For example, as it is moving downwards it is not transparent at all, however when moves up, it spontaneously becomes transparent.
How do I fix this problem?
detailPanel.setBackground(new Color(50,50,50,220));
detailPanel.setLayout(null);
detailPanel.setBounds(0,posY,1200,750);
Check out Backgrounds With Transparency for the cause of the problem (you can't use a transparent Color on an opaque background) and a couple of solutions to fix the problem:
make the component non-opaque and paint the background yourself
use the AlphaContainer class so it can do the painting of the background for you
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.
I am trying to make a simple game that will have squares of random size and color appear at a random location on the screen and you have to click on them. The more you click the higher your score.
I have everything working except, I have no idea how to make it so that when you click on a square it disappears.
Here is my rectangle code
g.fillRect(Cube.cubePosX, Cube.cubePosY, Cube.cubeSize, Cube.cubeSize);
The position, size, and color are already predetermined and randomly selected in another class file.
Suggestions:
Don't hard code the Rectangles that you're drawing.
Instead create a collection of Rectangles such as an ArrayList<Rectangle> that are drawn in a for loop in the paintComponent(...) method of your drawing JPanel.
Then you remove them from the ArrayList when the user clicks on them.
This would be done in a MouseListener and again uses a for loop,
but a key being that this loop iterates backwards. The reason for this is that the Rectangles on "top" of all the others are the last ones drawn. They should be the first ones removed if clicked on.
You would call repaint() on the drawing JPanel (or JComponent) after removing a Rectangle.
I am trying to make a simple game that will have squares of random size and color appear at a random location on the screen and you have to click on them
You can also use Swing components for this. See Playing With Shapes for more ideas. You would add a MouseListener to each component and you can just remove the component from the panel when it is clicked.
Even if you don't want to use components, you should still consider using a Shape so your game can have more shapes than just rectangles. In this case Howevercraft's suggestions would all apply.
You need to keep some kind of cache which tells you where these rectangles are.
You should use this cache to not only detect when they are clicked, but also to paint them.
Take a look at How to create a Mouse Listener for more details.
You make also find Rectangle of some use
I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.
I'm using the MouseInputListener (MouseListener + MouseMotionListener) to drag and drop multiple JLabels in a JPanel. Here is an outline of what I do;
MouseClicked: check if there is any
JLabel within the clicked area of
JPanel; if yes, select it (paint it to
a color,etc). If not, do nothing.
MouseDragged: If a JLabel is selected,
setLocation of that JLabel using
e.getX() and e.getY() of the event. If
nothing is selected, do nothing.
MouseReleased: If a JLabel is
selected, paint it back to its
original color. Select nothing (maybe
null). If not, do nothing.
These are all in JPanel; JPanel implements MouseInputListener.
So here is the problem: When the inital position of a JLabel is 0,0 say I move it to 10,10. And after the mouse release and nothing is selected, when I click on 0,0 it selects that JLabel; however it was supposed to select it if I click at 10,10 because this is its new position.
Now I think this might be because I'm using the wrong coordinates; I've heard that the coordinate values in JPanel are relative, so I have to do a subtraction (i.e. final-initial coordinates) everytime to get the correct coordinates. I did it, but it did not work either. Another possibility might be that Java is storing all the historical X and Y coordinates (so that everytime I click on a previous coord, I select that object) which is purely imagination!
What are your suggestions?
Thanks in advance.
Add the MouseMotionListener to each of the labels instead of adding it to the panel. Then you don't need to determine whether you clicked on a label or not.
See the Component Mover for a general implementation. You would need to customize it to support the coloring requirement.
Edit:
If you add the listener to the panel then the coordinates will always be relative to the panel, not the label, so I'm not sure what the problem is. If you want to find if you clicked on a component then use the Container.getComponentAt(Point) method.
If you need more help then post your SSCCE that demonstrates the problem.