For some reason, my application is not showing JPG files within a JavaFX window. It does not matter if I'm trying to display the image inside an ImageViewer node or setting it as a background with the CSS -fx-background-image: url(...); property.
I have tried with different jpg files. It did not work.
I have tried with different image extensions (png, gif). It did work.
I'm using OpenJDK 16+36 (I tested with Hotspot and OpenJ9 JVM), JavaFX 16, my operating system is Linux 5.11.16 and my window manager is GNOME Shell 3.38.4.
PS: Needless to say, the operating system handles JPG images without any problems.
What could be causing this behavior?
Edit 1:
For me, nothing that involves jpeg is working on JavaFX. Wether I try to use jpeg as an background-image, like this very Oracle's sample, or within an ImageViewer, the result is the same (the window opens normally, as if I hadn't put any image). If I replace the file with a different extension, it works perfectly. The funny thing is that jpeg is working without issues in Swing.
Here's one sample of the possible situations in which the issue happens:
/**
* JavaFX Application
*/
#Override
public void start(Stage primaryStage) throws Exception {
// opens a window with the image without issue and prints true
var fileUrl = getClass().getResource("/freemind.png");
var image = new Image(fileUrl.toExternalForm());
var imageViewer = new ImageView(image);
var vbox = new VBox(imageViewer);
var scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(new File(fileUrl.getFile()).exists());
}
But when I replace the fileUrl with another existing jpg file, in the same directory, it opens a blank window, and prints true.
Edit 2:
I found the -Dprism.verbose=true -Djavafx.verbose=true debug flags, which were unknown to me, and they print the problem:
Stack trace here
Some JPEG library, which I am not sure whether it comes embedded in the JavaFX SDK or is related to the operating system is in a different version than the expected. Now, which library is that, I don't know!
rpm -qa | grep -P jpe?g returns:
libjpeg-turbo-2.0.5-5.fc33.i686
libjpeg-turbo-2.0.5-5.fc33.x86_64
libjpeg-turbo-devel-2.0.5-5.fc33.x86_64
mjpegtools-libs-2.1.0-20.fc33.x86_64
openjpeg2-2.3.1-10.fc33.x86_64
Related
I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.
I am using netbeans. I have a project directory like this:
HTMLEdit/
src/
htmledit/
- pic.png
- MyClass.java
I tried to get the image, but it return null. I had trying both of these but still cannot get it to work:
System.out.println(getClass().getResourceAsStream("/pic.png"));
and
System.out.println(getClass().getResourceAsStream("pic.png"));
What's causing this weird behavior?
EDIT :
It looks like it's because I choosed JAVAFX Project when created the project. I recreate the project by choosing Java Project and it works fine. May be this is Netbeans bug.
getClass().getResourceAsStream() is used for files embedded inside your java jar file. You should use FileInputStream if you need to read a file from your file system as a stream of bytes. Here's the documentation: https://docs.oracle.com/javase/8/docs/api/java/io/FileInputStream.html
When you do getClass().getResourceAsStream("/pic.png")then the url that will be looked to access the file will be an absolute url. The absolute URL is indicated by the slash which is at the front of the resource location.
If you do getClass().getResourceAsStream("pic.png"), then a resource relative to the package where the class resides will be used.
Because you said that both of the getResourceAsStream() statements did not work in Netbeans, I checked the below JavaFX code in Netbeans and it worked perfectly.
public class MyClass extends Application{
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Image images = new Image(getClass().getResourceAsStream("pic.png"));
ImageView image = new javafx.scene.image.ImageView(images);
root.getChildren().add(image);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the structure and the output of the program.
In case if you want to know the Netbeans version, I am using then it is Netbeans 8.0.2.
Also, read the following post.
Different ways of loading a file as an InputStream
In Sikuli script, we have different ways of asserting like Screen.exists(a.png) etc for image assertion. But can someone please tell me how to assert an application screen with a screenshot file.
My requirement is that, I'm logging into a windows application, and after logging in I want to verify the screen that appears with a screenshot file(.png) captured earlier.
Sikuli api 1.0.2
Windows 7 64 bit
This should help you-
int timeout = <Specify timeout in millis>;
String imagePath = <give the path of your screenshot image here>;
ImageTarget target = new ImageTarget(new File(imagePath));
ScreenRegion result = desktop.wait(target,timeout);
Here if result is null after the timeout, it would mean the image found in the application does not match the one at the path of the screenshot you would have mentioned.
I am trying to render and export FusionCharts completely on the server. I am aware of solutions such as FCimg and FusionCharts .NET Solution. I have also implemented a Java solution that uses the Process class to run wkhtmltoimage.
However, I am trying to find a pure Java solution of doing this. I have an html file that includes FusionCharts JS Libraries and code to generate the fusion chart. I found JxBrowser that properly renders the chart but it requires X-Server for it to work on Linux. I also have tried Cobra/Lobo Browser but it does not fully support JavaScript. Are there any other ways to render and export fusion charts on the server or atleast render an html file that includes JavaScript completely in Java (and that does not require xserver)?
Thanks in advance for all the help!
Update: Solution that does not require xserver: WebRenderer. The Swing Edition is the only edition that supports HTML5 as of July 9th, 2012. You can use the swing edition to capture the image without a GUI.
I found a way that uses Eclipse's SWT Browser. However this cannot be run in an headless mode. You will have to use xserver to implement this. See this question.
Since this requires xserver and cannot be run in an headless mode, I would suggest using JxBrowser. It is a lot simpler and all you need is to generate an html file with all the fusion charts scripts. See #1, #2, #3
You have to create a template.html file that contains the header
(<html><head>), jquery.min.js, FusionCharts.js,
FusionCharts.HC.js, FusionCharts.HC.Charts.js. Make sure each of
these scripts are in their own script tags (<script type="text/javascript"> [js code] </script>)
Now add another JavaScript function with its own script tags containing the steps to render the chart. For example:
function load() { FusionCharts.setCurrentRenderer('javascript'); var chart = new FusionCharts("swf", 'chart0', "width", "height", "0", "1"); chart.setXMLData("XML DATA HERE"); chart.render("divNAMEHere"); }
Now you need to call the load() function onload, create a div to render the chart in, and end the html file. For example:
`
test
`
Create a new class that imports the eclipse swt browser libraries. Instantiate Display, Shell, and Browser (use this as a guideline to help understand what is happening: http://www.roseindia.net/tutorials/swt/swt-browser.shtml).
Set the text of the browser (browser.setText("htmlcode")) to the html code from template.html. The best way to do this would be to read the file using BufferedReader.
Lastly, the image takes some time to render. Now there is probably a better way to do this but if you want to just get it working, I set up a count and it captures the image after a certain number. This is what you need to add to the end:
int i = 0;
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
i++;
// System.out.println(i);
if(i==100)
{
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);
}
}
}
When I started programming with the JDK6, I had no problem with text components, neither in AWT nor in Swing.
But for labels or titles of AWT components I do have a problem. I can't display Farsi characters on AWTs components (in Swing I type them into the source code).
Here's my sample code:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Properties;
public class EmptyFarsiCharsOnAWT extends JFrame{
public EmptyFarsiCharsOnAWT() {
super("مثال");
setDefaultCloseOperation(3);
setVisible(rootPaneCheckingEnabled);
}
public static void main(String[] args) throws AWTException, IOException {
JFrame jFrame = new EmptyFarsiCharsOnAWT();
MenuItem show ;
// approach 1 = HardCoding :
/*
show = new MenuItem("\u0646\u0645\u0627\u06cc\u0634");
*
*/
// approach 2 = using simple utf-8 saved text file :
/*
BufferedReader in = new BufferedReader(new FileReader("farsiLabels.txt"));
String showLabel = in.readLine();
in.close();
show = new MenuItem(showLabel);
*
*/
// approach 3 = using properties file :
FileReader in = new FileReader("farsiLabels.properties");
Properties farsiLabels = new Properties();
farsiLabels.load(in);
show = new MenuItem(farsiLabels.getProperty("tray.show"));
PopupMenu popUp = new PopupMenu();
popUp.add(show);
// creating Tray object
Image iconIamge = Toolkit.getDefaultToolkit().getImage("greenIcon.png");
TrayIcon trayIcon = new TrayIcon(iconIamge, null, popUp);
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
jFrame.setIconImage(iconIamge);
}
}
These three approaches all work when run with an IDE, but when I make a JAR containing this class (by means of NetBeans > project > clean & build), I don't see the expected characters (it shows EMPTY/BLANK SQUARES)!
Note:
It seems I can not attach anything, so the contents of the text file would be this: نمایش and the contents of properties file:
#Sun May 02 09:45:10 IRDT 2010
tray.show=نمایش
And I think I have to let you know that I posted this question a while ago on SDN and "the Java Ranch" forums and other native forums and still I'm waiting...
By the way I am using latest version of Netbeans IDE...
I will be grateful if anybody has a solution to these damn AWT components never rendering any Farsi character for me...
I suspect that this is platform related. Your example appears to work on my platform using approach 1 in either Netbeans or the command line; I didn't try the other approaches.
There might be a disparity between the IDE and the command line with regard to the default character encoding. I've noticed that NetBeans, Eclipse and many consoles can be set to something other than the platform default. Here's code to check:
System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset().name());
You might look at this related question, too.
Addendum: Note show string changed to match the JFrame title for comparison. The title and menu look the same from NetBeans' Run > Run Project as well as via these command lines:
$ java -cp build/classes EmptyFarsiCharsOnAWT
$ java -jar dist/test6.jar
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class EmptyFarsiCharsOnAWT extends JFrame{
public EmptyFarsiCharsOnAWT() {
super("مثال");
setDefaultCloseOperation(3);
setVisible(rootPaneCheckingEnabled);
}
public static void main(String[] args) throws AWTException, IOException {
JFrame jFrame = new EmptyFarsiCharsOnAWT();
MenuItem show ;
// approach 1 = HardCoding :
show = new MenuItem("\u0645\u062b\u0627\u0644");
PopupMenu popUp = new PopupMenu();
popUp.add(show);
// creating Tray object
Image iconIamge = Toolkit.getDefaultToolkit().getImage("image.jpg");
TrayIcon trayIcon = new TrayIcon(iconIamge, null, popUp);
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
jFrame.setIconImage(iconIamge);
}
}
The most exciting part of your reply was:
"$ java -jar dist/test6.jar" !
Does it really shows the real characters (just like the frame title)?!
and not boxes or garbage ?
I'm sorry if I believe it hard, because the only problem in my developing work with Java took such long without any answer nor from searching, nor asking in forums is this!
So, what can I do? what font should I use? Unfortunately I'm not so familiar with fonts, until now I've just used global fonts in Java (Serif,SansSerif,etc.) and only modified their size or style, but after you suggest I examined several Persian ttf fonts through these codes:
File fontFile = new File("F_JADID.TTF");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
show.setFont(font.deriveFont(15f));
but just boxes was the result! (just using HardCoding)
I think i should mention that my envirounment is win xp and i have this problem not only in my machine, but another running xp os too. And I'm using jdk6u17.
I can be agree with you in suspecting the fonts, because encoding problem (in my experience) appears with question mark, but garbage or empty boxes related to rendering characters.
But still i have the problem, just like the first day :(
What font you use and another question i encountered is:
Why swing doesn't have any problem without specifying the font, but AWT.
Addendum: Thanks to Oscar Reyes in this page for giving this link and thanks to StackOverflow :)
They saved me! from this section i should quote:
An application using peered AWT components can only use logical font names.
and from this section should quote:
For applications using AWT peered components, Sun's JREs select fonts for Chinese, Japanese, or Korean only when running on host operating systems localized for these specific languages
Yes, you guess right! by setting the OS locale to Farsi, i got the right result.
but i still should research and see how is it possible to have the right result by not setting the right locale, from that article.
I will explain how, when i got the result, but still will listen to here. wish me luck.