Reading and storing Java Icons - java

In my Java application I'm planning to use a lot of ImageIcons which will be shown in JLabels. The size of a single .png file is about 8 kB. During runtime, icons will be shown in JLabels, but the icons shown in the labels will change often, using .setIcon(icon).
Should I load all those icons at once when the program starts and store those in an array (length would be > 150) or should I load the icons every time I change an Icon as shown in the code below?
Note that the number of JLabels is rather small, so, at a given moment, only a selection of icons is shown.
ClassLoader cl = getClass().getClassLoader();
String path = "somePath";
URL url = cl.getResource(path);
Icon icon = new ImageIcon(url);
The application will be a jar file with the resources inside the jar.

This will be a bit cliche, but "Do not optimize your application unless it needs to be optimized." It makes maintenance more difficult in future.
Because, every program evolves in time, and the kind of the optimization required may not be the one you are planning to do at the moment. Your JLabels may increase in number, or your possible icons may increase. Or some other new feature comes up and you may need to display all the icons at the same time etc. etc.
So, just write your code as clean as possible, and when you see that an optimization is really required, do it after measuring where is the bottle neck.
PS: I am telling this, because you write "I am planning to use" so you did not actually finished the application and saw the effect of this many icon loadings.
PS2: OS and Disk caches are getting better everyday, so it may just be enough for the time being.

Related

How to save images with Picasso so there is no loading time?

I have an app where I display a quote and there is a picture in the background. When I click on a "next"-button, the next quote will be shown and the new image will be loaded. The loading takes around 0.5 seconds and has the image before as placeholder. But when I switch back, there is no loading time.
This means, the image is saved somewhere, so it does not need to be loaded again. Unfortunately, this is just temporarily. When I get to see the next 5 pictures and go back to the first one, the first picture needs to be loaded again. So I tried loading all the pictures in the beginning (it is just 25 pictures), like this:
Picasso.get().load(backgrounds.get(1));
Picasso.get().load(backgrounds.get(2));
Picasso.get().load(backgrounds.get(3));
Picasso.get().load(backgrounds.get(4));
Picasso.get().load(backgrounds.get(5));
Picasso.get().load(backgrounds.get(6));
Picasso.get().load(backgrounds.get(7));
and when I click on my "next"-button, I use this:
Picasso.get().load(backgrounds.get(counterBackground)).fit().noPlaceholder().into(background);
But both things without the expected effect. The images need a loading time around 0.5 every time and "noPlaceholder" doesn't work like expected, the placeholder is still there.
So, does someone know how to cut the loading time? Like, how to load all images in the beginning?
Thanks for every answer!
You can create cache while using Picasso, this will lead to quicker loading times (as long the images are cached
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
Picasso.setSingletonInstance(picasso);
source
You can use indicators to see from where the image is loaded by using
setIndicatorsEnabled(true)
source
You can also use callbacks to have an indication when the image is completed loading
you can see more details here
Edit
You can use
Picasso.get().load(R.drawable.pic).into(imageview)
I think it will work also for mipmap, usually mipmap are used for errors / place holders for your resources (if you are not using drawables)
2.
You can create your own policies about how to cache/load your images
Link

Libgdx - Remove HTML greybackground/recompile button/ load splash

What is the proper way to remove the grey background that covers the entire screen, the recompile button, and the default libGDX load and/or load splash in a HTML build of my game?
Note: This answer applies only to the gdx-setup tool as of late 2022. The gdx-liftoff tool is similar but has a slightly less boneheaded configuration out of the box. Additionally, I would like to get some of libGDX's HTML backend reworked one day, as there is no point in the padding and it's applied unevenly, plus less obvious things like the way it creates a table for layout.
Grey background
The background colour can be customised by changing background: #222222 in html/webapp/styles.css to some other colour. Or apply it directly to the body in index.html and delete styles.css (plus the link to it) as it doesn't contain anything important once the superdev button has been removed.
Grey border
The border around the game can be removed by editing HtmlLauncher like so:
#Override
public GwtApplicationConfiguration getConfig () {
GwtApplicationConfiguration config = new GwtApplicationConfiguration(true);
config.padHorizontal = 0;
config.padVertical = 0;
return config;
}
Separating GwtApplicationConfiguration into a config variable brings it in line with the other launchers (desktop, Android, iOS) and setting the padding to 0 is self-explanatory. While we're here, passing true into the app config's constructor tells it to render at native resolution on high-DPI/"retina" displays instead of upscaling.
Recompile button
Or the superdev button, as I call it. Just remove the <a class="superdev"... line from html/webapp/index.html. If you need access to it during development, it's recommended you add its link to your bookmark bar. Visibility of the bookmark bar can be toggled using Ctrl+Shift+B in Chrome and Firefox.
Load/splash screen
You're probably best referring to https://libgdx.com/wiki/html5-backend-and-gwt-specifics#changing-the-load-screen-progress-bar for this (which may not have existed when the question was asked). In short, getPreloaderCallback() and adjustMeterPanel() can be overridden in HtmlLauncher. I typically just overwrite logo.png after building instead of using the recommended method for changing the logo.
Other changes
Things you might want to change before a final release:
styles.css isn't very important beyond changing the background colour, as noted earlier.
In index.html, a comma should be added to between device-width and initial-scale for it to be valid HTML.
In index.html, applying align="center" to a div is deprecated behaviour. Probably best remove that alignment. If you need it, apply via CSS instead.
In index.html, handleMouseDown() and handleMouseUp() are completely pointless, as far as I can see. I don't use them for my own projects and have had no complaints.
html/build/dist/assets/assets.txt references some files that may not be necessary. The default font (arial or lsans, depending on libGDX version) is only needed if you use it and the vertex/fragment shaders are only needed if you do 3D stuff, I believe. Removing these can remove load times ever so slightly, especially on HTTP/1.1 connections. But I don't have an automated way to remove those lines (except on Linux - head -n -8).
Setting an asset filter as seen at https://libgdx.com/wiki/html5-backend-and-gwt-specifics#speeding-up-preload-process is an easy way to reduce your load times. I return false for music files to reduce load times greatly - it ends up streaming music instead of preloading it (if using Music, not AssetManager).

If an image is tampered with some additional content, how to remove that additional content from the image in Java?

I want to know if there is any solution for the following scenario:
I have an application which uploads the files, after scanning and transcoding them, onto a server. Suppose, an image file is being uploaded which has been tampered with some additional contents over it. Now, as the uploaded file is illegitimate, I want to remove the additional tampered contents and upload just the original part of this image file. Is it possible to do so in Java?
Thanks.
It's not possible to detect in the general case, but there are some heuristic methods available to determine whether an image has been edited. Try using the tools at http://imageedited.com/ to get an idea of what's possible.
Removing the edit is a much more difficult problem, which is probably impossible with current methods.
I'm just speculating here, and I don't know how well it would work in practice, but you could do it if you limit to specific sources of tampering. E.g., suppose you want to remove the logo added to an image by memegenerator.net.
You know in advance what the text looks like and where it is. Create a transparent png template that matches the text. Then sum the differences between the image and template pixel colors, multiplying each by the alpha of the template pixel. Since for this particular logo, it's basically white (although it seems to have a thin black shadow) you would get false positives for a picture with a white part there, so you'd also need to verify that the surrounding pixels are (within a tolerance) not white. It's not clever but it could work for certain sites.
For anything more flexible (e.g., logos on images which have subsequently been resized) you're in to the territory of OCR and TinEye-like image matching, which are more advanced than I could advise you on.
To correctly detect all kinds of "tampering" and filter "illegitimate" from "legitimate" in general, you'd need an artificial intelligence that could understand the meaning and context of what it's seeing. The short answer is: you can't. That's what humans are for.
If this is for a website, probably the best thing you can do is a report button that lets users of your site report images that don't fit with your site's rules.

Java | I need to make an applet but I don't know how to begin

I need to make kind of a game in Java but I am a total beginner. It has to be applicable to the web and I need to use images on it and action listeners. So, do you recommend any site to know how to begin?
The description of the game (it is not really a game but it implements things that usually are in a game) is this:
Show a matrix of images of 3x3 elements, then, hide them and put instead empty squares. The images shown in the matrix, must remain in the lower part of the screen just below the empty squares and they must be randomly positioned. The user, must click one image and put it on the correct empty square. The result, must be, how many images were correctly positioned, the time it took to end the game, the time between mouse clicked and released for each image.
For additional information, I want you to know that this application is for a friend of mine who studies medicine. He wants this program to test patients who accident and receive hits on their heads. You may think that the description I gave you may not be a good software for that purpose, and in fact, it may be not, but, once I know the management of all that is required (Images, MouseListeners, how to introduce it to a web etc), I will be able to make a better product. So, please tell me, how can I begin?. What do I need to know?
I would start here. Except for some startup boilerplate and the restrictions of the sandbax (which, based on your description, you will b unlikely to encounter), there is no fundamental difference in an applet from normal code.

Absolute keyboard input position (blinking cursor)

Is it possible to make Java program that will determine the absolute position (x,y on screen) of blinking keyboard cursor? That cursor can be in any text editor.
Not easily, since the position of a cursor on a screen of a program is merely a data point inside that program and rendering that cursor is a method running in the program. (I'm reading your question to mean that the program/editor in question is a wholly separate process from your Java program, right?)
You can theoretically do it if:
The program explicitly exports via some API calls the cursor location
You capture the actual screengrabs of that program very fast, analyze the image difference, and deduce cursor location from appearing/disappearing rectangle or short line in case the screen of the program didn't change save for cursor blink.
If you need relative (e.g. in # of chars instead of # of pixels) location in an editor, then somehow use edge recognition to detect editor size, some advanced image processing magic to deduce font size, and compute character-based offsets.
If the editor is using standard OS (e.g. Windows) APIs to draw both editing window AND the cursor, it might theoretically be plausible to hook into the system to intercept those calls - i don't posess nearly the amount of knowledge of what those APIs might be or if such even exist, how possible it is to intercept them, and whether Java programs are able to do that even if it's possible theoretically.
The question here is not whatever is possible or not, the question is for what purpose should i want to do this? Frankly, i don't see any reason, and i am sick of annoying mini-apps who act like spyware. Are u trying to duplicate the existent app into another form of mass terror?
I beg your pardon if u find my comment offensive, but this is my answer. Another question : why?

Categories

Resources