SWT: draw "icons" in table or tree cells - java

We are having a Swing application which we plan to port to SWT/JFace. The Swing application draws a lot of icons (javax.swing.Icon implementations) in trees and tables (icon left to the text). If I understood it correctly, SWT only can draw images (aka graphic files). What would be the simplest solution to paint the icon or entire table/tree cell? Thanks in advance.

Images can be loaded from graphic files, or they can be drawn in-memory.
Image image = new Image(Display.getCurrent(), width, height);
GC gc = new GC(image);
// draw icon using GC
gc.dispose();
As far as displaying them in tables/trees, the most direct approach is to set the image on the table/tree item:
TableItem item = ...
item.setImage(theImage);
There is one significant drawback to this approach however: on Windows, the first time you set an image on a table item, that image's height becomes the standard height for all items in the table. So if you set a larger image, it will be scaled down to the first image's size. If you set a smaller image, it will be scaled up.
If all your images are the same size, this will not be a problem.
However if you can not predict the size of the images in advance, I recommend using the custom draw API to render your table/tree items. This approach is definitely more intensive but gives you fine-grained control over the result.

Related

How to resize an image to fill a Jpanel in BorderLayout

I added an image like this
BufferedImage eastFramerPicture = ImageIO.read(new File("src\\Images\\farmer.png"));
JLabel eastFarmer = new JLabel(new ImageIcon(eastFramerPicture));
but how can I make the image fill the panel, the image provided shows the problem that I am facing ( the colouring is used to indicate the regions of East, West, Center and south )
enter image description here.
What am I doing wrong ? Please if something is not clear comment it out, I will reply.
Thanks for your time
The answer will depend on a number of factors...
Do you want maintain the aspect ratio?
Do you want tile the image to fill the area
If you want to maintain the aspect ratio, do you want to fit or fill the available space.
The simple solution would be to use Image#getScaledInstance, but if you go that route, you should have a read of The Perils of Image.getScaledInstance
BufferedImage eastFramerPicture = ImageIO.read(getClass().getResource("/Images/farmer.png"));
Image scaled = eastFramerPicture.getScaledInstance(600, 600, Image.SCALE_SMOOTH);
You could also make use of a library like imgsclr
Example of maintaining ascept ratio
Example of tiling
Check out the Background Panel. You can display an image:
at its normal size
scaled
tiled
So try each option to see what you like best.
Or you could also use the Stretch Icon. The Icon in the label will be scaled to fill the available space.
Both solutions provide reusable code so you don't need to write custom code every time you have this type of requirement. The code is also dynamic and will adjust as the frame is resized.
Try to use setBounds(x,y,width,height);

Which image formats can be resized in JavaFX

There are about 100 jpeg & png color images used in our JavaFX-built desktop app which, when the window is resized, become stretched and blurry so I'd like to have all the graphics remade in a format that will allow them to be dynamically resized without losing quality. What image format or procedure should be used to do this?
Currently, each image is simply in an ImageView and resized as follows, but I'm open to other suggestions:
if(isSmall){
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
double sh = visualBounds.getHeight();
Scale scale = new Scale(sh, sh, 0, 0);
root.getTransforms().setAll(scale);
}
As has already been mentioned SVG is probably the way to go for you. JavaFX does not support SVG directly but you can find support here
javafxsvg and here svg-to-fxml-converter for example.
You can't resize an image to be bigger than it is without it getting blurry for most common formats. Instead make sure your images are big enough so you only need to downscale them.
The only format I ever heard of that could upscale further was using fractal compression, but AFAIK it is not in common use.

Resize image without loss of information

I was trying to do really the same functionality of resizing images like in MS Word.
I want to resize BufferedImage but I’m losing some information during process of resizing.
I tried to implement two approaches, but both produced same result.
Before any resizing:
Picture after few resize actions in my application:
First approach:
image = Thumbnails.of(image).size(w,h).asBufferedImage();
Second approach:
image = toBufferedImage(image.getScaledInstance(w, h, Image.SCALE_SMOOTH));
image is instance of BufferedImage, w is new width of image and h is new hight of image
Any idea, what I’m doing wrong?
You're constantly losing information from your image after each resizing attempt. If you want solution (like in MS Word) you have to keep somewhere original image but show only resized copy.
The best solution would be creating an object to store original image and making resized copy on demand. You can improve quality of this solution adding simple cache so you don't actually generate another resized copy of your image for every request but only if your application demands image with different height or width than last time.
I hope I helped you a bit.

Display a big number of images into a grid

I have an application, in which I want to displaying a number of image components. Each item of this will be a custom jcomponent which will has the image in a BufferedImage object to draw it in paintcomponents(Graphics g) overrided method. I will also use a JPanel for the grid with a gridlayout or flowlayout for place the custom image jcomponents, also the grid's layout will be inside an JscrollPane.
My question is what happens, when the number of the images I must to put into the grid will become big. With use of the jscrollpane, the number of the components so the number of the images must be draw will be smaller like 20-30 components, but each component inserted into the grid will have an object of BufferedImage to keep the corresponding image. This is bad for the perfomance and the memory consuption? May can use some pattern? for example if I use a main point of reference to load the images from disk to bufferedimage and depending of which rectangle of the scrollpane the user then release some bufferedimage where they putted away in the grid from the current position of the jscrollpane?
Thank you.
You're going to have to prototype the essential variables and profile the results. Then compare those results to the capabilities available on your lowest common denominator target platform. An sscce is invaluable. This one, for example, will let you easily vary N, the number of images.
Some view alternatives are mentioned here.
Noted in comment: To conserve memory, consider an LRU cache.
Thread the loading of the images, so that the UI isn't delayed by the loading
Scale the images, probably to fit the size of the panel, no point in wasting memory

How to display small size image as large size image without losing the clarity of image in android?

I want to display the background image for one image view and text view.My problem is my background image should grow and shrink depending on it's child views without losing the image quality.
If you can define specific regions within your background that should stretch versus areas that should stay anchored in the same position regardless of size, 9-patches may be what you are looking for. These are perfect for creating resizable frames for content.
You can use setDensity(int density) method on Bitmap. You can change the density every time you want to change the size of the image.
http://developer.android.com/reference/android/graphics/Bitmap.html#setDensity(int)

Categories

Resources