JMapViewer Issue [duplicate] - java

I am trying to add a mouse listener to a MapMarker, so that when a mouse will hover a MapMarker, I could react with an event.
I implemented the mouseListener, but I can't really add a listener.
The issue is that I did not find a way the MapMarker will addMouseListener, due to the fact that non of the hierarchy implements JPanel.
Any help appreciated

As noted here, the default JMapViewer constructor uses a DefaultMapController, "which implements map moving by pressing the right mouse button and zooming by double click or by mouse wheel." To see map coordinates, extend DefaultMapController, override mouseClicked() and use one of the viewer's getPosition() methods. You can use your controller as shown in comments at line 65 of Demo.java, r30377.
new DefaultMapController(map){
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(map.getPosition(e.getPoint()));
}
};

Related

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

Connect 4 GUI in Java (JApplet)

I am currently programming a Connect 4 Applet game, and i already have all the AI logic implemented. Now i need to design a simple gui so i can play with.
I have an image of the board that i want to use (a transparent png) and i want to be able to perform a move by clicking on the image (e.g on the specific column i want to play).
What is the best way of doing this? I thougt of using a class that extends JPanel,where i put all the buttons and stuff...including painting the board image. But can i put a mouse listener on an Image??
"But can i put a mouse listener on an Image??"
No, or at least, not in the context you are thinking.
You have a couple of choices depending on what you want to achieve.
The basic requirement though, is you are going to need to know exactly where on the image each click point is. This is best achieved by using an image editing program to map out the "hot spots" and then code these into your program
You could...
Use a JLabel to render the board image and attach a MouseListener to it.
The problem I would have with this is trying to figure out how to update the image with the player markers.
You could...
Use a JPanel and override it's paintComponent to render the image and player moves/markers.
You would then add a MouseListener to it and monitor the mouse clicks from there.
Regardless of which method you used, I would probably create a List of Rectangles which represented the hot spots the user can click. Each time mousePressed is called, I would walk this list and use Rectangle#contains(Point), passing the mouse click point, to determine which hot spot was clicked.
You would then to to compare this with the game model to determine if it's a valid move or not and take appropriate action as required.
Take a look at How to write a Mouse Listener and Performing custom painting for more details
Use 16(?) JToggleButton objects in a 4x4(?) GridLayout.
Knock the space & background out of the buttons as seen in the answer to Add a complex image in the panel, with buttons around it in one customized user interface.
Related: How to Use
Buttons, Check Boxes, and Radio Buttons
GridLayout
Advantages of Approach
There is no need to extend any component. We can use the 'vanilla' version of the layout, buttons and container. This is favoring composition over inheritance.
Logic of the game can be represented by changing the icons of the buttons. The GUI will update as needed.
It saves coding all the logic to detect which image was clicked. Add an ActionListener and the buttons will respond to either mouse focus/click or keyboard focus/action.
why do you wanna add mouse listener to image? if your class implements mouselistener you can tell what to do when the mouse clicked or what ever else. take a look :
public class Test extends JPanel implements MouseListener {
.
.
.
#Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}

Mouse event handling in Swing

I was wondering, how does a Swing component know where the mouse is and when it is clicked, and how could I use that in my own class, without having to add a new mouse listener every time i want to add an object to a new panel?
EDIT:
Im extending JComponent and I want to get an event method called when the mouse moves
EDIT2:
Got it working now thanks everyone!
Add an actionlistener to your JButton and it will tell you when its been clicked like so:
someButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//the button was pressed and do your stuff here.
}
}
how does the JButton know where the mouse is and when it clicked
thats what for Listeners are - it listens for a corresponding type of event
just implement ActionListener and register it to its listener by doing this:
jbutton.addActionListener(this);
now when you click the button it will generate an event which will b handled in this part
public void actionPerformed(ActionEvent e){
... // handle event
}
From the comments of the other post, you want to register a MouseListener to you custom component using Component#addMouseListener
You may like to have a read through How to write a mouse listener for more info

Setting the position of the cursor in java awt

I've been looking at how to programmatically set the position of the cursor. Doing some googling I found the use of the Robot class. But when I do this it calls the mouseMoved event implemented in MouseMotionListener which I don't want. Are there any other ways to set the position that wouldn't call that method?
The mouseMoved event will still fire no matter what you do, but you can overwrite it, so that it does nothing once fired.
You can overwrite the listener of the component that you are moving the mouse on, so that only that component will ignore the event but others components will trigger properly.
myComponent.addMouseMotionListener(new MouseMotionAdapter()
{
#Override
public void mouseMoved(MouseEvent e)
{
/*Do Nothing*/
}
});

Java JButton - MouseMotionListener ( MouseMoved ) MouseOver Effect

I'm trying to do the MouseOver effect like it is known in JavaScript in Java for a JButton. I added a MouseMotionListener and it worked. I did want to set 2 other Buttons visible, if my mouse touches the 1st Button. So that works perfectly.. but I don´t know how to handle if the mouse isn´t over the Button. I want to setVisible the Buttons false after the Mouse left the Button
Heres my code:
mouseover.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent arg0) {}
public void mouseMoved(MouseEvent arg0) {
del.setVisible(true);
addone.setVisible(true);
}
mouseover is the Button I want to listen to.
del is another Button wich I want to setVisible
addone also
Sry for my not really awesome english :P
Thank you !
You're looking for a MouseListener, specifically implementing mouseExited.
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You may want to use MouseAdapter to avoid being forced to implement all of the methods from MouseListener. MouseAdapter is simply a class that implements the mouse listening interfaces.
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
Perhaps you want to check setRolloverIcon(), setRolloverSelectedIcon() methods, instead of using MouseEvent.
Instead of using a MouseMotionListener. Use a MouseListener, this class has two methods called mouseEntered() and mouseExited() these should allow you to make the necessary changes as the mouse comes in and out of the button.
Here is a brief tutorial on MouseListeners

Categories

Resources