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.
Related
I am making an Oscilloscope using JavaFX for making the GUI. The task is to read the values from USB (between 0 and 1023) and show it graphically. I have built the GUI from what I have learnt from tutorials. My problem is how can I constantly and simultaneously update the GUI and also keep reading data from the usb?
Many other questions like this have been answered on Stackoverflow, but the answers provide a button which causes changes. Which is not in my case.
This should be fairly simple. If your API for reading the values from USB is synchronous you have to place the relevant call in a separate thread. If the API is asynchronous (uses some kind of callback to notify you about new values) then you can use that directly. Once you have received new values either way you can just update your GUI in a Platform.runLater call.
The following simple snippet grabs all the available screen devices in Java no problem:
GraphicsDevice[] gds = ge.getScreenDevices();
That much is simple. However, I'd like to get notified when this changes (an existing screen changes, one is removed, a new one is added, etc.)
I could just poll on the above method call which is do-able, but ideally I'd like a notification (observer pattern style) when something changes. I can't see a way in the API to hook onto such an event though.
Is this possible, either through plain JDK or an add-on library, or should I just resort to polling every other second or so?
From all the looking around I've done, the answer at present (looking at Java 7 and the currently proposed Java 8 APIs) is simply no - you just have to poll it seems.
Although this is nasty from an OO perspective from a performance perspective it doesn't really make a difference as long as you're sensible with intervals - it seems to be a very cheap operation. My approach was to do the polling in a separate class and provide an observer pattern on which others could register. Does the job well, all the polling is in one place and if a better API comes along later, it'd be trivial to swap out with the new interface.
I've developed an interface that allows a user to load and manipulate data. The GUI is developed in Java and all the computational stuff is done in the background by R, linking the two with jri. The idea is that the user doesn't have to have any knowledge of R to use it, it's all options and buttons. However, i'd like to give the user the option to write some code if necessary. So here is my problem:
If I use the following code to start the Rengine and not let the user interact via console, everything works fine:
Rengine re=new Rengine(null, false, new TextConsole());
But if I use this:
Rengine re=new Rengine(null, true, new TextConsole());
The functionality of the gui doesnt work. I tried using the
re.startMainLoop();
function after the data was loaded. I was able to manipulate the data from the comand line in R, for example I could make a new variable from a column of the data loaded:
newVariable<-data$column1
But yet again, I couldn't use the gui anymore. Has anyone got any ideas or explainations as to why this is?
Thanks in advance,
Aran
Fundamentally, if REPL is not running, R is simply used via eval calls from your code. You have control at all times, except during the actual evaluation. That is the most common use, because you can do pretty much anything that way.
The moment you enable the event loop (REPL), you have to implement the callback methods that are used by the loop. By design R surrenders the control only by calling the rReadConsole callback which you have to implement. The example TextConsole works only as a demo, it uses blocking call (readLine()) to wait so you definitely don't want to use that in your GUI. You'll have to implement all callbacks correspondingly to react to your GUI's elements (wait in ReadConsole for your GUI to wake it up from a separate thread, dispatch WriteConsole to your elements etc.). You can have a look at JGR how it's done properly. Unless you are really building a general purpose R GUI, I wouldn't go into that trouble ...
(PS: please use stats-rosuda-devel mailing list for rJava/JRI questions - you get answers much faster)
how can I add a listener to the computer's processor? I want the mouse cursor to change when the processing is hight.
You are going to have to drop to native code to get the CPU info. Here is an article on doing that.
You would have to write a class that then makes use of that native call and provides the API you want for the listener.
Edit:
Hmmm... you might also check out http://support.hyperic.com/display/SIGAR/Home.
This question sort of extends my other question on robots and captcha. I did what everyone recommend (thanks everyone!), however is it at all possible to detect a robot on the server first? For Example (Once again, I will use Stackoverflow as a reference): Sometimes when I ask a question, Stackoverflow comes back asking me to verify if I am human.
However, sometimes it does not.
How does Stackoverflow do that, because that is what I want to do: Check data and if it looks like a robot, request human verification.
Also this needs to be done on Java (preferably), Perl or PHP.
Thanks
On StackOverflow, it's done by performing the same task too many times too quickly or performing multiple tasks too quickly.
If you want to emulate this, you can keep track of the number and time(s) of recent requests and check to see that everything is within your limits. If it isn't, redirect to a CAPTCHA.
Unfortunately, I don't have enough Java EE experience to provide any code, but hopefully my approach will give you some idea(s).
The simple method would be to log activity (clicks, comments, ect.) and then check the frequency and similarity between these. You can usually detect robots by looking for similar tasks performed repeatedly.
If you are really serious about robot detection, log every keystroke and mouse movements. Regular users have a percentage of error and uncertainty associated with typing and navigating the site. A 100% typo free user that navigates the site easily and quickly (moving the mouse on a straight line from point a to point b) without ever going for the back button is very likely to be a bot.