How to hide SWT Browser or run in headless mode? - java

I took the rendered page from the SWT Browser and exported it to an image. My problem is that I am not able to get it to export properly when the shell is not visible. How can I go about hiding the browser and have the image export properly?
I have tried setting shell.Visible() to false but that messes up the image export.
This is how I export the image (not sure if this is necessary to the question):
GC source = new GC (shell);
Image image = new Image(display, browser.getClientArea());
source.copyArea(image, 0, 0);
ImageLoader io = new ImageLoader ();
io.data = new ImageData[] { image.getImageData() };
File f = new File (currentDir+"/workpng.png");
io.save (f.getAbsolutePath(), SWT.IMAGE_PNG);

This might be impossible because the X server/Windows will throw away all rendering commands when the window isn't visible (no point in rendering what you can't see).
Also what is the client area of the browser in this case?
To make this work, you'll need to allow the shell to open be visible (i.e. not hidden by some other window). Tools like Jenkins use a plugin that starts an X session with Xvfb or Xvnc. After setting the env variable DISPLAY, all UI rendering goes to these sessions.

Related

Why does my ImageView work on Windows but not on Mac

Here is the simple code for javafx in intellij:
Image image = new Image(url);
ImageView imageView = new ImageView(image);
movieBox.getChildren().add(imageView);
//checking
System.out.println(imageView.getImage().getUrl());
Textfield tx = new Textfield();
tx.setText("hi");
movieBox.getChildren().add(tx);
I copied the project over to intellij on my mac and everything works accept the images won't show. Here are the things I tried:
Checking that the URL is valid by printing out the imageview image url. It prints out a valid URL of an image i can open in my browswer
Adding a random textfield to the movieBox (a flowpane object) to make sure it works. And it does.
There shouldn't be any error in the code because it works in my windows intellij. It seems like a problem with the environment, but I haven't even got a clue how should i approach this problem. Any sort of advice will be nice, thanks!
Try downloading the image and using the path to the image on your disk, if it works then the problem will be with the url, otherwise its with the ImageView. If the problem is with the URL, post the one you are trying to use so the community can test it, and try another URL.
P.S. If you aren't manipulating the image you can just pass the path/url directly top the ImageView constructor.
public ImageView(String url)
Allocates a new ImageView object with image loaded from the specified URL.
The new ImageView(url) has the same effect as new ImageView(new Image(url)).

Cannot create image from getResourceAsStream() in JavaFX projects

I cannot create an image in any of my JavaFX projects using the following kind of code:
final String url = "line.jpg";
Image image = new Image(Config.class.getResourceAsStream(url));
because there is always a null pointer exception pointing to the second line. Obviously, I have checked that the image file is in the correct directory. I have tried example programs, some directly copied from these boards, but these also fail for the same reason.
I suspect I lack a resource in Netbeans or JavaFX but I can't figure out what is missing.
The only workaround appears to be to include the image file in a css stylesheet and link it to the program by setting the gui components id like this:
Button homeButton = new Button();
homeButton.setId("homebutton");
In the stylesheet there is:
#homebutton {
-fx-background-image: url("images/homebtn.jpg");
-fx-pref-width: 30;
-fx-pref-height: 30;
}
Its not ideal being forced into this solution and Swing seems far better at dealing with image files. I assume a bug in JavaFX that always causes the following to fail:
Image image = new Image(Config.class.getResourceAsStream(url));

Java application getting launched when trying to generate a thumbnail from a image byte array

Code:
public byte[] getThumbnail(byte[] imageBytes) throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).size(50, 50).keepAspectRatio(true)
.outputFormat("jpg").toOutputStream(outputStream);
byte[] picture = outputStream.toByteArray();
return picture;
}
I am trying to generate a thumbnail from an image in the above code.
When I call the above function, it launches a Java icon, which is shown in my attached screenshot. If I try to close this icon, my application gets closed.
The dock icon appears, because some of the imaging code you use, use awt under the hoods. This triggers the dock icon to appear on OS X. It's possible to suppress the icon, though.
The cross platform way of doing it, is running you application in "headless" mode, that is, with no user interaction using mouse, keyboard or screen feedback (ie. windows). You can specify headless mode at startup, using the system property java.awt.headless on the command line like this:
java -Djava.awt.headless=true
Alternatively, in code like this:
System.setProperty("java.awt.headless", "true");
For OS X (and an Apple JRE) you can alternatively use the system property apple.awt.UIElement, it will suppress the dock icon only, but otherwise let your app use windows etc.:
java -Dapple.awt.UIElement=true
From the documentation:
Suppresses the normal application Dock icon and menu bar from appearing. Only appropriate for background applications which show a tray icon or other alternate user interface for accessing the apps windows. Unlike java.awt.headless=true, this does not suppress windows and dialogs from actually appearing on screen.
The default value is false.

WizardPage adjustment with change of screen resolution

I have a dialog with a wizard containing one wizard page. The wizard page appears differently for different screen resolution. Some contents of wizardpage is missed if the it is executed in laptop or desktop with different screen resolution.
I have set the wizard page size in the dialog:
dialog.setPageSize(700, 700);
But eventhough this is not working properly. Please let me know is there any way so that wizard page gets adjusted with screen resolution changes.
Thanks in advance.
In your WizardPage you can reset the size of the dialog to match the preferred size of the contents using something like this:
private void recalcSize()
{
Composite dialogAreaComp = (Composite)getControl();
Shell shell = getShell();
Point shellSize = shell.getSize();
dialogAreaComp.layout(true, true);
Point newSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
if (newSize.x != shellSize.x || newSize.y != shellSize.y)
shell.setSize(newSize);
}
Put a call to recalcSize in the setVisible method of the page.

How to set a high quality OSX dock icon for an SWT Java application

I have written an SWT Java application and would like to configure it to use a high quality icon in the OSX dock. This is my current code:
// Prepare window
final Shell window = new Shell();
// Load icons
Display display = window.getDisplay();
ClassLoader loader = InTraceStandaloneUI.class.getClassLoader();
InputStream is16 = loader.getResourceAsStream(
"org/intrace/icons/intrace16.gif");
Image icon16 = new Image(display, is16);
is16.close();
InputStream is32 = loader.getResourceAsStream(
"org/intrace/icons/intrace32.gif");
Image icon32 = new Image(display, is32);
is32.close();
window.setImages(new Image[] {icon16, icon32});
https://github.com/mchr3k/org.intrace/blob/master/org.intrace/src/org/intrace/client/gui/InTraceStandaloneUI.java
This works for loading the 16x16 and 32x32 logos which look OK on Windows but the logo used by OSX still looks really pixelated. Do I just need to specify a third version with a higher resolution or should I be using a different image format or API?
Actually, if you call Shell.setImages(...) and include a 128x128 icon then this will be chosen by OSX and you get a high quality dock icon without having to use the Apple extension classes.
You can detect OSX and use the Apple Java Extensions and call Application.setDockIconImage(...)
Shouldn't the dock icon be referenced in the Info.plist using the
<key>CFBundleIconFile</key>
<string>icon.icns</string>
lines?

Categories

Resources