ImageIcon shows wrong image on retina display - java

I am loading an image from an URL like this:
new ImageIcon(new URL(url))
The url is something along the lines of http://example.com/image.png, so an image on the internet.
This works just fine, however I've been getting a few reports of users of my program that they are getting strange images instead of the regular ones when using the program on a retina display on Java 8u20 (and maybe later).
I determined that the strange images they are getting is an image that the CDN the images are loaded from automatically redirects to if the image wasn't found. After some research, I found that newer versions of Java apparently automatically add #2x into the url when using the Toolkit.getImage(URL url) method (https://bugs.openjdk.java.net/browse/JDK-8011059), which is used by new ImageIcon(URL url).
The problem of course is that the CDN actually returns a valid image if the image isn't found, so Java probably thinks it actually found a higher resolution image to use on the retina display. Other images in my program that also use ImageIcon in the same way, but are loaded from another source, seem to work just fine.
The question is now, what would be the best way to resolve this? I don't have access to the CDN, so I have to prevent Java from trying to load the second URL somehow (if that is actually the problem, but it certainly seems that way). Unfortunately I don't have a computer to test this on, so it's a bit hard to tell which solution will work.

Related

Android - Osmdroid 6.0.2, switch from SqlTileWriter to TileWriter (local storage)

I am playing around with Osmdroid for use in an application. I already have a tiles server and want to use those tiles. Looked into docs used setTileSource(new XYTileSource()) magic happened. I have to mention that the documentation is a bit stale on many parts.
The tiles of the aforementioned server are already used in the app in a different feature. As logic dictates I want to use the same cache location so that I do not have duplicates of the tiles in the system. Again through docs, used Configuration.getInstance().setOsmdroidTileCase(new File("path")) and again everything is going just fine. This was not so easy though as the documentation refers to the Osmdroids Constants class which is changed in the latest stable.
Then last part is to change from the SqlTileWriter to a normal TileWriter as we already have the image files downloaded from the other feature. This is where I cannot figure it out. I am using:
MapTileProviderBasic tileProvider = new MapTileProviderBasic(getActivity(), new XYTileSource("My Tiles", 5, 16, 256, ".png",
new String[]{"SERVER_URL"}), new TileWriter());
mMap.setTileProvider(tileProvider);
and things get weird. The images are actually downloading but the map shows just the loading background. In the logs I got StorageUtils telling me that the storage is not writable. Gave permissions to the app, now writable, still nothing. I noticed that except from the tiles folder the .db files have been created but the tiles table is empty. I went back to the above code and replaced the new TileWriter() with a new SQLTileWriter() just for sanity check and the map works just fine, without any permissions.
So the main question.
How do you change from SQL to storage TileWriter?
For anyone who might get stuck with this, I opened an issue in Osmdroid's Github page where we found and solved the issue. Take a look at this link for more info.

How to Hide / Protect Media Folder in Java Project?

I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.

pull path of the screen image

I'm trying to pull the path of a screen image I'm looking at on the android phone of the app im testing. The app shows various pictures. I want to pull the path of the picture to pull the picture name, but have no clue how to start. Any guidance?
edit:
I'm trying to find out if I could pull the path from the app code to see which image the user is viewing. for example. if im looking at an image, I want to know whats the path of the image (path should be coming from someone's harddrive)
there's not necessarily a path for an image. it's better to say the source of the image, be it a file, an HTTP stream, or otherwise. and unfortunately, that info is lost once it's set into an ImageView.
to set the image bits into an ImageView, you construct a Bitmap, usually using one of the decode*() methods in BitmapFactory. that is what determines the source of the image. in your unit test framework, you can get a handle to the ImageView by getting the Activity and calling findViewById(), but that's not going to help you.
there may be other places for you to hook into the code to determine that sources for the image data, but i can't say without knowing your application source code.
you could ask your developers to make the source available in the ImageView's tag (see setTag() / getTag()). that way you could pull it out in your unit tests. of course, you'll have to have an understanding with them as to what the source will be (a URL, a database URI, a file path, ?).

Adding a path for a ImageIcon - Java

Hey So I'm pretty much awful at java and I want to add a ImageIcon now I've done this before and I even have the images to work the only issue is the program I am making is for University work and when I submit the work it will be submitted online through a .rar file.
So my issue is currently the image's have a huge direct path so i.e.
C:\Users\MY-NAME\Documents\NetBeansProjects\UNI-PROJECT\src\IMAGES\image.png
Since they will be reviewing my work on their work PC or home PC whatever the images wont work for them since it's using my home reference. How can I reference a image so they can open it where ever and the images will still work?
Thanks in advance
-SKENG-
The Java Tutorial on How to Use Icons has a small section explaining how to use Class#getResource() when the image is included with the application - look at the createImageIcon() method and the description that follows it.
You can put your images in the current folder or subfolders and then use the following path (for example):
System.getProperty("user.dir") + "images\pic1.gif"
Simply do not use absolute path, and put the images into a subdirectory where is located your sources or binaries.

Creating online help in Java Swing application - using pdf user docs

I am trying to link some user docs to my Java Swing application. The user docs are pdf and I would like to be able to click on a button in my application which opens up some sort of pdf renderer to display my userdoc.pdf.
I do not want to use the runtime exec command
I have tried the following with not much success --
pdfRenderer -- this seems to not work for the current version of pdfs (seems to want older pdfs)
acrobat viewer -- I keep getting font errors (the document is displayed but everytime I go to a new page, I get an error)
JPedal pdfHelp - This sounds cool and if it will work, it would be the best for me. But for some reason, I am unable to open any file here. No pdf file that I have loads in the window (The pdfHelp panel comes up with a list of pdfs as promised, but when I right click one of them and say "OpenPDF", I just get a progress bar and the file does not load, I have tried a few files on this and checked file permissions)
JPedal SimpleViewer -- So far this one seems to be the most promising. I would like to remove some of the functionality of this viewer though and am not able to edit the xml to do this. When I edit the xml, then I seem to get xml parsing errors.
Has anybody done anything similar that worked? Help please
PS. I need this to work on Linux machines (IcePDF seems to works only for Windows). I would very much prefer something that is free!
Would Java's Desktop API meet your requirements? It does launch another process, but it's not through you calling Runtime.exec().
The Desktop API uses your host operating system's file associations to launch applications associated with specific file types.
Update: pdfHelp from IDR solutions (creators of JPedal) works now. The latest version (4.0) has fixed the bug and it reads my pdf files (and I am sure more of the latest pdf files). Just playing around with the product today and it is great! There are few minor bugs but I really appreciate how quickly Mark Stephens(?) responded to the first bug. Good work guys!
I chose pdfbox for a similar use case - it's not perfect with every PDF, but works pretty well and is under active development. From the PDDocument you can get a list of PDPage objects, which have a convertToImage() method that gives you a BufferedImage you can draw on screen. I switched to this from pdf-renderer because I felt it gave better results in general.
PdfHelp has moved to its own domain at http://www.pdfhelp.org and have been updated to fix your issues and add some other improvements...

Categories

Resources