Adding icon to my application issues - java

I want to change the icon of my application. system.gif is in current directory. getDefaultToolkit() is not working. getToolkit() is working but another minimized window is also open. Other components are Swing components.
Toolkit theKit = jtfMainFrame.getDefaultToolkit();
Image icon = theKit.getImage("system.gif");

To get an icon for an app.
Add it to the Jar and use Class.getResource("/system.gif") to obtain an URL to it. File objects will generally not work for such an 'application resource'.
To load the image, use ImageIO.read(URL). This is a blocking method that will ensure the image is entirely loaded before proceeding. It will also throw informative exceptions if anything goes wrong.

Related

ImageJ API: how to display an image with zoom and drag to scroll features active in the image window?

If I use Fiji application to open an image, then the image is displayed in a window where I can use + and - keys to zoom in and out, also I can hold down the space bar and drag the zoomed image with mouse to scroll through it.
I want to implement the same features in a java application using ImageJ API. If I use the following code (taken from here) to open an image, then the image is displayed in the same type of window as in Fiji case, but zoom and drag to scroll features are not active in the window.
Opener opener = new Opener();
ImagePlus imp = opener.openImage("/path/to/image.tif");
imp.show();
Could somebody suggest how to get the two features active? To me they look like standard features that everybody uses, so I expect that their activation could be easily available through the ImageJ API. However I found no hints in the API specs.
As far as I understand, the call imp.show(); in the code above is equivalent to the following:
ImageWindow imageWindow = new ImageWindow(imp);
imageWindow.setVisible(true);
If so, there should be some methods of this ImageWindow class that would assure activation of zoom and of drag to scroll. Does anyone have a clue?
Or, maybe, could someone share a direct link to Fiji source code where these exact features are implemented? Is it supposed to be here? If yes, then where exactly?
Perhaps you should be looking more at the ImageJ source code. I'm assuming this is for ImageJ1. My guess is that the keybindings are set when the main gui is loaded in the ImageJ class
Having downloaded the imagej1 source code and doing a grep for zoom I found
./IJ_Props.txt:zoom01="In [+]",ij.plugin.Zoom("in")
./IJ_Props.txt:zoom02="Out [-]",ij.plugin.Zoom("out")
./IJ_Props.txt:zoom03="Original Scale[4]",ij.plugin.Zoom("orig")
./IJ_Props.txt:zoom04="View 100%[5]",ij.plugin.Zoom("100%")
./IJ_Props.txt:zoom05="To Selection",ij.plugin.Zoom("to")
./IJ_Props.txt:zoom06="Scale to Fit",ij.plugin.Zoom("scale")
./IJ_Props.txt:zoom07="Set... ",ij.plugin.Zoom("set")
Try launching the main GUI with the ImageJ class. Alternatively lookat how the key listener is set up in that class.
There are multiple places in the code of ImageJ that are of interest to you. Line 388 for example is the beginning of the KeyPressed method.
After those actions the doCommand methdod calls the respective plugin.
I resolved it. As I expected, there exists a simple way to activate zoom and drag to scroll features of ImageJ in a Java program.
As I see now, most of the features of ImageJ are implemented as plugins under ij.plugin.*.
To activate a desired ImageJ feature in your Java program you need (1) to find the plugin that corresponds to the feature inside ij.plugin.* package, (2) to call the plugin in your code, and (3) to put IJ_Props.txt file taken from the standard ImageJ distribution to the directory where you run your java .jar executable (it appears that IJ_Props.txt file contains settings used by many of the available ImageJ plugins).
So, in our case we need to activate ij.plugin.Zoom plugin in our java code:
//Launch ImageJ in NO_SHOW mode: Run embedded and invisible in another application.
ImageJ imageJApplication = new ImageJ(2);
Opener opener = new Opener();
String imageFilePath = "path/to/your/image.png";
ImagePlus imp = opener.openImage(imageFilePath);
imp.show();
IJ.runPlugIn("ij.plugin.Zoom", null);
When executing this code we get ij.plugin.Zoom plugin activated and both of the desired features (zoom and drag to scroll) will be functional in the window where our image is displayed. We also get the traditional ImageJ thumbnail view of the displayed image area in the upper left corner of the window.

Java Web Start application shows empty window on XMonad

I use XMonad+gnome as window Manager. I have the problem that, when I run a Java Web Start applicacion, It olways show an empty window, like the screen:
Is there something I can do to fix it?
UPDATE:
I tried simple programs from JWS Examples and it works, right. So it should be a problem of the application.
The failed aaplication is Blast2Go.
Change the name of the window manager to a known one like LG3D:
startupHook = setWMName "LG3D"
I had the exact same problem with my own Java code and using the other suggested solutions (setWMName, MToolkit, etc.) did not solve the problem with Xmonad. I must note that in other window managers (e.g. fluxbox), the app works as expected. In my case, however, I figured out the problem. If you have access to the source of the Java app, I suggest you consider the following:
If you have any JFrame or JPanel or other containers, you should explicitly define their layouts. If you set the layout to null, then the dimensions of the container must be set explicitly. Otherwise, the container won't be rendered at all. For instance, I have a JPanel that contains all my widgets. I'd rather have full control over the layout, so I set the layout to null, and then explicitly set the dimensions for the JPanel:
jContentPane.setLayout(null);
jContentPane.setPreferredSize(new Dimension(appletWidth,appletHeight+100));
Hope this helps.

Image.setIcon(Parameters) Is not refreshing the Label

I'm working with Java Swing in the making of an Online Chess(Client-Server), I have been able to make the chessTable form wonderfully with no trouble, but in the lobby I'm showing the user's Avatar, the problem is that, the user is able to change his avatar through the server, but when ever I call the setIcon of the new image, it wont refresh it.
Explanation:
The users start with a default image(Avatar.jpg) downloadead from the server,if he changes it, the server sends to the client the new image he chose with the same name of the previous one, so it gets replaced. But when ever i call setIcon of this new image(after the download), it wont do anything and the Label stick to show the first image he ever set. I'm checking the folder and the image are being sent form the server to the client with out problems, and the only way for me to make the client refresh the Avatar is by closing and opening the app. Any ideas?, I tried reprint every random thing I could find still no solution, and the labels refresh by a clickedListener.
I use this line to set the image:
this.JLProfilePic.setIcon(new javax.swing.ImageIcon(getClass().getResource("/View/Images/avatar.jpg")));
Edit1: I'm not working with a JApplet, still I don't have any idea of where the problem might be originated, since Im able to change the Label Icon to any image but when ever I try to change the Icon to Avatar, it still shows the previous one even if in the folder you can see that Avatar.jpg is not the same, it changed for a new one.
Edit2: Well I still haven't found any solutions for this, I supose might be true or not, since i setIcon once, the image is saved at the cache when ever the program re access to the same image it goes to the cache instead of checking again the source of the file. Not sure but is the only Logic I can make.
Try this,
Image img = ImageIO.read(new URL("http:\Server_IP:Port\image.jpg"));
setIconImage(img);

Window containing an Applet

How can I, in an Applet, get the Window containing the Applet as an Window Object?
My goal is to make a screenshot of the content of a "Website".
I tried to make this with the Robot object:
Window window = ???
BufferedImage bufferedImage = new Robot().createScreenCapture(window.getBorder());
To get the border of the browser is the reason why I need this as a window object.
Or is they another possibility to do this?
To get the border of the browser is the reason why I need this as a window object.
Figure how to do it in JavaScript. Call the JavaScript from the applet.
This might not be the only way to do it, but it is arguably the best. It uses JS for the type of thing that JS is especially good for, interacting with the web page. Get it working and debugged in JS, then it is a relatively simple matter to call that function from the applet.

OfficeBean won't display after moving containing Swing Panel from one Container to another

I'm developing a Java 6 applet which allows users to view OO (v.3.2) documents (read only), and if they choose, click a button which launches a new JDialog window, with the document displayed in it which allows the user to and mark and redact it as they wish. Once they are done, they can close the JDialog, which saves the document to a server and redisplay the updated document (read-only again) in the original applet window
I guessed that I could do this with a single instance of an OfficeBean, embedded in a Swing Panel. However, I cannot seem to successfully move my Panel (containing the OfficeBean) from the applet to the JDialog when the "Redact" button is clicked. All I get is a blank area in the JDialog where the document should be. I get no errors.
I have currently managed to get round this by creating new instances of the OfficeBean every time I need to display the document (once when the applet is loaded, again when the user chooses to redact and it is opened in a JDialog, and finally when they click "Save" in the dialog and the redacted result is displayed in the applet again.) However this means three trips to and from the server where the documents originate. That seems mad to me.
I'm in no way a Swing expert and may well be making a silly mistake. However, I've done a lot of fiddling around, debugging and googling and can't seem to get this to work. Can anyone help me in this? Am I trying to do something which is fundamentally impossible? I hope not.
One rule in Swing is that a component can only be displayed / attached to one part of the gui "tree" at a time. When you "move" your component to the dialog, are you first removing it from the applet?

Categories