Showing JFrame in a non Raster Graphics Configuration - java

Greetings,
I'm trying to get a JFrame drawing in a non screen device. The JFrame constructor has a
JFrame(GraphicsConfiguration)
to seemingly allow this:
My First attempt was to create my own GraphicsConfiguration, who's GraphicsDevice reported GraphicsDevice.TYPE_IMAGE_BUFFER when getType() was called.
However JFrame.init specifically looks for the type and throws an exception if the type isn't TYPE_RASTER_SCREEN:
if (graphicsConfig.getDevice().getType() !=
GraphicsDevice.TYPE_RASTER_SCREEN) {
throw new IllegalArgumentException("not a screen device");
}
Next i tried to make the GraphicsDevice i returned report GraphicsDevice.TYPE_RASTER_SCREEN. This allowed the JFrame to be initialized correctly, but when it went to display it, I got
Exception in thread "main" java.lang.ClassCastException: TestGraphicsConfiguration cannot be cast to sun.awt.X11GraphicsConfig
So i've run out of ideas, on how to draw a JFrame that doesn't show up on the screen, but is never the less fully layed out and functional.
Am going down a rabbit hole here, or can this be done?

A Java top-level container such as JFrame requires access to a peer component native to the host platform, typically via JNI. Alternatively, you may be able to use a BufferedImage or java.awt.headless mode, as discussed here.
Addendum:
I wonder if "any human" can replace how peers are selected.
I don't know how to replace a particular peer component, but it's possible to evoke platform-specific native components; Java Native Access (JNA) is one such avenue. As an extreme example, this 6502 JVM runs in 128K on an 8-bit processor running at 1 MHz. The demos, including source for the lower right screenshot, were compiled using javac.

Related

Is there a way to set the default mouse cursor image for a java Swing application?

I'm working with a large java swing application. The customer wants to have a larger cursor image for the mouse because of the limitations (space) of the monitors that can be used for the application.
The way to do this for a single Swing JFrame instance is something like:
Image image = toolkit.getImage("resources/NetworkGreen48.gif");
Cursor customCursor = toolkit.createCustomCursor(image , new Point(0, 8), "img");
mainJFrame.setCursor (customCursor);
this works fine for the mainJFrame JFrame and any components embedded on the mainJFrame JFrame.
However, this application has literally hundreds of independent JFrames, JDialogs, etc. For these, running in the same JVM, but not actively added to the mainJFrame JFrame, the cursor reverts to the operating system default.
Is there a good way to set the mouse cursor for ALL the places the mouse will be used in the Swing application?
Considered doing it by talking to the OS, but, this application is running on Windows currently and will run on some version of linux in the future so I'd better not depend on the OS.
I can do this on a Frame by Frame basis. Would just like to know if there is a better way.
first part : custom cursor
I was searching to define this using look and feel but i didn't find anything about loding custom cursor inside, so there is 2 solution always use a custom sub class of JFrame where the method frameInit is override to use your cursor. Or use a factory to create all the frame you need to set the property as you do before.
Swing is not completely os independant i think that you see few rendering difference between the os you use. The only think that change is the look and feel(laf or plaf), each OS use its own plaf. but there is a cross OS Laf call Metal. Here is the documentation related to how change laf :
https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
to be sure to be cross OS its recommanded to work using Metal but it's not required.

Using Processing with Eclipse: close sketch without closing the rest of the windows?

I'm working on a Java project in Eclipse that has a class extending PApplet to run a Processing sketch.
First I have a JFrame login screen, and after the user logs in there, I call
PApplet.main("Game"); //"Game" is the class that extends PApplet
to start the sketch.
Now when the game ends I want to close the sketch window, but not the original JFrame window.
Normally I would call
exit();
in Processing but this closes the entire application (ie all windows).
I have also tried
dispose();
but this does nothing.
I guess I'm looking for something like
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
but for PApplet.
First, you have to get the instance holding your sketch. That means using the runSketch() function instead of calling main() directly:
Game game = new Game();
String[] args = {};
PApplet.runSketch(args, game);
Now that you have a reference to your sketch instance, you can use it to get to the internal window. How you do this depends on which renderer you're using, but you can figure it out using a mix of the Processing JavaDoc and the Processing source code.
Here's an untested example using the default renderer:
PSurface surface = game.getSurface();
SmoothCanvas smoothCanvas = (SmoothCanvas)surface.getNative();
JFrame frame = (JFrame) smoothCanvas.getFrame();
Now that you have the parent window, you can do whatever you want with it, including:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(false);
Like I said I haven't tested this code, and this is going to depend on exactly which renderer you're using, but this process of using the source code and JavaDoc to figure out what's going on under the hood to get to the underlying window is what you have to do.

Maybe a good reason to use multiple JFrames, if not open to suggestions

I'm developing an application for a deposit ATM. Almost everyone has used one of these at least once in their life so it's safe to say you know what I'm talking about.
I'm currently doing the GUI and I think I should use multiple JFrames.
My reasons:
Each frame is set to respond to certain, different conditions - smart card reader sends a signal, timeout occurs, click occurs, different parts of the machine send various signals to which the app must respond and display an appropriate message
Since this is an embedded device the user has zero ability to interact with the os of the machine beyond using this one program. I think this sets aside considerations of esthethics - multiple windows in taskbar.
The Fullscrean mode does a great job of conceiling everything else going on in the background.
What I dislike:
I get a screen flicker when switching from one frame to another. This might not be related to the general topic of the question and might just be because I'm disposing of frames everytime the program switches away from them instead of setting them to be invisible.
Any thoughts on the subject are welcome.
You should use a single JFrame, and have multiple JPanels for the various "screens" you want to show. To change "screen", just remove from the JFrame the JPanel currently shown and add the new one.
EDIT:
To make the switch, you can use CardLayout as LayoutManager of your frame. It shows one panel at a time and allows you to easily switch between them.
It's no real advantage to use multiple JFrames, when you can use a single one and have multiple content panes for it.
This should prevent any useless flickering and make sure that only one of your "screens" is visible at the same time.

Gdx exit closing entire application - how to close only the gdx frame?

I have a game editor, in which it is possible to launch the game being edited in a separate window (but within the same VM). However, when the game is closed, I would like to close its gdx window, without bringing down the whole application (that is, the editor).
I am currently using the following code inside the JFrame that hosts the LwjglApplication:
public void windowClosing(WindowEvent e) {
System.err.println("Now closing app...");
Gdx.app.exit();
System.err.println("App now closed.");
}
This prints the goodbye, closes the GDX window, and proceeds to terminate my VM. Any suggestions?
In the desktop (lwjgl backend) Gdx.app.exit() posts a Runnable that causes the loop mainLoop to complete and control to fall out the bottom of that function (see the source). The mainLoop ends with:
if (graphics.config.forceExit) System.exit(-1);
The graphics.config is the LwjglApplicationConfiguration object passed in LwjglApplication constructor. So just set
config.exit = false
in the config object (you may need to create one and use a different constructor if you're not currently creating a config object). There are other handy things you can set in the config object, too.
(This code is from GIT, so things may be different in older version of GDX or with other backends.)

Notify minimised window of an event occurrence in an applet

I have a JApplet which is used for chat. I would like to make it possible that when the applet is minimised and a chat message is received by the user, the minimised window becomes orange (and thus shows the user that something has occurred).
How is it possible to make the applet do this?
Thanks,
Tim
You may have access to the system tray in an applet (I'm not sure). Have a look at the java.awt.SystemTray class - the in-tray lets you pop up messages to the user.
Alternatively you could attempt to cause the Window's toFront method to be called or to "maximize" using the setSize methods (again, I'm not sure what effect this has in an applet). I suspect that the toFront method will be a good bet
Another option I'd look at is raising a JDialog. The presence of this may cause the OS to draw attention to the minimized applet. You could listen to window events representing the screen un-minimizing to clear the dialog so that the user never knew it was there.

Categories

Resources