LibGDX: Sequence Not Working - java

I cannot use sequence at all, even THESE gives me an error.
this.AddAction(sequence());
this.AddAction(sequence(setX(getY())));
No matter what I do sequence won't work, despite these being imported. addAction does not work either, nor does addAction & Actions.sequence.. Nothing does as far as this goes.
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AddAction;
Edit: I needed to make my class an Actor.

This worked fine for me.
Do this in your constructor of the screen where you want the action:
image.addAction(sequence(fadeOut(3), run(new Runnable() {
#Override
public void run() {
game.setScreen(new MenuScreen(game));
}
}));

Related

Running code x ticks after opening an inventory (Minecraft spigot)

I have heard of ways to run code later like run task later and other scheduler methods but I want to do it inside of an event which I am having trouble with. Here is some code for an example of where I want to run it:
public class InventoryEvents implements Listener {
#EventHandler
public void onOpen(InventoryOpenEvent e) {
// Run later code here
}
}
When I try using code for something like run task later here or outside of it or even in another class I get a lot of errors relating to the class not extending JavaPlugin or me trying to use it in an event.
Any help is appreciated, thanks :)
You will need to create your scheduled task inside the onOpen method that's handling the InventoryOpenEvent.
Bukkit.getScheduler().runTaskLater(main, new Runnable() {
#Override
public void run() {
//Run your delayed code in here
}
}, 100L);//replace 100 with how many ticks you want to wait before the code executes
The main is where your errors are coming from. You need to use an instance of whatever class extends JavaPlugin (this would be your main class). I recommend passing the instance of your main into the Listener class in the constructor so you can use it as needed. An example would be as follows:
public class NamedListener implements Listener {
private Main main;
public NamedListener(Main main){
this.main = main;
}
}

How to properly destroy a JDialog?

I'm currently working on an application for work that has a main JFrame that always exists. I currently have a child JDialog that shows up on a button press. This frame has a JMenu with an item to "log out of the display." I've been tasked to ensure this child JDialog goes away when the log out of the display option is pressed. When the logout occurs, the main display is set invisible via:
mainFrame.setVisible(false);
The child JDialog has the default close operation:
DISPONSE_ON_CLOSE
When the user logs back in, the first thing that's done is:
mainFrame.setVisible(true);
When this happens, the child dialog shows back up. Looking at the JDialog Javadoc, this seems to be expected behavior. However I haven't found a way to break the parent/child releationship or completely destroy the child JDialog. It also seems like the JDialog will remain until it has been GC, which may not happen in a timely manner.
Here is a sample program that simulates the behavior I'm seeing:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
public class WindowTest {
public static void createAndShowGUI() {
JFrame aFrame = new JFrame("LAUNCHER");
final JFrame aParent = new JFrame("PARENT");
final JDialog aChild = new JDialog(aParent);
aParent.setSize(200,200);
final JToggleButton showParentButton = new JToggleButton("HIDE");
showParentButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showParentButton.setText(!showParentButton.isSelected() ? "SHOW": "HIDE");
aParent.setVisible(!showParentButton.isSelected());
}
});
aChild.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
aChild.setSize(200,200);
aParent.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent e) {
aChild.dispose();
aChild.setVisible(false);
}
});
aFrame.setContentPane(showParentButton);
aFrame.pack();
aFrame.setVisible(true);
aParent.setVisible(true);
aChild.setVisible(true);
}
public static void main(String [] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
}
When the parent is hidden, the child is disposed. When the parent is shown, the child shows back up. What's really weird is that when I press the X on the child: when the parent is hidden and then shown again, the child does not show back up.
The only difference I see is that clicking the X also fires a WindowClosing event. I tried the dispatch the even, in the componentHidden method above by:
//Added into the constructor
//add to the imports: import java.awt.event.WindowEvent;
aParent.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent e) {
aChild.dispose();
aChild.setVisible(false);
WindowEvent closingEvent =
new WindowEvent(aChild, WindowEvent.WINDOW_CLOSING);
aChild.dispatchEvent(closingEvent);
}
});
And that didn't solve the problem.
Currently it looks like my only option is to change the type of child to a JFrame. I just wanted to know if there was a proper way of disposing a child JDialog.
I'm currently running with Java version: 1.7.0_76 64 bit on Redhat Enterprise Linux Server release 6.4.
I wasn't aware the naming conventions affected the compile.
It doesn't. Conventions are done for consistency and readability and maintainability. The person who writes the code is not always the person that maintains the code. So if you want other people to read your code, especially when asking for help, follow the standards.
You can start with Java Programming Style Guidelines
I'm copying the code by hand from another screen per my companies standards.
This is a complete waste of time. There is nothing proprietary about your code. Again when you ask a question, the code should be in the form of a SSCCE so it demonstrates the problem. This allows you to remove all the unnecessary code.
it would be straight forward to figure out the imports.
Exactly, so you should do it. You want us to help you, so why should we spend the time figuring it out??? Make is as easy as possible for people to want to help you.
Adding the imports did not help. The code you posted still does not compile so I don't know if it accurately reflects the problem you are attempting to describe.
Again the point of posting code is so that we can copy/paste/compile/test. Until you post a proper SSCCE I will not provide the answer.
Edit:
From my testing, if the visibility of the child window is changed by the visibility of the parent when the parent is made non-visible, then the visibility of the child is also changed by the parent when it is made visible. So it looks like the parent is retaining the state of the child windows when the visibility changes.
So the solution is to make the child window non-visible before the parent:
showParentButton.setText(!showParentButton.isSelected() ? "SHOW": "HIDE");
aChild.setVisible(false); // add this
aParent.setVisible(!showParentButton.isSelected());
If you don't have a reference to the child window then I guess you can use the Windows.getOwnedWindows() method to access all the child windows.
Another edit:
As a hack I created a custom dialog that can't be shown again once it is disposed:
final JDialog aChild = new JDialog(aParent)
{
private boolean disposed = false;
#Override
public void dispose()
{
super.dispose();
disposed = true;
}
#Override
public void show()
{
if (disposed)
return;
super.show();
}
};

Autocomplete in Enums for Eclipse?

EDIT: Turns out this is a long-standing bug with Eclipse and enums / array initializers, which is really unfortunate.
In Eclipse, I'm finding that I don't have access to any kind of code completion inside of enums. Here's a WIP, of course, snippet of code that I'm working with.
enum PlayerState implements State<Player> {
NORMAL(){
#Override
public void enter(Player player){
}
#Override
public void update(Player player){
}
},
JUMP_START(){
Timer jumpTimer = new Timer();
public void enter(Player player){
jumpTimer.set(1);
}
public void update(Player player){
}
},
GLOBAL_STATE(){
#Override
public void update(Player player){
}
};
#Override
public void enter(Player player) {
}
#Override
public void exit(Player player) {
}
#Override
public boolean onMessage(Player player, Telegram telegram) {
return false;
}
}
So in this code, I have no completion with "player.", or with "jumpTimer.". To note, I'm working with the GDX-AI library to manage a state machine, and it advises putting the states into an enum, like I have above. Note that I was able to have completion in IntelliJ. I've tried enabling "Java Proposals" in the Preferences/Java/Editor/Content Assist/Advanced section, but that didn't work, and restarting the IDE didn't help, either.
Any ideas?
EDIT: To confirm, this is just a snippet, and the code as a whole does compile. There's nothing wrong with the way it works so far, as far as I can tell; Eclipse isn't throwing any errors, so it's fine. The only issue is code completion, at the moment.
Oh, also, the strange thing is that Eclipse can tell me what's wrong with code (i.e. doing "changeState()" on the state machine from the state through the Player class will have a syntax error because I'm not providing an argument). So, there's some level of code checking, but no code hinting? I dunno...

org.eclipse.ui.startup in use with WindowListener - first activation not caught

Greetings fellow Stackoverflowians,
I am developing an Eclipse RCP application, and I want to add a listener to the ProjectExplorer Eclipse View, and this listener needs to be added before the user does anything, but after the GUI has been generated.
Right on startup, though, the PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null (d'oh, the window isn't activated) so therefore I add to the already created Workbench a WindowListener
PlatformUI.getWorkbench().addWindowListener(new IWindowListener() {
#Override
public void windowActivated(IWorkbenchWindow activatedWindow) {
//do stuff here
}
}
#Override
public void windowClosed(IWorkbenchWindow arg0) {
//remove stuff here
}
#Override
public void windowDeactivated(IWorkbenchWindow arg0) {
// stub
}
#Override
public void windowOpened(IWorkbenchWindow arg0) {
//stub
}
});
Now the problem that I've come across is that even though the ActiveWorkbenchWindow is populated, the windowActivated() method from the WindowListener is not called :(
Funnily enough, when I click on another window, then I click back on the application window, the windowActivated() method is called... therefore the listener was indeed registered.
Any help and suggestions are appreciated!
You could use overrides of the postWindowCreate or postWindowOpen methods of WorkbenchWindowAdvisor to set this up.
I've succeeded in not using the WindowListener anymore on the Workbench, so instead of adding it in the earlyStartup() method of my IStartup implementation, I've done this:
public class StartupHook implements IStartup {
#Override
public void earlyStartup() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
ISelectionListener projectListener = new ProjectSelectionListener();
window.getSelectionService().addSelectionListener(projectListener);
}
}
The trick is that despite there being multiple windows opened on startup, only one is represented, that includes all the views, hence it is reasonable to access: PlatformUI.getWorkbench().getWorkbenchWindows()[0]
Presto, worked around the active window not being in the getActiveWindow() method of the Workbench

Capturing global key presses with java.awt.Toolkit

I found the method addAWTKeyListener in the class Toolkit, but I can't get it to work properly, whether or not the window has focus. My code is as follows:
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
public class KeyTest {
public static void main(String[] args){
Thread t = new Thread(){
#Override
public void run() {
System.out.println("STARTING");
Toolkit kit = Toolkit.getDefaultToolkit();
kit.addAWTEventListener(new AWTEventListener(){
#Override
public void eventDispatched(AWTEvent event) {
System.out.println("EVENT");
if(event instanceof KeyEvent){
KeyEvent kEvent = (KeyEvent) event;
System.out.println(kEvent.getKeyCode());
}
}
}, AWTEvent.KEY_EVENT_MASK);
while(true);
}
};
t.start();
}
}
Is there something I'm doing wrong? I get to the point that STARTING prints and there are no errors. The even is simply not called.
I may be wrong as I'm certainly not an expert, but as far as I know what you're trying to do isn't possible in Java.
Are you trying to capture a key click using a Java program, but without creating a window? Part of Java's security, and this is what I may be wrong on, is that it can only listen to events inside Java windows created by that particular Java program.
So if you were trying to make something key-logger-esque that runs in the background and captured a key press, it wouldn't be able to do that.
I wish I could give you a more concrete answer but I hope this helped.
Just a guess, but you your sample doesn't have any AWT windows in it, so I'm guessing that is why the event never gets fired.
When you say "whether or not the window has focus" does your real app have windows that you have chopped out, or are you talking about a java console window or similar?

Categories