This utility application takes a screenshot of multiple monitors by pressing a button on a JFrame. The intended logic of this method is as follows:
Create new Rectangle to represent a union of all the monitors' boundsm
Use InvokeAndWait to hide to JFrame, and ensure it is hidden before processing any further
Take a screenshot using the Robot class
Set the frame to visible again and return the image
Even with the final "return frame to visible" step commented out of the code, leaving me seeing no frame on the screen after execution, the frame is visible in the screenshot. I do not know why.
Using print statements, I have determined that the firing of the method to turn the JFrame invisible does run before taking the screenshot. I also attempted to use an if statement to check if the JFrame was visible before taking the screenshot, and the if statement was never triggered.
What is the solution to this?
public Image capture(ArrayList<Monitor> monitors) {
Rectangle bounds = new Rectangle();
monitors.stream().forEach(a -> Rectangle.union(bounds, a.getBounds(), bounds) );
Image image = null;
try {
EventQueue.invokeAndWait(() -> {
frame.setVisible(false);
System.out.println("Set frame invisible");
});
} catch (Exception ex) {
ex.printStackTrace();
}
try {
image = new Image(new Robot().createScreenCapture(bounds));
} catch (Exception ex) {
ex.printStackTrace();
}
//frame.setVisible(true);
return image;
}
This is not a definitive answer, but maybe it'll point you in the right direction.
I had a similar problem requiring a JFrame to go fullscreen on a 3D application using OpenGL.
What I assume is happening is that frame.setVisible() talks to the OS window manager requesting the window be hidden (or shown). Once the request is made and acknowledged, the method call is no longer blocking, and the thread in invokeAndWait() is now done invoking and waiting.
Your code proceeds to a take a screenshot, but the operating system is not guaranteed to have actually processed the request to minimize the Window. In your case, it appears that is doesn't.
The Solution (maybe?):
It looks like Java has a class called a WindowEvent doc here. You should be able to create a listener and/or a loop before the screenshot call that waits for a status update. Note the doc specifically says:
The window-deactivated event type. This event is delivered when the Window is no longer the active Window. Only a Frame or a Dialog can be the active Window. The native windowing system may denote the active Window or its children with special decorations, such as a highlighted title bar. The active Window is always either the focused Window, or the first Frame or Dialog that is an owner of the focused Window.
I suspect waiting for the WINDOW_DEACTIVATED and/or WINDOW_LOST_FOCUS could be the actual indicator of if the window has been minimized by the OS window manager.
Again, I'm basing this off of a mostly unrelated project I worked on several months ago, but hopefully some of this will help.
UPDATE
OP Implemented a Solution as follows:
public Image capture(ArrayList<Monitor> monitors)
{
Rectangle bounds = new Rectangle();
monitors.stream().forEach(a -> Rectangle.union(bounds, a.getBounds(), bounds) );
Image image = null;
try
{
EventQueue.invokeAndWait(() -> frame.setExtendedState(Frame.ICONIFIED));
while (frame.getExtendedState() != Frame.ICONIFIED) { }
image = new Image(new Robot().createScreenCapture(bounds));
}
catch (Exception ex)
{
ex.printStackTrace();
}
EventQueue.invokeLater(() -> frame.setExtendedState(Frame.NORMAL));
return image;
}
Related
I am trying to make a simple event driven TicTacToe game using JavaFX. Currently I am struggling with termination once certain conditions are met. To put it into more details, the player can click on the GridPane elements, which are Canvas Objects, and then they are filled with "X" or "O" shapes respectively (I am using strokeLine and strokeOval methods of GraphicsContext). Code below:
private static void placeX(Canvas square){
GraphicsContext gc = square.getGraphicsContext2D();
gc.setLineWidth(10.0f);
gc.setStroke(Color.CORNFLOWERBLUE);
gc.strokeLine(square.getWidth()*0.2, square.getHeight()*0.2, square.getWidth()*0.8, square.getHeight()*0.8);
gc.strokeLine(square.getWidth()*0.8, square.getHeight()*0.2, square.getWidth()*0.2, square.getHeight()*0.8);
}
Once 3 of the same shapes appear in line or diagonally the program should terminate. I am doing this using Platform.exit(). Code below:
class HandleGame implements EventHandler<MouseEvent>{
#Override
public void handle(MouseEvent e){
Canvas can = (Canvas)e.getTarget();
//function to check if the canvas is clear
placeX(can);
if(game.isEnded()){ //checks if the same shape appears three times
Platform.runLater(new Runnable() {
#Override
public void run(){
try {
Thread.sleep(5000);
}
catch(InterruptedException exc){
System.out.println("Got something: " + exc.getMessage());
}
Platform.exit();
}
});
}
}
}
This event handler is attached to every Canvas object in GridPane and triggers on mouse release. The problem I am having is that once the last Canvas is clicked, before the shape appears on the Canvas the specified Runnable is executed and the rendering is unnaturally delayed (the "X" shape appears only for a second before closing). Strangely enough 1 out of 10 runs it executes as expected. How can I make the rendering trigger before the Thread.sleep() and following Platform.exit()? Why on rare occasions the rendering is actually performed before Thread.sleep()? I did a little research but could not find anything decisive, I am newbie when it comes to JavaFx. Appreciate your help.
Based on #Slaw and #VGR comments I managed to solve the problem using two different methods, one utilizing PauseTransition and second Thread. Code below:
Using PauseTransition:
if(game.isEnded()){
PauseTransition termination = new PauseTransition(Duration.seconds(1d));
termination.setOnFinished(event -> Platform.exit());
termination.play();
}
Using Thread:
if(game.isEnded()){
new Thread(() -> {
try{
Thread.sleep(2000);
}
catch(InterruptedException exc){
; //exception handling code here
}
Platform.exit();
}).start();*/
I would like to know how to get a transparent JFrame in the latest version of Java.
Currently, you can only use
<JFrame>.setOpacity();
if the frame is not decorated.
I have no use for an undecorated frame, so I'd like to know how to go around this restriction and set the opacity of the frame to 0.5f while still keeping the title bar, resize options etc.
I have read the docs here: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html. The code only worked on Java 6 and no longer runs. The error, as I said, is:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at TranslucentWindowDemo$1.run(TranslucentWindowDemo.java:53)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
...
I have also tried setting the background (setBackground : Color) using a Color with custom Alpha value (new Color(int, int, int, Alpha)) but it throws the exact same error.
Setting the transaprency of a JPanel this way won't work, as it will still lay on the JFrame, which is not transparent.
I could find no other answer on Stack Overflow that correctly addressed this issue. In fact, a few suggested that this could be fixed with:
JFrame.setDefaultLookAndFeelDecorated(true);
But they were misinformed of perhaps referring to Java 7, as I have tested it and the result is just the same.
I have also tried to manually set the Look And Feel:
try {
for (final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch [...]
And combining this with the solution suggested above also did not work.
Please refer to the code over to the example I linked above (Oracle doc) for a MCVE, as that is the one I'm using.
Any way around this?
As far as I can tell, the basic answer is: it is not possible, at least with the System look and feel. As indicated in Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?, the JavaDocs clearly indicate that “the window must be undecorated” for setOpacity() to work.
It is however possible to do it with the (ugly) Cross-platform look and feel, that you can progrmmatically set as follows:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
In fact, as the cross-platform look and feel could be overridden through configuration, the safest would actually be to set it explicitly to Metal as follows:
UIManager.setLookAndFeel(new MetalLookAndFeel());
The reason this works, is that the JDK implementation of Frame.setOpacity() throws an exception when !isUndecorated(), and JFrame.frameInit() sets itself as undecorated when the look and feel's getSupportsWindowDecorations() returns true. It then calls getRootPane().setWindowDecorationStyle() with JRootPane.FRAME, indicating that the decorations will be provided by the root pane instead of the frame.
From what I can see in the JDK, the Metal look and feel is the only one for which getSupportsWindowDecorations() returns true, as it is the only one which overrides it, and the default implementation simply returns false.
However, some third-party look and feels support it too. This is the case for instance for the Tiny Look and Feel, as I just tried:
(Note that I took this screenshot on Ubuntu, TinyLAF just so happens to have a default theme that looks like Windows XP!)
See also this question for a list of known third-party look and feels.
Try adding this line before creating the JFrame window:
JFrame.setDefaultLookAndFeelDecorated(true);
Exactly that line, don't replace JFrame in the beggining, it needs to be JFrame.
(You can also spot this line in the tutorials you mentioned precicely placed before creating the window).
Actually this is possible, using the dirty solution of reflect. If we dig into setOpacity method (which is inherited from java.awt.Frame class) we will see the following code:
#Override
public void setOpacity(float opacity) {
synchronized (getTreeLock()) {
if ((opacity < 1.0f) && !isUndecorated()) {
throw new IllegalComponentStateException("The frame is decorated");
}
super.setOpacity(opacity);
}
}
where isUndecorated is a simple getter to the field named undecorated (inside java.awt.Frame class).
Changing the value of this field will do the trick and this exception won't be thrown.
Check this example i have made:
public class JFrameOpacity {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
setSystemLookAndFeel();
JFrame frame = new JFrame("Opacity to decorated Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JButton decreaseOpacity = new JButton("Reduce Opacity");
decreaseOpacity.addActionListener(e -> {
if (frame.getOpacity() - 0.1f <= 0.1f)
frame.setOpacity(0.1f);
else
frame.setOpacity(frame.getOpacity() - 0.1f);
});
frame.add(decreaseOpacity);
JButton increaseOpacity = new JButton("Increase Opacity");
increaseOpacity.addActionListener(e -> {
if (frame.getOpacity() + 0.1f >= 1f)
frame.setOpacity(1f);
else
frame.setOpacity(frame.getOpacity() + 0.1f);
});
frame.add(increaseOpacity);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
try {
undecorate(frame); //Change it after frame is visible
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
}
private static void undecorate(Frame frame) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field undecoratedField = Frame.class.getDeclaredField("undecorated");
undecoratedField.setAccessible(true);
undecoratedField.set(frame, true);
}
private static void setSystemLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}
Preview:
I have tested this in The Microsoft Windows Look and Feel (Windows 7 x64) and it works. Note the comment i added when i call undecorate method. I made some tests on that and i realized that if you undecorate the frame before it gets at least one time visible, when you will make it visible it will be undecorated - it will not have this title bar and stuff.
I am not sure though if this is going to give other problems to the application but you can always change the value of the field, change its opacity and the set it back.
I just encountered a problem when trying to add an imprint to a swing application which is shown for five seconds when the application is closed.
I had planned to open a JDialog just containing a simple image when the main frame is closed.
I got a function showing the JDialog (I removed everything which is not necessary).
public static void show() {
JDialog d = new JDialog();
JLabel l = new JLabel(new ImageIcon(MainController.class.getClass().getResource("/path/to/endlogo.png")));
d.add(l);
d.setVisible(true);
}
The function is called by the following snippet (in the window listener of my main window)
#Override
public void windowClosing(WindowEvent e) {
show();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The problem is that the dialog is shown AFTER the five seconds (which is when the windows is already closed and the application exited, I tested it and it showed up perfectly after five seconds when run on application start).
Do you guys know a way to achieve it the other way round?
You're blocking the UI thread. Don't do that, basically... use a swing timer or something similar if you want to do something on the UI thread at a later time.
Golden rules:
Don't do anything time consuming on the UI thread
Only access the UI on the UI thread
I currently have a JFrame where on it's content pane I draw images on from a game loop at 60 frames per second. This works fine, but at the right side, I now have more Swing elements on which I want to display some info on when selecting certain parts of the content pane. That part is a static GUI and does not make use of a game loop.
I'm updating it this way:
public class InfoPanel extends JPanel implements Runnable {
private String titelType = "type: ";
private String type;
private JLabel typeLabel;
private ImageIcon icon;
public void update() {
if (this.icon != null)
this.typeLabel.setIcon(this.icon);
if(this.type != null || this.type != "")
this.typeLabel.setText(this.titelType + this.type);
else
this.typeLabel.setText("");
}
public void run() {
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.update();
}
(this method is only called when the player has actually moved, so it's just called once - not 60 times per second)
I noticed that, when calling this update()-method from the game loop, I get flickering effects. I assume this is because updating the UI takes some time, so I decided to put it in a new thread. This reduced the flickering, but didn't solve it.
Next, I decided to give the new thread low priority as the part of the screen which is redrawed 60 times a second is far more important. This reduced the flickering again, but it still happened. Then, I decided to use Thread.sleep(150); in the new thread before calling the update()-method, which solved the flickering effect on my system completely.
However, when running it on other systems, it still happens. Not as often as before (maybe one time per 20 seconds), but it's still pretty annoying. Apparantly, just updating the UI in another thread doesn't solve the problem.
Any ideas how to completely eleminate the flickering?
Call the update() in SwingUtilities.invokeAndWait() which stops the thread and updates UI in EDT.
Problem is that you are use Thread.sleep(int), that stop and freeze GUI during EventDispatchTread more in the Concurency in Swing, example demonstrating freeze GUI by using Thread.sleep(int), example for Runnable#Thread
If you want to delay whatever in Swing then the best way is implements javax.swing.Timer
I have to write an applet that brings up a password dialog. The problem is that dialog is set to be always on top but when user clicks on IE window dialog gets hidden behind IE window nevertheless. And since dialog is modal and holds all IE threads IE pane does not refresh and dialog window is still painted on top of IE (but not refreshed). This behaviour confuses users (they see dialog on top of IE but it looks like it has hanged since it is not refreshe).
So I need a way to keep that dialog on top of everything. But any other solution to this problem would be nice.
Here's the code:
PassDialog dialog = new PassDialog(parent);
/* do some non gui related initialization */
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
Resolution: As #shemnon noted I should make a window instead of (null, Frame, Applet) parent of modal dialog. So good way to initlialize parent was:
parent = javax.swing.SwingUtilities.getWindowAncestor(theApplet);
What argument are you using for the parent?
You may have better luck if you use the parent of the Applet.
javax.swing.SwingUtilities.getWindowAncestor(theApplet)
Using the getWindowAncestor will skip the applet parents (getRoot(component) will return applets). In at least some versions of Java there was a Frame that was equivalent to the IE window. YMMV.
Make a background Thread that calls toFront on the Dialog every 2 seconds.
Code that we use (I hope I got everything):
class TestClass {
protected void toFrontTimer(JFrame frame) {
try {
bringToFrontTimer = new java.util.Timer();
bringToFrontTask = new BringToFrontTask(frame);
bringToFrontTimer.schedule( bringToFrontTask, 300, 300);
} catch (Throwable t) {
t.printStackTrace();
}
}
class BringToFrontTask extends TimerTask {
private Frame frame;
public BringToFrontTask(Frame frame) {
this.frame = frame;
}
public void run()
{
if(count < 2) {
frame.toFront();
} else {
cancel();
}
count ++;
}
private int count = 0;
}
public void cleanup() {
if(bringToFrontTask != null) {
bringToFrontTask.cancel();
bringToFrontTask = null;
}
if(bringToFrontTimer != null) {
bringToFrontTimer = null;
}
}
java.util.Timer bringToFrontTimer = null;
java.util.TimerTask bringToFrontTask = null;
}
This is a shot in the dark as I'm not familiar with applets, but you could take a look at IE's built-in window.showModalDialog method. It's fairly easy to use. Maybe a combination of this and Noah's suggestion?
You might try launching a modal from JavaScript using the JavaScript integration (see http://www.raditha.com/java/mayscript.php for an example).
The JavaScript you would need would be something like:
function getPassword() {
return prompt("Enter Password");
}
And the Java would be:
password = jso.call("getPassword", new String[0]);
Unfortunately that means giving up all hope of having a nice looking modal. Good luck!