What are the differences between using a Canvas control or a Label control for drawing an image?
Both controls seem to have similar capabilities in terms of drawing images onto their displayable area, and can both use PaintListeners to further customize the act. I have always assumed that Canvases are more suited for the purpose than labels simply due to their name, but I cannot determine the actual practical differences. In what scenarios would you use one over the other for loading an image file and painting the image onto the control?
The Label inherits its drawing capabilities from Control, as well as many of the features in Canvas. But Canvas was built with extra controls for images, like dynamic drawing, buffering and partial update.
Summing up, it is ok to use labels for static small images, but when it comes to heavy-duty use, a canvas is more appropriate.
Related
In what instance would I want to use ImageIcon to represent a picture file rather than an Image object? I've been searching and I've seen people say that you would use an ImageIcon object when dealing with images that will be part of the GUI, but I still don't understand the implications of this. In other words, what is the actual difference between the two object types and what situations are they each suited for?
Image is an object that represents a bitmap: an array of pixels of different colors.
Icon is an object that can draw a rectangular piece of graphics. Since it is an interface (and a simple one too), you can imagine many different types of icons: drawing predefined vector graphics, generating images on the fly etc. Therefore it is a useful abstraction and is used by Swing components (buttons, labels).
ImageIcon is an object that IS an Icon, but HAS-A Image. That is - it draws graphics based on a specific image.
When you say "why should I be using an ImageIcon instead of Image" you miss the point: in fact you are using an Image either way.
An Image is an object representing the data model of a picture. An ImageIcon is a Swing component that draws an Image on the screen, and you have to provide it with the appropriate Image to draw (either by passing in an existing Image or by giving it enough information to find and load the image).
The relationship is similar to that between a String and a JTextField; one is the representation of the data, and the other is the graphical component that draws it on the screen.
The implementation is to not hold up the Swing thread.
Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.
Basically, then you can set an ImageIcon for a button without actually forcing it to be loaded beforehand.
This can be seen by having a very large icon and setting the Frame's icon to this image. Once set visible, it may take a few seconds to actually appear.
The touch-screen application I am developing will be used on different screen resolutions. I never had to worry about this before, but now I do. I am wondering how can I design the GUIs so that EVERY object on the GUI resizes proportionally to fit the screen resolution? Can you refer me to a good tutorial page? I am designing the application using the NetBeans and the Swing framework. Thank you
I am wondering how can I design the GUIs so that EVERY object on the GUI resizes proportionally to fit the screen resolution?
not easy job you have to test all possible pixels ratio that exist (by default is possible to change and set various pixels ratio on todays fullHD screen) with lot of fails
this is reason why LayoutManagers exist,
I'd suggest to use NestedLayout
sure there are custom LayoutManagers, for example by using MigLayout (something betweens GridBagLayout and SpringLayout) is possible to put all JComponents to the container from one place, but again I'd be suggest use NestedLayout instead
you have to set (internally) minimum screenSize for displaying contents in the JScrollPane, for example screen with resolutions less than 1024 x 600 will have the content scrollable, otherwise let's this job for LayoutManagers,
this idea required model JFrame ---> JScrollPane ---> JPanel (father panel) in all cases, JScrollPane will protect your GUI against small (and dual too) screen resolutions
you have to look at FontMetrics for Font, because you need in/decrease Font size for various pixel ratios continiously with JComponents size on the screen
same with Icons if exist, prepare that before in some of Graphics SW's, not resize on runtime
my view ---> any Framework based on AWT/Swing isn't advantage for job this nature,
Give that I have written a JPanel with many different components on it, is there a way to apply an overall "dilate" ability on the panel so that everything in it stretches proportionally when I resize my window?
That is, if I manually resize my window to be 1/4 the size, everything in the panel should also shrink by 1/4 so the new panel is just a dilation of the first. Given that I have not designed the individual components inside to do this (there are many) is there any easy way to make the panel behave this way?
UPDATE: In order to be more clear on the solution I need, I will describe the panel contents:
The panel is a "game" of sorts, with a single null-layout and dozens of ImageIcons flying around the screen at any time. The ImageIcons are preloaded PNG files, which already have a permanent size. Of course, I could manually resize each ImageIcon and reposition them relative to window size, but that would involve recoding many components.
There are no buttons or text to worry about, so what I'm really looking for is some kind of "postprocessed" resize where the panel simply shrinks whatever's rendered by some porportion (think of resizing an image in Photoshop).
One option is of course to give up swing all together and use some 3rd party widget component library which draws itself using any Graphics. Then you can either draw the widgets on the image and resize the image, or, better yet, apply a transform to the graphics object you pass to the library.
If you do want to stick with swing there is the SwingUtilities.paintComponent method, which you could use to paint the Panel onto a BufferedImage which you could then resize. (I've used this myself to do some nice transitions between "views" in a game.)
The problem is of course that you somehow need to translate the user input accordingly. I have no solution for this right now, but the above perhaps helps you in some way.
You can try to override paintChildren() method of the panel and scale graphics to achieve desired visible size.
You could try J(X)Layer, see http://www.pbjar.org/blogs/jxlayer/jxlayer40/
Using layout managers instead of absolute positioning of the widgets will give you this behaviour. See the oracle tutorials: Using Layout Managers.
Do you really want fonts to resize on resize events? I don't know a layout manager which will do that for you.
I have an application that displays plots using JFreeChart. As it happens, these plots are big and JFreeChart is not terribly fast, resulting in atrocious redraw times, particularly when resizing the plot.
What I am after is a way to stretch an image representing the plot while resizing (a bit like the iPhone will present a screenshot of a stopped application while it gets started again), and perform a full redraw only after the user has released the mouse (i.e. once the final size of the plot is known).
The interaction features of the chart sould stay intact (when not resizing, obviously).
Is there a generic solution / Swing wrapper for this? (there is no reason why it should be JFreeChart-specific).
Cheers
No concrete answer, just a possible strategy
on starting the resize, paint the plot into a BufferedImage and show and resize that image instead of the plot
on stopping resize remove the image and show the plot again
in JDK 7, you can use a JLayer for the image/manipulation.
Edit
Alternatively (for JLayer), you could use a CardLayout: showing one card with the image while resizing and the another card with the plot while not resizing. SwingX ImagePainter can do the image scaling during the resize
I have a graphics system for Java which allows objects to be "wallpapered" by specifying multiple images, which can have (relatively) complex alignment and resizing options applied.
In order to perform adequately (esp. on very low powered devices), I do the image painting to an internal image when the wallpaper is first painted, and then copy that composite image to the target graphics context to get it onto the screen. The composite is then recreated only if the object is resized so the only work for subsequent repaints is to copy the clipped region from the composite to the target graphics context.
The solution works really well, except that when I have PNG images with alpha-channel transparency the alpha channel is lost when painting the composite - that is the composite has all pixels completely opaque. So the subsequent copy to the on-screen graphics context fails to allow what's behind the wallpapered object show through.
I did manage to use an RGBImageFilter to filter out completely transparent pixels, but I can't see a solution with that to making blended transparency work.
Does anyone know of a way I can paint the images with the alpha-channel intact, and combined if two pixels with alpha values overlap?
What type of Image do you use for the composite image?
You should use a BufferedImage and set it's type to TYPE_INT_ARGB which allows translucency.