Java mouseclick logger - java

Hi I want to build on java mouse click logger. I need to print the coordinates of mouse clicks in a file. Can you tell me how I can make this. Which APIs to use some examples or links. I need to get all the mouse clicks not only in one window.

you could implement the MouseListener interface and overwrite the mouseClicked function:
public class MyMouseListener implements MouseListener {
// [...]
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getLocationOnScreen());
}
// [...]
}
Adapt that example to your needs. THe getLocationOnScreen Method returns a "Point" object which you can ask for x/y coordinates.

Related

Can I add my ActionListeners to separate class windows?

I currently have all my action listeners declared under my constructor, but I'm starting to get a lot of them building up. Is it possible to create a new classes (via the default package window) and have them all there separately?
This seems obvious to me, I have tried this and I get no errors, but my application wont load when I do, it says its open but theres nothing there.
Here is a link to my code that is compilable.. I have commented out anything that uses other classes (there isn't much), if I have missed any just comment them out.
https://shrib.com/Tum8kjgH?v=nc
Thanks!
The most simple solution is to declare your listeners each in their own class. For example, for some button:
public class SomeButtonActionListener implements ActionListener{
private InternalFrame iFrame;
public SomeActionListener(InternalFrame iFrame){
this.iFrame = iFrame;
}
public void actionPerformed(ActionEvent ae) {
//TODO
//Example: iFrame.getSomeButton().doSomething();
}
}
Note that in this way, you need to expose getter methods for all swing components you need to access from your listeners (an alternative is to send the specific components needed as arguments to the listener when constructor is called).
In your InternalFrame you can add the listeners as:
someButton.addActionListener(new SomeButtonActionListener(this));
Also you can put all your listeners in a specific package like yourapp.listeners.
EDIT
A more specific example:
public class AddRoomListener implements ActionListener{
private InternalFrame iFrame;
public AddRoomListener(InternalFrame iFrame){
this.iFrame = iFrame;
}
public void actionPerformed(ActionEvent event) {
iFrame.getIntFrame2().setVisible(true);
iFrame.getIntFrame2().toFront();
}
}
In this case you need to declare the getIntFrame2() getter in the InternalFrame class.

Inform class that action has happened - JAVA

I would like to know how can I inform a specific class that action has happened? I heard there is something called "callbacks" but I didn't understand how exactly I can use it.
Like the people above commented on you're post, you can do that with listener interfaces or with adapters that implement the required interface.
For example:
You have a JButton (called button) on the JFrame, you can click the button but nothing really happens.
What you will need to do is add the functionality, this can be done by adding a listener to the button, but first u need to create the listener!
The created listener:
public class TestClickedListener extends MouseAdapter {
//The button text will be set to: Clicked!
public void mouseClicked(MouseEvent source) {
JButton buttonSource = (JButton)source.getSource();
buttonSource.setText("Clicked!");
}
}
In the main method of the JFrame u can then add the created MouseListener to the button:
public static void main(String args[]) {
button.addMouseListener(new TestClickedListener());
}
Now try and click the button ;)
I hope this wil help you out buddy, just a simple example to understand it :) !

Unselect a DrawLine after its drawn

After I draw a DrawLine it stays selected. I would like them to be un-selected after its drawn.
How do I un-select a DrawLine after its drawn?
DrawLine.setEnableSelect(false) (WAG from examining the JavaDocs of an API I'd never heard of before right now).
If you have this method available then use this: DrawLine.setSelectNewLines or you can use setSelected(null) to un-select the newly drawn item. You also need to add DrawLineListener so you can listen to new line to use this function. Sample lines of code will be:
drawLine.addDrawLineListener(new DrawLineListener() {
#Override
public void lineSelected(ChangeEvent ce) {}
#Override
public void lineNew(ChangeEvent ce) {
drawLine.setSelected(null);
}
});

How do I add a MouseListener to a graphics object inside another graphics object?

I'm working on a GUI for a card game and am using the ACM's student graphics library for the sake of familiarity. I have written a program that draws my solitaire game to the screen, and am having trouble making it interactive.
Background:
There are a lot of classes here, and I'll do my best to describe them each.
Top level JFrame containing the application.
GCanvas (that holds all the graphics objects)
SolitaireGameControl (GCompound holding all the other GCompounds making up the solitaire game)
Array of PileViews, a pile of cards (GCompound consisting of an array of Cards)
Cards (GCompound consisting of rectangles and labels)
(GCompound: a collection of graphics objects treated as one object. (If car was a GCompound, it would have GOval[] wheels, GRect body and so when I add it to the canvas, it displays as one object))
A card as seen from the top-level class would look like a bit like this: jFrame.gCanvas.solitaireGameControl.pileViews[pile number].cardView
What I've been trying to do is add a MouseListener to every single card, so that when a card is clicked and a MouseEvent is fired, MouseEvent e.getSource() = the card that was clicked.
Here's how it looks now:
public SolitaireGameControl(SolitaireGame game) {
this.game = game; // Model of the game.
this.pileViews = PileView.getPileViews(game.drawPiles); // ArrayList of PileViews (the pile of cards)
for(PileView pv : pileViews) {
for(CardView cv : pv.cardViews) {
cv.addMouseListener(this); // add a mouseListener to the card
}
}
this.addMouseListener(this); // if I don't include this, nothing happens when I click anything. If I do include this, this whole object is the source.
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getSource()); // should return the card I clicked.
}
When I run this program, the source of every event is SolitaireGameControl, granted I leave in the this.addMouseListener(this);. If I take out this statement, nothing is printed at all, leading me to believe that the mouseListeners I have added are only working one level deep. (The first GCompound on the canvas, not the GCompounds inside it.)
Therefore, my question is as follows: Is there a way to get a MouseListener for a GCompound inside of a GCompound inside of a GCompound, and have MouseEvent's getSource to correctly identify the card? If not, is there a way to restructure my program to make it work as intended? (I know I should really be using a better graphics library for starters.)
That would make sense. From my experience, if I put some components inside a top-level container, the container is the one that receives input events.
Have you tried an approach where you do something like:
/* This is the mouse listener for the top-level container. */
#Override
public void mouseClicked(MouseEvent e) {
for(PileView pv : pileViews) {
for(CardView cv : pv.cardViews) {
if(cv.getBounds().contains(e.getPoint())) {
cv.dispatchEvent(e);
}
}
}
}
... and then handle mouse clicks on a 'CardView' level normally.
When the top-level container receives a mouse event, it checks if the mouse interacted with a card based on the location of the event (if the card's area contains the point). If it did, it passes down the mouse event to the card's mouse listener.
I'm assuming that the elements near the beginning of 'pv.cardViews' are the cards that are more to the front.

Avoiding create new ClickHandler Instances on every Click

im sitting on this for 4 hours now, and once again I end up on Stackoverflow because I just cant solve this (simple) problem.
I want to fire a method when I click a button, Google gives an Example like this:
// Listen for mouse events on the Add button.
addStockButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
});
But this creates a new Instance(?..How can they even create an instance of Clickhandler, since its an Interface) everytime the button is clicked. How can I solve this that all buttons share a Clickhandler and the Handler askes the Button which button he is, so he can fire the method attached to that button.
Any Ideas? If you this is to vage information and you require more code please let me know.
Thanks in advance,
Daniel
Java creates a new instance of an anonymous class that implements ClickHandler. Which it can do because you provide an implementation for the onClick function specified by the interface.
This class is however not created when you click on the button but at the moment you call addClickhandler. If you need the handler for multiple events do something like:
ClickHandler handler = new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
};
addStockButton.addClickHandler(handler);
someOtherButton.addClickHandler(handler);
Within the handler you can identify from where the event is coming using event.getSource().
If you have access to your button variables you could simply check the pointer
if (addStockButton == event.getSource()) ...
Or you can cast the result of getSource to the appropriate type and access the properties/methods of the object.
Eelke has already answered your question. I just add that if you would use GWT's UiBinder feature, you could achieve what you want like this:
#UiField
Button addStockButton;
#UiField
Button removeStockButton;
#UiHandler({ "addStockButton", "removeStockButton" })
void handleClickEvents(ClickEvent event)
{
if (event.getSource() == addStockButton)
{
addStock();
}
else if (event.getSource() == removeStockButton)
{
removeStock();
}
}
Its an anonymous instance of the interface, this is like declaring a new class that implements that interface.
I would have to ask why you would want to do this, you would need to make the ClickHandler contain a reference to its parent. You would also need to make the buttons identifiable so you can select the right one in the body of the ClickHandler. Is your need to only have a single instance really that bad that you can't have multiple anonymous instances ?

Categories