Let say I created n JButtons and place them randomly in a JFrame and I add a mouse listener on each JButton.
My question is:
When I click on any one of the JButtons, does Java check through all JButtons to determine which button am I clicking (worst case: n checks) OR it does not need to check through all buttons (worst case: 1 check)?
The reason I ask this is because I am trying to interact with 2DGraphics and I know one of the ways to check whether I click on a particular Shape is by checking through all Shapes whether that Shape contains my current mouse coordinates. So if I draw 10 shapes, my worst case would be 10. Honestly speaking, I feel that kind of inefficient.
So I was wondering how does JComponent handles an actionEvent. By checking through all JComponents or they don't?
If they don't, how does Java determines which button was clicked?
Related
I have a draggable panel (not using the java drag and drop) that has to land on another panel. The dragged panel generates the event but I want to obtain the panel it lands on. Do I have to have an array of all the "landable" panels somewhere and cycle through it to get the one I want by comparing positions or is there a faster, better way?
Thanks in advance.
The Container class has a findComponentAt(...) method.
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.
I want to create my UI with a JFrame and a minimize and maximize look-alike button with the same functionality as normal minimize and maximize buttons but I am not sure how. I want to take this approach so there will not be an X button in the top corner.
You cannot remove it and keep the other two, however, you can disable it using setDefaultCloseOperation:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
Note that this will not make the button unclickable but will disable the functionality.
For an undecorated frame you'd have to use JWindow instead of JFrame, then render your own title bar.
Of course, whatever you render will only look right on one particular platform in one particular look and feel (unless you write your own logic to handle different platform conventions by hand). For example, Mac users will expect the close, min and max buttons to be traffic light coloured circles at the left hand end of the title bar, not square buttons at the right.
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 uses a FlowLayout and contains many JButtons. The buttons are in a 2-dimensional grid (by the FlowLayout). The JPanel can be resized, and, of course, when it is, the location of the buttons in the grid changes (although the order, of course, stays the same). I want the user to be able to navigate from one button to another in the grid by using the arrow keys on the keyboard.
Is there a way to tell for a given button, which button is currently directly above it, which button is directly below it, and which buttons are to its immediate left and right?
Obviously, this would be trivial if I were using a GridLayout, but I need to use a FlowLayout for other reasons.
The left and right arrow keys are not an issue. As mentioned by jzd you just add the KeyStrokes to the set of traversal keys.
For the up/down keys you will need to create a custom Action. You can use the location of the current component. Then to go up you can change the Y coordinated by say 10 pixels (5 pixels for the row gap between components pluse an extra 5). Then you can use the:
Container.getComponentAt(Point p)
to find the component at that new location.
To go down you would start with the location of the button then add on the height of the button plus 10 pixels.
Of course you would then use Key Bindings to bind the up/down KeyStroke to the Action.
Note: I'm not sure if you need to add the 5 extra pixels to find the componen above or below the component. You may just be able to use the vertical gap. I'm just not sure how the boundary checking works on the getComponentAt() method.
I think you can just use focus travel implementation that is in place as tab or shift navigates the selected buttons in a FlowLayout correctly.
I think you just need to add the arrow keys to sets like the forwardDefaultFocusTraversalKeys
More info:
http://download.oracle.com/javase/tutorial/uiswing/misc/focus.html