I wrote a program that solves mazes with depth-first search. I was wondering how to turn this Java program into a Screensaver application? Is there a way that Windows 7 starts my app when the screensaver would normally be activated?
A Windows screen saver is just program that accepts certain command line arguments. So in order to have your program be runnable as a screen saver you must code it to accept those arguments.
Next you will probably want your screen saver to run in full screen mode. This is very simple to do in Java as the example below shows:
public final class ScreenSaver {
public static final void main(final String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame screenSaverFrame = new JFrame();
screenSaverFrame.setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE);
screenSaverFrame.setUndecorated(true);
screenSaverFrame.setResizable(false);
screenSaverFrame.add(new JLabel("This is a Java Screensaver!",
SwingConstants.CENTER), BorderLayout.CENTER);
screenSaverFrame.validate();
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.setFullScreenWindow(screenSaverFrame);
}
}
Finally you will need to make your Java program into a Windows executable using something like Launch4j and give it .scr extension.
I have never used this but it might be the place to start. Screen Saver API.
The link to the screensaver SDK seems to be broken at the moment so I am linking to the index page: JDIC. When they fix their link I'll adjust this.
Windows screensavers are just exe files that accept certain command-line arguments, detailed here.
If you can compile your java app into an exe (I don't use java much any more so I'm not sure what tools exist), then rename it to .scr, that would do it.
Or it might be enough just to make a .bat file like:
#echo off
java myProg.class %1
.. And rename it to .scr.
I would look into the SaverBeans API for creating screen savers in Java.
Use the JScreenSaver library.JScreenSaver is the middleware to make a screen saver for Windows in Java language
One of the alternative to this approach is the new JavaFX. You can create fancy screen saver in the same way you do it in Swing. On top of that you get *.exe file quite easily using NetBeans.
Related
I wish to thank you in advance for taking the time to read my question, and I would greatly appreciate any comments, answers, insights, techniques and critiques that you may be able to provide.
I'm looking for a useful method for changing the desktop icon for a Java application. I've looked into this for a few days now, but am not finding an accurate result.
Before you mark this down and call it a duplicate, I have read: How do I change the default application icon in Java? to others who asked this question), but this does not address my specific problem. I know that their method utilizes a url location instead of an import, but I am trying to learn how to use this with the import(if that is, in fact, possible). When I attempt to use their method for changing by source location. Besides that, the url example doesn't seem to work for a file stored on the computer. I get an "uncaught error" message when I attempt to run it.
I use the following format to declare an image that I have imported into NetBeans:
Image image = new ImageIcon("imported.png").getImage();
frame.setIconImage(image);
Now this works fine for the icon that displays in the toolbar and it also appears in the upper left-hand corner of the frame, but I still have the Java coffee-cup as the icon for the application when I clean and build it.
For additional resources to the code that I am using to attempt this:
import java.awt.Image;
import javax.swing.*;
public class Check {
JFrame frame;
public static void main(String[] args) {
new Check().go();
}
private void go() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = new ImageIcon("owl.gif").getImage();
frame.setIconImage(image);
frame.setVisible(true);
frame.setSize(300, 300);
}
}
The "owl.gif" bit is what I imported into NetBeans by click and drag method (as described in one of the books that I read that focused on NetBeans).
I'm looking for a way to make a file that I already have saved on my computer the desktop icon for my application after it is built.
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start1. JWS works on Windows, OS X & *nix.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
The 'desktop integration' will use the image identified in the launch file as the desktop or menu item icon.
Is there a way to make sure that a user cannot close or leave my Swing application? I've tried to make it fullscreen, but you can still Alt-Tab away from it—and besides, that doesn't work well when you decide to use JOptionPane's dialogs.
So, is there any way to make a user use only this one Java program on a device?
Edit: Some people wonder about the purpose. The application is supposed to be sorta "embedded" into the handheld device (which runs under Windows), so the users of the device will use it as we intend it to be used—for example, that they won't play Freecells or do something worse instead of doing the actual work. Have you seen ticketing kiosks? They are locked down pretty well, you can't just close their big flashy GUI and get to the Windows desktop!
Okay #Daniel showed you how to make a Java app un-closeable (+1 to him) by calling:
JFrame#setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
on the JFrame instance.
To make sure you cant leave it i.e by pressing CTRL+ALT+DEL and ALT+TAB etc you may want to do this (applies to windows only):
1) Disable TaskManager/CTRL+ALT+DEL by:
Setting registry key:
HKCU/Software/Microsoft/Windows/CurrentVersion/Policies/System/DisableTaskMgr = 1
via reg script or cmd.exe.
2) To disable all shortcuts together like ALT+TAB etc see here (Download/use the *.reg script and execute it via cmd).
Uncloseable yes, you can use setDefaultCloseOperation(); and pass it JFrame.DO_NOTHING_ON_CLOSE
Example:
import javax.swing.*;
import java.awt.*;
class app extends JFrame {
app(String title, int height, int width)
{
super (title);
setSize(width, height);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setVisible(true);
}
}
class program {
public static void main(String[] args) {
app myApp = new app("Hello", 350, 750);
}
}
We do something similar with a POS application, but we cheat a little bit. While making use of Full Screen helps a lot, we found users could STILL exit the application.
In the end, we created a Windows Service (yes, we run on Windows) that automatically RESTARTS as soon as it's closed. So, you can see the Windows desktop (we removed the icons) for a split second, but then the app pops up again. The benefit of this is also that we can update the JAR file remotely, from the intranet, and all the users need to do is push a button that closes the system, it restarts automatically and is updated. We wanted to use WebStart, but had problems integrating it with our wrapper.
The wrapper itself is a Python application that just starts the JVM and application, compiled to an EXE. Simple, but effective.
The net effect is a closable application that will automatically start itself again. Good enough for our use where we need to user to be able to do one thing and one thing only. Giving the startup credentials for the application Admin privileges, and the users just a normal account, took care of the pesky 'alt-ctrl-del' users as well. You can't kill a process that's got higher access rights than yourself.
If you don't feel like writing your own wrapper, give http://wrapper.tanukisoftware.com/doc/english/product-overview.html a whirl, it looks like a brilliant product.
You can catch pretty much any keystrokes you like (and have them ignored) in your app, so Alt-Tab can be fixed. The one key combination that an application can never (ish) handle itself, is the Ctrl+Alt+Delete, which is hooked into the kernel in many operating systems for security reasons. As a side note: this is why many login screens ask you to hit C+A+D before entering your username and password.
You can make a maximized window that can't be unmaximized or closed. You'd also have to trap certain keystrokes such as CTRL+SHIFT+ESCAPE, ALT+TAB, WIN+[anything], various Fn keys, and you'd have to do something to prevent CTRL+ALT+DELETE from working (Either from showing the task manager or from bringing up the blue options screen in windows vista or 7.
I believe there are windows API calls that can change the behavior of CTRL+ALT+DELETE somehow but have no idea what they are. A good place to investigate would be Sysinternals Process Explorer, which has the functionality to replace the task manager. Figure out how it does this, then replicate it.
I am a beginner in java game programming. I have developed a simple java game and obtained a .jar file of it. It is not an applet. I would like to run it on a browser. Is that possible? How can I achieve that?
Assuming your jar's main class simply opens a JFrame to show its contents, you can build a wrapper applet class which simply invokes it, like this:
public class WrapperApplet extends Applet {
public void start() {
new Thread("application main Thread") {
public void run() { runApplication(); }
}.start();
}
private void runApplication() {
my.Application.main(new String[0]);
}
}
If you want it nicer, have the applet show a button and start the main method only after the button is clicked.
If you want to embed a java application in a web page, you need it to be in applet form. It's not that difficult to convert them, see this link for a bit of help.
It's possible with Java Web Start. From the Wikipedia article:
Web Start can also launch unmodified applets that are packaged inside .jar files, by writing the appropriate JNLP file. This file can also pass the applet parameters. Such applets also run in a separate frame. Applet launcher may not support some specific cases like loading class as resource." The same article mentions some of the problems with applets "Web Start has an advantage over applets in that it overcomes many compatibility problems with browsers' Java plugins and different JVM versions.
This SO question explains some of the tradeoffs on Applets v. JWS... In my opinion, if you expect a lot of people on different types of systems to use your application, or if it uses a fair amount of memory (likely with a game), JWS is better.
How to make your desktop Java app looks like Open Office or Eclipse etc ?
Installation process looks like any Windpws app. installation. There is no Java logo on the to on a app window. You run it by .exe file. How it is done? Is this jar->exe conversion?
Is there any free tool to do that?
For the native look, you can obviously go the SWT way, like Eclipse does, however it's a painful one. You could/should prefer the Swing look'n'feel, by using, as an example, the Substance Look'n'Feel.
For the installation part, you can use
InstallAnywhere
IzPack
For the exe wrapper, you can use
Launch4J
JSmooth
or others ...
However, I think that, by doing so, you're doing it wrong.
indeed, instead of the classical download/install step, which is cumbersome, you can go the Java Web Start way : user only has to click one webpage link to install application to its machine (with an update mechanism directly integrated in), an install that go as far as potentially including desktop and start menu shortcuts, and an element in the Windows install panel to remove installed software.
I tried Jar2Exe, and JSmooth, they both produce exe files from jar archives.
The question is a little unclear, but I think that what you're after is making your java app behave like a native app (stuff like running it when an icon is double-clicked, etc...). There is an excellent tutorial on this here.
Note that, for the graphic part, Eclipse uses a library called SWT, which is a set of widgets that feel and behave in a different way that Java Swing or AWT.
Anyway, if you go the normal Java (Swing) way, the Java logo on the top of an app window is setIconImage() method in JFrame components.
Riduidel already told you about .exe wrappers and installers you can use. For the installer, I also suggest you to consider Java Web Start instead of a normal Windows installer.
I think it uses LookAndFeel, I let you read: http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){}
EDIT: I didn't read the entire question ^^' Maybe it will be useful to someone...
This is a followup question to one I previously asked:
start-program-if-not-already-running-in-java
I didn't get a great solution there (as there doesn't appear to be one), but I have a related question:
Is there anyway to launch an application in Java code (an .exe in Windows, not a Java app) and have it start minimized? Or perhaps to minimize it right after start? That would solve the focus issue from the other question and the already running problem would more or less deal with itself.
Clarification issues again: the Java client and the .exe are running in Windows and I really don't have the ability to write any wrappers or make use of JNI mojo or anything like that. I more or less need a pure Java solution.
Again, thanks for the help and I am more than willing to accept an answer that is simply: "This is just not possible."
Windows only:
public class StartWindowMinimized {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err
.println("Expected: one argument; the command to launch minimized");
}
String cmd = "cmd.exe /C START /MIN ";
Runtime.getRuntime().exec(cmd + args[0]);
}
}
Sample usage:
java -cp . StartWindowMinimized notepad.exe
java -cp . StartWindowMinimized cmd.exe
To understand the arguments involved:
cmd /?
START /?
I'm not that familiar with the specifics of Java, but according to a web site I just looked at, if you're using java.awt.Frame (which includes JFrame from Swing), you should use the function off of that frame called setState, which accepts Frame.ICONIFIED and Frame.NORMAL as a parameter (iconified would be the minimized state).
How do I minimize a Java application window?
If these apps have command-line switches to make them start minimized, then you can easily use those. Otherwise, I can't be 100% sure, but I highly doubt this is possible. You would have to have some way to interface with the Windows window manager, which is inherently very platform-specific and Java is therefore unlikely to include it. It's always possible that someone has written a third-party library to handle the task but it just doesn't seem likely to me.