I have a java based tool that displays every new canvas image in a new tab, and am planing to improve it with more functions. And soon its gona become complex tool have options to create images, viewing sequence images, thus having thought of using JInternal frame rather than JTabbedPane. So would like to know which would be better option in terms of efficiency,processing power and memory?
I think it depends a lot of the size of the images and the way you actually want to display the images to users.
If you have very big images like 10+ of MPixels ones or your users don't need to see several images in the same time then you could go with the JTabbedPane. When you display only one image at a time, you can improve memory consumption by having only that image loaded into memory and probably keep the other images wrapped in SoftReference.
I'd go with JInternalFrame, illustrated here, as each frame can be resized individually. JTabbedPane, shown here, has a lot of appeal, but one-size-may-not-fit-all without scrolling. Only profiling such examples can realistically address efficiency, processing power and memory.
Related
The number of models that my swing application needs to display is currently 2000 to 10000 images. After multiple zooming or using for a period of time, the memory continues to increase and will not decrease,How can I optimize it?
It is hard to recommend anything without knowing more about the environment, but I'll list some general tips:
You resize images every time you zoom in/out. This creates new images rapidly, especially if you zoom in using the mouse wheel or some other tool that fires events rapidly. Consider creating the image after the resizing has finished.
Don't hold references to unused images. This means you shouldn't store all the resized images, only the used ones.
Probably you don't need every image in every state of the
application - consider creating a loading screen which is shown while your application loads and resizes the used images, and discards the previous ones. (It does not have to be an actual loading screen or animation)
I've never seen a small or medium-scale project which needed 10k unrelated images. You might be storing animations per-frame, which is way less efficient than using video formats.
If you have many similar images, consider not loading them from file but instead paint them dynamically (you have a base image and paint over it using the component's Graphics).
Call the garbage collector. This should free up any unused space in the memory.
Another conceptual question..
I am fighting against a java heap space exception.
My problem is, that i have to render/display small but a lot of images.
I exceed maximal ram. (edit vm options does not help) Yet i store all images in one List.
I think about if it may need less ram if i render the images one by one and store it temporary on local hard disk.
Will the images be still all in ram if displayed on screen?
Changes in the code would be significant so i want to ask this before..
If your use case is lots of images/icons in a JList/JTable inside a JScrollPane, I once wrote something similar for a CMS search result display:
I had thumbnails of larger images stored on server/cached on disk. You can exploit the fact that the JList/JTable (or Swing components in general) will only try to paint the components visible on screen, so you don't need to track this yourself. I used a JLabel with ImageIcon as renderer in the list, and a fixed size cache of icons in memory. Whenever the renderer hit an image not in cache, I used a lazy-loading technique with a placeholder icon returned immediately, while loading the icon in the background using a SwingWorker. When loaded, the SwingWorker issued a repaint to JList. The icon would now be in cache, and the list would paint nicely.
Some bonus tips: Make sure your icon cache is larger than the maximum number of items that can be displayed on screen. Also, use a limited (fixed size) thread pool for your SwingWorkers (possibly loading multiple icons at once), to avoid exessive reapint-loops that may cause flickering.
I'm currently working in a map editor for a 2D, tile based game. When I create a new map, the tileset is loaded into memory and displayed in a JPanel that's inside a JScrollPane, so I can choose the tiles I wish to draw.
At first, I was just drawing the full image, but that made scrolling pretty slow, so now I only draw the visible portion of it and that works just fine. What I'm worried about is the memory usage, because the tileset is pretty big. I'm not getting any OutOfMemory erros, but I would like to optimise everything I can.
I tried using ImageReader together with ImageReadParam.setSourceRegion(); and that uses a lot less memory, but the scrolling becomes slower and the image blinks like crazy when scrolling - probably because it takes some time to read the image form disk.
So, is there a good way to quickly load parts of a big image without fully loading it? Or maybe there's a way to fully load it but in a compressed manner?
I'm trying to develop a Java program that does the following:
uses a background picture of a network diagram of clients
positions an image of a bandwidth graph to respective client
refreshes the image of graph every 5 seconds which it fetches from another program that constantly produces snapshots of bandwidth
Now, I can set the background picture and I'm pretty confident that I will be able to refresh the images by using a timer. What I am trying to plan out is how I am going to position these images to the respective clients which is displayed in the background picture. I did some research and it appears that I have several options but I want to make sure I am choosing the right one before I run into problems further down the line.
It seems to me that using a GridBagLayout would be the choice for me, however I would like a second opinion for a more experienced population. If using a GridBagLayout is the correct choice, could you recommend any good tutorials that would help me understand this Layout Manager? Please keep in mind that I have limited experience with Java, especially with GUI oriented Java.
EDIT: If I am not explaining the concept well enough please let me know.
check this out-
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
GridLayout is likely to meet your needs and it is easy to use - the assumption I make is that image of bandwidth graph is the same/fixed ...
Look for an example here
http://www.roseindia.net/tutorialsearch/?t=java+gridlayout+color
Try Google the keyword "GridLayout" - there are plenty of hits ...
If you want to do advanced layouts, you should take a look at MigLayout.
It's an extremely flexible layout manager that can pretty much act as a replacement for any/all of the existing Swing layout managers. It's worth using if you want to do difficult / complex layouts.
It feels more like a HTML table layout tool, enabling multi-column spans etc. and arbitrary scaling rules.
The "Quick Start Guide" is very good with lots of examples - I'd suggest taking a look at that if you want to evaluate MigLayout for your project.
If the 'bandwidth graph' image(s) is on top of the BG image, look to OverlayLayout
A layout manager to arrange components over the top of each other. ..
OTOH I would not use a layout strategy for this, instead..
Create a BufferedImage the size of the BG.
Paint the BG to the image.
Display the image in a label.
When it comes time to paint the graphs, get a Graphics instance for the image in the label, and paint them to that. If the graphs are always the same size and shape and do not have transparency, you can simply paint them. Otherwise, draw the BG first.
I have an Swing app whose main panel is divided into a 3x2 grid of charts, and the app can be resized, with the charts (JFreeChart) auto scaling. One of these panels I would like to display the Apdex rating in, which is just text (e.g. '0.89 [0.5]*'). We use the application to display on a monitor visible to everyone, and scale multiple instances of the app that monitor different data centers. Scaling the Apdex font size to fit available panel space is what I'm after.
Any good ideas?
After re-reading and rethinking the question I would suggest for you to try calculating it yourself by use of FontMatrics stringWidth with the string and iteratively increasing the font size until you can, i.e. the size evaluated by you versus the available space.
A ready algorithm would be nice but I didn't hear of any.
Good luck, Boro.
I'd render it off-screen at some suitably large point size, as shown here, and then down-sample it using AffineTransformOp, as shown here.