My application works on tablet (with Windows 7) which sometimes turns off its display in order to save battery. After touching the screen, it turns on again.
Application should remain operational even after turning off the screen, what is, in general, accomplished (i.e. sounds are played). The only problem is the fact that when screen is turned off and application tries to add new component to the scene (technically - add children to the displayed GridPane), the newly added component is not visible when I touch the screen (and it turns on).
It is just like there was a cache which stores and restores displayed content when screen is turning on/off. After turning screen on - when I click windows button (keyboard) or or change focus to another application - newly added component appears.
I have tried setting -Dprism.dirtyopts=false but it did not change anything.
My Java is 7u67.
Is it known bug? Is there any programming workaround? I have tought about using WinAPI to force redraw/refresh of the application window but it seems too big for this case.
I have just figured out "simple" workaround:
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.IntByReference;
/** WINAPI CONSTANTS **/
private static final int RDW_UPDATENOW = 0x0100;
/**
* Redraws all windows associated with currently running process.
*/
public static void redrawApplicationWindows() {
final int processId = Kernel32.INSTANCE.GetCurrentProcessId();
User32.INSTANCE.EnumWindows(
new WinUser.WNDENUMPROC() {
#Override
public boolean callback(WinDef.HWND hwnd, Pointer pointer) {
IntByReference someProcessId = new IntByReference();
User32.INSTANCE.GetWindowThreadProcessId(hwnd, someProcessId);
if (someProcessId.getValue() == processId) {
User32.INSTANCE.RedrawWindow(hwnd, null, null, new User32.DWORD(RDW_UPDATENOW));
}
return true;
}
}, Pointer.NULL);
}
It requires you to have jna and jna-platform in dependencies.
This method should be called after drawing when the screen is turned off (it is another problem how to detect it). As my application rarely draws automatically (without user input), I have placed its call after every drawing happening automatically.
It works in Win7.
Related
I'm currently working on a stock market program that teaches students how to interact with the stock market. An issue that I'm working on right now revolves around the issue of efficiency and memory. I make all of my 2D icons (such as a settings icon, portfolio icons, etc) in adobe illustrator, I export those files as png files, and throw them into my program. I currently use JavaFX, and one of the features of JavaFX is what's called imageView, which is a method that handles the opening and viewing of images.
So let's say that the user would like to press on the Settings icon in the game, I would like to change the settings icon to a darker or lighter color when the user hovers over that icon in order to provide the user with a better user experience (UX) at the moment, I use two different images and remove one from the frame, and replace it with another, which is highly inefficient.
I know that JavaFX has a Shape class that inherits many methods such as Fill, or setFill, but these methods can only affect those of the Shape class.
My question is, "How can I convert an image that is imported into the project, into something that I can use methods such as setFill and Fill on"
If you're only aiming for basic changes such as darkening or lightning your icons, you can look at the Effects part of javaFx, you can read about it here, or else you can import your images as SVG as suggested in the comments
If you're planning to do it using Effects, you can achieve a darken-on-hover effect using the ColorAdjust Effect by setting the brightness value to something negative (the brightness in ColorAdjust ranges between -1 and +1 where 0 is the default) as in the following example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage s) {
ImageView image = new ImageView(new Image("img.png"));
ColorAdjust darker = new ColorAdjust();
darker.setBrightness(-.3);
image.setOnMouseEntered(e-> {
image.setEffect(darker);
});
image.setOnMouseExited(e-> {
image.setEffect(null);
});
s.setScene(new Scene(new StackPane(image)));
s.show();
}
public static void main(String[] args) {
launch(args);
}
}
Changing the color of an image might involve adjusting the hue using ColorAdjust by setting the hue value
ColorAdjust hueShift = new ColorAdjust();
hueShift.setHue(-.3);
image.setOnMouseEntered(e-> {
image.setEffect(hueShift);
});
image.setOnMouseExited(e-> {
image.setEffect(null);
});
You can combine effects by setting effects as input to other effects, so for example, if you want to darken a Node and blur it at the same time you can set the blur effect as input to the darkening colorAdjust
GaussianBlur blur = new GaussianBlur();
blur.setRadius(10);
ColorAdjust darker = new ColorAdjust();
darker.setBrightness(-.3);
darker.setInput(blur);
image.setOnMouseEntered(e-> {
image.setEffect(darker);
});
image.setOnMouseExited(e-> {
image.setEffect(null);
});
I am trying to create a simple menu for a small project using a JSpinner and JOptionPane. I created my desired output, but when I interact with the window or even hover over the buttons in the box, it creates visual artifacts over and over again (see images below):
JOptionPane before mouse hover
JOptionPane after mouse hover
I did some researching and thought it might be due to JOptionPane not being thread safe, but could not get anything to work.
Overall, my specific question is how do I prevent Java from repainting these visual artifacts over my JOptionPane window?
For reference, please see the method I am using to show this menu:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
public class Battleship
{
public static void main(String[] args)
{
SpinnerNumberModel battleshipRange = new SpinnerNumberModel(1, 1, 5, 1);
JSpinner rangeSpinner = new JSpinner(battleshipRange);
JOptionPane.showMessageDialog(null, rangeSpinner, "Battleship Number", JOptionPane.QUESTION_MESSAGE);
}
}
I am running this code on BlueJ and am using Windows 10 Pro.
Thank you in advance and apologies if this is a beginner question. I am still fairly new to programming.
EDIT: Updated code to give complete source of problem, but it disappeared. I will keep an eye on it to see where the source of the error first occurred.
After completing my project, I finally found why visual artifacts would pop up in all buttons/radio buttons/etc. in my game.
In the GridWorld source code, under the "GridPanel.java" script, the original programmers created a method called "setToolTipsEnabled(boolean flag)". Its function is to pop up a message box next to the cursor when it is hovering over the grid when the game stops running.
When extending GridWorld to create my project, the method reaches past the grid structure and attempts to create a tool tip underneath anything the cursor hovers over. Thus, creating visual artifacts on buttons/radio buttons/etc.
To fix this, I made sure this method was always set to false as I did not need tool tips for my game anyway. This method was implemented in the "GridPanel.java" and "GUIController.java" scripts (in the GridWorld code). I changed following methods to fix this problem:
GridPanel.java
/**
* Construct a new GridPanel object with no grid. The view will be
* empty.
*/
public GridPanel(DisplayMap map, ResourceBundle res)
{
displayMap = map;
resources = res;
// Phillip Sturtevant: Commented out to prevent visual artifacts
//setToolTipsEnabled(true);
}
GUIController.java
/**
* Stops any existing timer currently carrying out steps.
* Phillip Sturtevant Note: keep tool tips hidden to prevent visual artifacts
*/
public void stop()
{
display.setToolTipsEnabled(false); // hide tool tips while stopped
timer.stop();
running = false;
}
Alternatively, the method calls could be omitted entirely, but I commented them out in case I needed them in the future.
For reference, the method below sets the tool tips visibility in GridWorld (located in "GridPanel.java"):
/**
* Enables/disables showing of tooltip giving information about the
* occupant beneath the mouse.
* #param flag true/false to enable/disable tool tips
*/
public void setToolTipsEnabled(boolean flag)
{
if ("hide".equals(System.getProperty("gridworld.gui.tooltips")))
flag = false;
if (flag)
ToolTipManager.sharedInstance().registerComponent(this);
else
ToolTipManager.sharedInstance().unregisterComponent(this);
toolTipsEnabled = flag;
}
Below is the code sample, with other features left out. The code below encompasses the media player only. It's used on a menu screen for a project I'm working on. My issue is getting the musicButton (which is a toggle button- On/Off) to work properly. Using the following code, when I interact with the music toggle button, the playing music stops. When I click it again to resume playing, it does not resume. It stops once and stops altogether.
You can see I've tried simply using the boolean values of the toggle button in two if statements... If it's off and pressed, pause the music. If its on and pressed, resume the music. The problem is, as stated earlier, pausing the music works but it cannot be resumed. I've tried some combinations with loops, but nothing worked either.
I think if statements are too simple for this. I've scoured the JavaDocs and various online articles but I cannot find anything definitive. I've read a little about listeners, but they seem overly-complex for an on/off switch.
My question:
How do I get the musicButton to pause/play the music, whenver the user clicks it?
Any help is appreciated, thanks.
-Bagger
/* A simple game, the mechanics not yet implemented.
This is simply working on the title screen. */
import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class MenuFX extends Application {
#Override
public void start (Stage primaryStage) {
// Make the window a set size...
primaryStage.setResizable(false);
// Create media player
// Rather than inputting the entire absolute URI, which would confine the program
// to the creator's device, we create a new file, grab the URI on whatever machine
// the program is running on and convert it to a string... portability.
Media menuMusic = new Media(new File("music/menu.mp3").toURI().toString());
MediaPlayer menuPlayer = new MediaPlayer(menuMusic);
// Want to see the absolute URI? Uncomment the next line
//System.out.println(new File("music/menu.mp3").toURI().toString());
// Adjust the cycles and volume then start playing menu music
// Lazy, but it will suffice
menuPlayer.setCycleCount(999999999);
menuPlayer.setVolume(0.1);
menuPlayer.setAutoPlay(true);
/*
Need assistance here
*/
// Create music toggle button
ToggleButton musicButton = new ToggleButton("Music On/Off");
if (musicButton.isSelected() == false) {
musicButton.setOnAction(e -> menuPlayer.pause());
}
if (musicButton.isSelected() == true) {
musicButton.setOnAction(e -> menuPlayer.play());
}
// Add all nodes to the vbox pane and center it all
// Must be in order from top to bottom
menuVBox.getChildren().add(musicButton);
menuVBox.setAlignment(Pos.CENTER);
// New scene, place pane in it
Scene scene = new Scene(menuVBox, 630, 730);
// Place scene in stage
primaryStage.setTitle("-tiles-");
primaryStage.setScene(scene);
primaryStage.show();
}
// Needed to run JavaFX w/o the use of the command line
public static void main(String[] args) {
launch(args);
}
}
I think you are over thinking it. You should have an EventListener on your ToggleButton to Pause and Play the music.
musicButton.setOnAction(event -> {
if (musicButton.isSelected()) {
menuPlayer.pause();
}else {
menuPlayer.play();
}
});
This should give you the desired effect.
The reason your code was not working is because the ToggleButton is not selected by default, so the only EventListener that gets associated with it is the menuPlayer.pause();. So when you click on it, it only ever pauses. I have moved your code into one EventListener, and used the approriate if-else.
I'd like to know how I can code a Java program that knows which Windows application is in focus. I can have many windows open but I want to know the one that's being used (like Google Chrome right now as I'm typing this).
I don't need to change anything in the window or application, just need to know its name.
As the others have already pointer out, there is no portable way to get this on all platforms. But to make things worse: There not even a consistent way on MS Windows. I will provide some code that will solve the problem for different platforms and will point out the limitations. Use at your own risk, the code may provide wrong results or not run at all because of security reasons. If it runs on your machine, it will not mean that it will run equally well on other machines.
The code uses JNA. During my experiments I had problems with different versions of JNA and the JNA platform library. It might be best to compile it yourself, so you have a consistent environment.
Windows
The answer provided by kichik was correct at its time but will not work with Windows 8 in all cases. The problem is, that it will not handle Metro apps correctly. Unfortunately there is currently no stable API to get the name of the currently running Metro app. I have inserted some hints in the code, but it's best to wait until Microsoft will provide you with an API.
On Windows you will also have problems with privileged apps and with the UAC dialog. So you will not always get a correct answer.
public interface Psapi extends StdCallLibrary {
Psapi INSTANCE = (Psapi) Native.loadLibrary("Psapi", Psapi.class);
WinDef.DWORD GetModuleBaseNameW(Pointer hProcess, Pointer hModule, byte[] lpBaseName, int nSize);
}
if (Platform.isWindows()) {
final int PROCESS_VM_READ=0x0010;
final int PROCESS_QUERY_INFORMATION=0x0400;
final User32 user32 = User32.INSTANCE;
final Kernel32 kernel32=Kernel32.INSTANCE;
final Psapi psapi = Psapi.INSTANCE;
WinDef.HWND windowHandle=user32.GetForegroundWindow();
IntByReference pid= new IntByReference();
user32.GetWindowThreadProcessId(windowHandle, pid);
WinNT.HANDLE processHandle=kernel32.OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, true, pid.getValue());
byte[] filename = new byte[512];
Psapi.INSTANCE.GetModuleBaseNameW(processHandle.getPointer(), Pointer.NULL, filename, filename.length);
String name=new String(filename);
System.out.println(name);
if (name.endsWith("wwahost.exe")) { // Metro App
// There is no stable API to get the current Metro app
// But you can guestimate the name form the current directory of the process
// To query this, see:
// http://stackoverflow.com/questions/16110936/read-other-process-current-directory-in-c-sharp
}
Linux / Unix / X11
With X11 we have three problems:
Because of network transparency, multiple windows from completely different machines might be mixed in the same X11. So neither name nor PID of the process belonging to a window might be make sense on the machine you are querying.
Most windows managers have mutliple desktops. On each desktop there can be a different application in the foreground
Tiling window managers (like XMonad) don't have the concept of a foreground window. They arrange all windows in a way, so each window is in the foreground at the same time.
On X11 it makes more sense to query for the window that currently has the focus.
public interface XLib extends StdCallLibrary {
XLib INSTANCE = (XLib) Native.loadLibrary("XLib", Psapi.class);
int XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
}
if(Platform.isLinux()) { // Possibly most of the Unix systems will work here too, e.g. FreeBSD
final X11 x11 = X11.INSTANCE;
final XLib xlib= XLib.INSTANCE;
X11.Display display = x11.XOpenDisplay(null);
X11.Window window=new X11.Window();
xlib.XGetInputFocus(display, window,Pointer.NULL);
X11.XTextProperty name=new X11.XTextProperty();
x11.XGetWMName(display, window, name);
System.out.println(name.toString());
}
Mac OS X
Mac OS X does not focus on windows but on applications. So it makes sense to ask for the currently active application. Older versions of Mac OS X provide multiple desktops. Newer versions can have multiple fullscreen applications open at the same time. So you might not always get a correct answer.
if(Platform.isMac()) {
final String script="tell application \"System Events\"\n" +
"\tname of application processes whose frontmost is tru\n" +
"end";
ScriptEngine appleScript=new ScriptEngineManager().getEngineByName("AppleScript");
String result=(String)appleScript.eval(script);
System.out.println(result);
}
Conclusion
When I played around with this code, it worked in the most basic cases. But if you want this code to run reliable, you will have to put in a lot of polish. Decide for yourself if it is worth it.
To make the code complete, here is the import section I used:
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
Of course you will have to rearrange the parts of the code. I used one big class with the interfaces at the beginning a and then the rest in one big main method.
I'm afraid there's no java api for that. JVM does not know anything about the windows it does not manage. You'll probably have to use JNI and call this function
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
MSDN link
PS. THere is a GetWindowText function that you might want to use if you need to grab the title of the window.
This post has JNI examples that might be helpful for you.
As Hovercraft Full Of Eels said, JNA is your best bet here. Unlike JNI, you won't have to compile any C code for it.
To get the process name:
Call GetForegroundWindow() to get the window handle
Call GetWindowThreadProcessId() to figure out which process owns it
Call OpenProcess() to get a handle to the process (with PROCESS_QUERY_INFORMATION | PROCESS_VM_READ)
Call GetModuleFileNameEx() to get the process name from the handle. You can also call GetModuleBaseName() for just the module name without the full path.
A full example is available in Getting active window information in Java.
C code can be found here.
How do I capture the mouse in a Java application so that all mouse events (even ones that happen if the mouse is moved outside the app window) are seen by the Java app? This is like the Windows SetCapture function.
You don't; the JVM, or more specifically AWT, only generates input events when Windows sends it input events, and the JVM only registers for those events which occur within it's window.
You might be able to pull it off using JNI, but then again you might not - it will depend if you can get your hands on the information required by the underlying API. Since that's likely to be a window handle, you won't have what you need to invoke the API, even from JNI.
You have to hook the mouse at the operating system level. Windows(Swing, AWT, MFC, etc....) are only aware of mouse movements within their bounds. If you need a way to access the current position of the mouse regardless of where the mouse is on the screen, you need to write an Input Hook: Input Hooks. You can then use JNI or read the STDOUT from a win32 console application designed to use the Input Hook to forward mouse events/positions to your Java code. I use the latter method in some of my user interface test cases with success.
I needed to do that too!
I after searching the web I found that its possible to use the moveMouse in java.awt.Robot.
Basically use Robot to move the mouse into center of your frame. If user moves it: check how much and move it back to center.
No additional packets or JNI are needed for this (my demo uses JOGL and vecmath but that's for the graphics). Is it good enough? Try the demo, its here:
http://www.eit.se/hb/misc/java/examples/FirstPersonJavaProtoGame/
If the above solution is not good enough then perhaps lwjgl is what you need:
http://www.lwjgl.org/javadoc/org/lwjgl/input/Mouse.html
/Henrik Björkman
Just use the system-hook library available on gitHub https://github.com/kristian/system-hook
This only apply to windows-based systems but really simple to implement.
Sample usage
import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;
public class GlobalKeyboardExample {
private static boolean run = true;
public static void main(String[] args) {
// might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();
System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown.");
keyboardHook.addKeyListener(new GlobalKeyAdapter() {
#Override public void keyPressed(GlobalKeyEvent event) {
System.out.println(event);
if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
run = false;
}
#Override public void keyReleased(GlobalKeyEvent event) {
System.out.println(event); }
});
try {
while(run) Thread.sleep(128);
} catch(InterruptedException e) { /* nothing to do here */ }
finally { keyboardHook.shutdownHook(); }
}
}