I'm making a server, and it is on a Text Based Raspberry Pi. basically, everything is running from the command line, so when the server runs, there is no graphics, and it prints everything out using System.out.println();. so my question is, instead of having a button that runs a shutdown() method, how can i make it so at any point in time, i am able to push, say, 'e', and the program will run the shutdown() method? i've done some searching, and am not sure quite how to phrase the question. i was thinking adding a keylistener, but im not sure if that can be added to nothing graphic? anyway, any help would be appreciated!!! thanks in advance
To register keyboard events you first need to have the focus on your program, and for that you need a gui. I suggest:
1.- Create a JLabel(and a scrollbar).
2.- Instead of using System.out.print("text");, use myJLabel.append("text" + "/n");.
3.- Add a keyboard listener. Register key events so that the x key closes your server, the s key stops it, ...
As you have described your app, you can't write input to the server, you can only read output from it. I recomend step 4.
4.-Add a JTextFiel to send input to the server.
5.-To make it user-friendly , you could use a JEditorPane instead of a JLabel, and add HTML to your output.
Alright, so based on the comments, (which i up voted btw), i made a thread that constantly used scanner to see if i typed "exit". Thanks for all the help!
If you don't want to have a visible gui, you could create an "always-focused invisible grafical interface" with the listener.
Related
I'm trying to create a game where JADE Agents are the 'enemies' and they chase a player around a maze.
So far I have:
MazeView.java (uses Swing to paint various things on the screen, and lets the user interact through button presses)
Enemy.java (a JADE agent that will have behaviours like search, pursue, etc.)
And a few other classes doing things like generating the actual maze data structure etc.
My problem is that, while I can instantiate an Agent and paint it on the screen, for some reason I can't add any behaviours. For example, if I wanted something like this (in Enemy.java):
protected void setup() {
// Add a TickerBehaviour that does things every 5 seconds
addBehaviour(new TickerBehaviour(this, 5000) {
protected void onTick() {
// This doesn't seem to be happening?
System.out.println("5 second tick... Start new patrol/do something?");
myAgent.addBehaviour(new DoThings());
}
}); // end of addBehaviour
System.out.println("End of setup()...");
} // end of setup
When I run the code, no errors are thrown, and I can see "End of setup()..." displayed in console. So for some reason it's just not going into the addBehaviour() method at all. Even if the DoThings() behaviour didn't work (right now it just prints a message), it should at least display the "5 second tick" message before throwing an error. What is going wrong here?
I think it could be something to do with the fact that currently there is no concept of 'time' in my maze. The user presses a key, and THEN processing happens. So having an agent that does things every 5 seconds might not work when there is no real way to facilitate that in the maze? But I'm still confused as to why it just skips addBehaviour() and I don't get an error.
A possible solution might be to re-implement my maze as a constant loop that waits for input. Would that allow a concept of 'time'? Basically I'm not sure how to link the two together. I am a complete beginner with JADE.
Any ideas would be appreciated. Thanks!
I've never used Jade, but my first thought was that you are adding behaviors and then assuming that Jade will decide to run them at some point. When you say that you never see your behaviors activate, it strengthened that hypothesis.
I looked at the source and sure enough, addBehaviour() and removeBehaviour() simply add and remove from a collection called myScheduler. Looking at the usages, I found a private method called activateAllBehaviours() that looked like it ran the Behaviours. That method is called from the public doWake() on the Agent class.
I would guess that you simply have to call doWake() on your Agent. This is not very apparent from the JavaDoc or the examples. The examples assume that you use the jade.Boot class and simply pass the class name of your agent to that Boot class. This results in the Agent getting added to a container that manages the "waking" and running of your Agents. Since you are running Swing for your GUI, I think that you will have to run your Agents manually, rather than the way that the examples show.
I got more curious, so I wrote my own code to create and run the Jade container. This worked for me:
Properties containerProps = new jade.util.leap.Properties();
containerProps.setProperty(Profile.AGENTS, "annoyer:myTest.MyAgent");
Profile containerProfile = new ProfileImpl(containerProps);
Runtime.instance().setCloseVM(false);
Runtime.instance().createMainContainer(containerProfile);
This automatically creates my agent of type myTest.MyAgent and starts running it. I implemented it similar to your code snippet, and I saw messages every 5 seconds.
I think you'll want to use setCloseVM(false) since your UI can handle closing down the JVM, not the Jade container.
So I wanted to create a GUI for a server to make stuff easier for ppl. I created a Gui with buttons for easy use so you don't have to enter commands all the time. The button works but I didn't find any function that executes a command as the player itself. I googled around but didn't find a solution for this problem.
(btw. this obviously didn't work: player.sendChatToPlayer("/{any command}"); (player is a EntityPlayer))
Regards Jens
Do a Minecraft.getMinecraft().thePlayer.sendChatMessage("/Command here")
In case you want to execute a client-side only command, you can use
ClientCommandHandler.instance.executeCommand(Minecraft.getMinecraft().thePlayer, "/your command");
Is there a way to check if the user is typing in the console window in Java? My program prints information it receives about client connections using System.out.print() and I want it to stop printing information temporarily while the user is typing. User input is read on a separate thread using the Scanner class. I need to be able to see if the user has typed anything and still have whatever the user has typed (if anything) available to the scanner. If it's possible I would like to avoid using external libraries and just stick with the java libraries.
No, there are no ways to do that directly using the console (that I'm aware of) - the content of the console will only get sent to the application once enter is pressed and will then be available to the Scanner.
One way to solve it is to make your own console, that you can read and write from. Then you'll be able to do anything you want really (including check if anything is highlighted and so on). If you don't know how to code a GUI, you should look into that. Oracle has a tutorial on GUI with Swing.
The thread that listens to the user console , Generates an interrupt when the user starts typing, while the other thread that prints the information need to reset the interrupt, all you have to do is implementation of the scenario.
I think you're looking for Thread.sleep(milliseconds);. Using this until the user is done, you should get what you want.
Next time, please elaborate on your questions.
I created a sample Java application. I want to clear the window options, i.e.:
Register
Login
Clear
If the user presses 3 I need to programmatically clear all options. Something like Console.clear?
Is there any way that I can do this with Java?
You will need to output a bunch of blank lines. Even in Windows/*nix, clear/cls doesn't truly clear the screen, it just prints enough blank lines that you cannot see the previous text.
You can try System.out.print("CLS");
Or use loops to clear the screen like
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
and then call this method clearScreen(); if you want to clear.
Sorry my english is bad. :) I just want to help you.
You mean you created a console application and want to clear the console (not necessarily the Eclipse console)?
If so then I guess you're looking for:
Runtime.getRuntime().exec("cls");
But be aware this will be system dependent.
If you are working with the console, then these might prove useful.
Using "backspace" character.
Using process builder.
One of my coworkers would like my Swing app to adapt correctly to the removal of a 2nd display monitor.
Is there any way to get notification of this, other than polling to repeatedly compute virtual bounds? (per the code sample in http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsConfiguration.html)
Hum, tricky one. Because GraphicsConfiguration class won't give us any listeners, I'll have only a couple of alternatives:
(If Windows) Use a JNI interface to
Windows to detect display settings
change and forward them to Java.
This would be the
SystemEvents::DisplaySettingsChanged
Event.
Create a simple polling Thread -
timer that retrieves the result of
Toolkit.getDefaultToolkit().getScreenSize()
as you've already stated before.