Robot focus in minimized frame - java

I'm using at the moment the Robot class of the AWT library for a Java Frame.
But when I minimize the frame, the robot will still type and using the mouse in another application that isn't even written in Java.
How can I set the focus on the frame only and when I minimize, it'll still continue?

How can I set the focus on the frame only and when I minimize, it'll still continue?
You can't. By design a minimized frame does not have, and cannot get, focus.
You might be able to 'hack it' by making the frame go almost transparent when asked to minimize, though that will block input to any window that is behind it, and will fail if another is in front.

That's the whole point of Robot. It would be a lot less useful if it could only affect Java programs. – Kayaman May 12 '15 at 6:06
That's right. Robot only 'controls', you need to provide logic like 'If Frame minimized, click the minimized icon' to the Robot.
But this involves a whole set of new questions:
How do you know the Frame is minimized? (Sikuli)
What is the coordinate for the minimized icon? (Sikuli)
After you at least answered these two questions, you can control the Robot to have your Frame back and continue with your GUI Automation.
The answers are in the brackets.

Related

Java registering events through window

What I would like in Java is to create a transparent screen over the entire screen that does not respond to events such as mouse or keyboard. Essentially, the user won't even know about the screen. How would I make the window "transparent" to events?
Read the section from the Swing tutorial on Creating a Translucent Window.
You would use an opacity of 0.0f.
Edit:
With an opacity of 0.0f the events go through the frame to the component below, therefore the window you click on will come to the front.
With a non-zero opacity (ie 0.05f) the events are intercepted by the frame.
You may also need to use:
frame.setAlwaysOnTop( true );
to prevent the window from coming to the front.
Play with the opacity and always on top property to get your desired effect. If these two properties don't work then I have no ideas to do what you are asking, nor do I understand the requirement.

JPanel vs JFrame when animating

I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.

Remove close (X) button from the top right corner and keep min/max on JFrame

I want to create my UI with a JFrame and a minimize and maximize look-alike button with the same functionality as normal minimize and maximize buttons but I am not sure how. I want to take this approach so there will not be an X button in the top corner.
You cannot remove it and keep the other two, however, you can disable it using setDefaultCloseOperation:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
Note that this will not make the button unclickable but will disable the functionality.
For an undecorated frame you'd have to use JWindow instead of JFrame, then render your own title bar.
Of course, whatever you render will only look right on one particular platform in one particular look and feel (unless you write your own logic to handle different platform conventions by hand). For example, Mac users will expect the close, min and max buttons to be traffic light coloured circles at the left hand end of the title bar, not square buttons at the right.

My JApplet disappears when moved to secondary monitor

So I wrote a small program for a class. I designed it as a JApplet inside an undecorated JFrame, not in a browser. Other than that, it's a simple drawing program of sorts. You click two points to draw the selected shapes, then it calls a repaint. The problem I'm having is that when you draw while the program has been moved to my secondary monitor, the entire JApplet seems to disappear, only displaying the drawn shape. It only disappears after the 2nd point is selected, so I presume it does this on repaint().
My secondary monitor is using the exact same brand and resolution, even color profile.
Any other technical details, I'm using Java 1.7 (Can't recall which update off the top of my head), Windows 8 Enterprise 64x, using Eclipse's Run button to test.
Thanks in advance for any help!
I am indeed calling getGraphics(); in the init() method of the JApplet..
That is the problem. The Graphics object is a transient thing that will be repainted the very next time the JVM thinks there is any need to do so. That might be triggered by:
Changing the size or location of the window.
Covering it with another program and then removing the covering app.
Adding new components or changing values that are displayed.
See Performing Custom Painting for more details on how to do what you are attempting to achieve. OTOH Swing has a JLabel that can show a BufferedImage. You can use the BufferedImage in the way you want. When it is updated, call repaint() on the label to see the effect.

Java Applet to Multi Panel Application

I have made a Java game, but it is applet based. I want to convert it into a standalone application. Unfortunately I have 0 knowledge of swing/java applications so I'm not exactly sure where to start and how to get what I want.
My main issue is that the game screen is essentially divided into two parts. The "game screen" and what I refer to as the "dashboard". If you can imagine age of empires, star craft, or any other RTS type of game, that's what the layout is.
So what I want is the screen to have its own graphics panel (all the drawing/animation is done with the graphics and image class) that also has scroll bars in it, that way the size of the game isn't limited to the size of your screen. And I want the dashboard to be it's own separate independent panel, which also uses graphics methods. But in the end, both panels are in the same window.
Is there anyway this is possible?
PS: feel free to request any code or screenshots of the game
Edit: if it is possible, how should I go about doing this?
Simple Swing applications usually based on JFrame class. As i understand, whole game is rendered and not using standard components in UI. Then, roughly, almost no difference between JApplet and JFrame classes. If you used specific JApplet methods for loading resources or something similar, almost all of them would easily replaced with counterparts.
You could create a component class (by extending JComponent) and use it to render main game window. And create another one to render dashboard. Then use BorderLayout as layout manager on JFrame, place main window on center and dashboard on any edge.
There are pretty clear HowTo`s on Oracle site:
How to Make Frames and How to Use Panels.
I think Mersenne's answer covers it pretty well, though I'd consider rendering the game play in a BufferedImage and adding that to (an ImageIcon in) a JLabel in a JScrollPane.

Categories

Resources