Greenfoot - mouse click and an object removes - java

new to Java here but i have been experimenting...
I am trying to achieve this in Greenfoot: I want to have a mouse click on an object (Actor) and it disappears from the world. This is my code so far:
public void act()
{
disappear();
}
public void disappear(){
if(Greenfoot.mouseClicked(this)){
getWorld().removeObject(this);
}
}
From my understanding, when the mouse is clicked, then it will remove the object from the world... but the object still doesn't disappear, what am i missing here?
Many thanks!

You can regist the action
getWorld().removeObject(this);
to the mouse clicked event list.
Maybe show us the class archi of your system could give us more information.

Related

MouseListener across several panels?

My user story is the following:
In order to upgrade a tower, the player has to drag a gun from the shop and drop it on the tower.
In practice, the GameView contains both the BattlefieldCanvas and the ShopView. Besides, the ShopView contains a GunSelector for each buyable gun. All those guys are sub-classes of JPanel.
I'm currently using a MousListener to handle several actions performed on the BattlefieldCanvas; I thought I could use the same stuff to handle a mouse trip from one of the GunSelector to the BattlefieldCanvas ( = across several panels ), so I tried to add the same MousListener to the gun selectors and the battlefield.
Problem: doesn't work. The getSource() method of the event object returns a reference to the gun selector while the mouse is actually released on the battlefield.
PS: Unlike gun selectors, towers are not swing components but images drawn by the paintComponent method.
1) To answer to your original question as to why source is still the component mouse was clicked on. You just need to read the JavaDoc:
public Object getSource()
The object on which the Event initially occurred.
Returns:
The object on which the Event initially occurred.
2) Now, how can we get the actual component that mouse is released on. You can try this approach:
#Override
public void mouseReleased(MouseEvent e) {
Component theCOmponentMouseIsReleasedOn = frame.findComponentAt( e.getLocationOnScreen() );
}
You don't need to always call findComponentAt on frame, you can call on the container that contains your BattlefieldCanvas.

Processing/PApplet Show or hide a figure based on clicking the mouse

Hi guys. I am a newbie programmer and learning Java with processing.core.PApplet on Eclipse. The project visualizes Earthquakes/Majors Cities/Airports on the google Map. I created a "Control Window" which allows users to toggle on or off markers when they click the square box next to the text. Now my problem is, I want the "green check mark" to show inside the box when user clicks it and vanish when user clicks it again, but I don't know how to do it.
Thank you.
Note: The major methods I have are setUp() ,draw(), mouseClicked() and mouseMoved(). All text,rect, and the check mark was drawn in draw() method. I use the MouseClicked() method to check whether the location where the cursor clicked is inside the box.
Just use a boolean variable that stores whether the checkbox should be drawn. Here's a little example:
boolean checked = false;
void mouseClicked(){
checked = !checked;
}
void draw() {
background(0);
if (checked) {
ellipse(50, 50, 20, 20);
}
}
You might also try using a GUI library like G4P, which has a checkbox component you can use instead of drawing it yourself.

Context help for every jComponent with questionmark cursor

Most windows users may remember that every windows 98 properties/settings window had a little question mark button next to other window buttons:
If you clicked on that button, all click events were overriden by different callback for that window. And that new callback would display element's individual help text.
I'd like to do the very same. My idea was to do it using class which holds all JComponent and Help associations:
public interface Help {
/** based on implementation, displays help to the used. May use
* JDialog, url redirection or maybe open document on the computer.**/
public void getHelp(JComponent comp, ActionEvent evt);
}
public class HelpLibrary {
public HashMap<JComponent, Help> helpLib;
public void getHelp(JComponent comp, ActionEvent evt) {
Help help = helpLib.get(comp);
if(help!=null) {
help.getHelp(comp, evt);
}
}
}
Writing these two classes was the easy part. The hard one is this:
How to override all click events in particular window and then remove override after help was called?
How to ensure help cursor will override all other cursors, and again, safely remove this setting?
I have no idea where to start with this. I really do not want to change the GUI structure or used classes just because of this, which is why I want to store the help and do the overrides from the outside.
public class HelpLibrary {
/**
* Overrides click events on the given window and displays help cursor.
* User then may click a JComponent, such as button, to initiate
* help callback for that element. If no help exists for that element,
* do nothing and stop the help mode.
* #param window the window to get help for
**/
public void waitForHelp(JFrame window) {
???
}
}
You can try following:
Register a global MouseListener using
Toolkit.getDefaultToolkit().addAWTEventListener(myListener, AWTEvent.MOUSE_EVENT_MASK)
Cast the incoming event to MouseEvent and check the event type using the getID() method
If the event is a click for a component, which has help, you need to show help, consume event and remove this listener from the global listener list.
You can also override mouseEnter/Exit event in this
listener for components which have help text, and set the cursor to
question/normal type (don't forget to consume this event).
This is only idea for you, I've not tested whether it works.
You could use a GlassPane.
Read the section from the Swing tutorial on How to Use Root Panes. The Glass Pane demo shows how to intercept mouse events and redispatch the event to the underlying components. YoOu would obviously change this code to find the component under the mouse click and then display the help context.
The glass pane can be toggeled on/off by making it visible or not.

ListSelectionListener double click

For a school project I have to use a ListSelectionListener(LSL) on a JList. I know that an LSL responds to a mouse click and a mouse release. But for the project, i have to let it respond to a double click. Is there anyway to make an LSL respond to that?
I don't know what a ListActionHandler is since you haven't provided the code for it.
My guess is that it implements MouseListener, or maybe extends MouseAdapter. If so, there will be a method called public void mouseClicked(MouseEvent e) that you'll have to implement. In there, just put an if-check to only respond to double-clicks:
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// do your stuff here
}
}
EDIT:
Now that you've corrected your post to a ListSelectionListener, you'll notice that there is only one method to implement, void valueChanged(ListSelectionEvent e) which does not directly translate to mouse clicks.
Why? Because the mouse isn't the only way to change selection on a JList. It can be done via the arrow keys, or programmatically.
This can be (sort of) solved by adding a MouseListener to the JList and then implementing the click count code I've shown. However, most would consider this a hack since using MouseListeners to track changes in a JList's selection is not advised for reasons already mentioned.

Keypressed while a thread is running - how to capture the keys?

I have a JPanel that contains a few other objects that do stuff. I will simplify the example by talking about some circle object (defined by circle class I made), and a square object (similar).
the circle moves randomly around the screen, while the square sits at its place. my intention is to move the square using the arrow buttons.
the current design is to have a thread with a while loop that contains a delay that sets the 'refresh rate' inside its run method.
I'm trying every method I know to capture the arrow keys and move the square around while the ball is running around the screen.
how do I capture keypresses (arrows for the example) so I can know where to move my square to?
I tried implementing keylistener in the jpanel but it didn't work. when I tried to use a KeyEvent in the run, I got an exception.
please save me. :)
EDIT:
Thanks for that info. I would like further help to settle this issue -
lets say I have the following code:
this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "actionName");
this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "actionName");
this.getActionMap().put("actionName",
new AbstractAction("actionName") {
public void actionPerformed(ActionEvent evt) {
//dostuff
}
}
);
how do I distinguish between UP and DOWN presses? what do I need to change?
Thanks! I'm a bit of a newbie, I know :)
KeyListener isn't designated for listening in Swing GUI, this Listener was builded for pre_historic AWT Component, these days so far away, use KeyBindings, this example can save your person

Categories