Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Thumbnails of files
Manoj Paul
Ranch Hand
Posts: 112
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi everybody
What should i do to view all image files as thumbnails on the right side of my application When i click a folder on the JTree which is on the left side of my application?.. What are the components should i used?
Please help...
Craig Wood
Ranch Hand
Posts: 1535
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here's one way you could set it up.
import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ThumbnailPreview { Dimension size = new Dimension(80, 60); JPanel panel; ThumbnailPreview(BufferedImage[] images) { // Send in images obtained from your tree file // and scale them to the specified size. BufferedImage[] scaledImages = scale(images); // Mount scaled images in a JPanel. buildPanel(scaledImages); } private BufferedImage[] scale(BufferedImage[] images) { int w = size.width; int h = size.height; int type = BufferedImage.TYPE_INT_RGB; BufferedImage[] scaled = new BufferedImage[images.length]; for(int j = 0; j < images.length; j++) { double xScale = (double)w / images[j].getWidth(); double yScale = (double)h / images[j].getHeight(); double scale = Math.min(xScale, yScale); // scale to fit //Math.max(xScale, yScale); // scale to fill // Center the scaled image in the new BufferedImage. double x = (w - scale*images[j].getWidth())/2; double y = (h - scale*images[j].getHeight())/2; AffineTransform at = AffineTransform.getTranslateInstance(x, y); at.scale(scale, scale); scaled[j] = new BufferedImage(w, h, type); Graphics2D g2 = scaled[j].createGraphics(); // Fill background for scale_to_fit images. g2.setBackground(UIManager.getColor("Panel.background")); g2.clearRect(0,0,w,h); // Scale the image on-the-fly and draw it into // the new BufferedImage. g2.drawRenderedImage(images[j], at); g2.dispose(); } return scaled; } private void buildPanel(BufferedImage[] images) { panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; int cols = 4; for(int j = 0; j < images.length; j++) { JLabel label = new JLabel(new ImageIcon(images[j])); gbc.gridwidth = ((j+1)%cols == 0) ? GridBagConstraints.REMAINDER : 1; panel.add(label, gbc); } } public JPanel getPanel() { return panel; } }
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Thumbnails of files in Applets
Want to view all image files as thumbnails
webview not responding to touch events
ImageIcon problems and X11 -- Part 2....
Thumbnails of files in Applets
More...