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
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
Resizing Icon for a button(urgent)
Sham Grandhe
Ranch Hand
Posts: 73
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Friends,
Iam presently working on a swings project and want to
resize an Icon that is being placed on a button or a label.
can some one plz help to do this.
Thank you
regards
sham
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import java.util.Random; import javax.imageio.ImageIO; import javax.swing.*; public class IconResizeTest { JButton[] buttons; Random seed; BufferedImage[] images; // assuming all images int origWidth, origHeight; // have same dimensions public IconResizeTest() { buttons = new JButton[3]; seed = new Random(); loadImages(); origWidth = images[0].getWidth(); origHeight = images[0].getHeight(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(getNorthPanel(images[0]), "North"); f.getContentPane().add(getSouthPanel(images[1], images[2]), "South"); f.setSize(400,275); f.setLocation(200,200); f.setVisible(true); } private void resizeIcons() { for(int j = 0; j < images.length; j++) { BufferedImage resized = resizeImage(images[j]); buttons[j].setIcon(new ImageIcon(resized)); buttons[j].getParent().validate(); buttons[j].getParent().repaint(); } } private BufferedImage resizeImage(BufferedImage in) { double scale = (20 + seed.nextInt(81))/100.0; // 20% -> 100% int w = (int)(origWidth * scale); int h = (int)(origHeight * scale); //System.out.println("scale = " + scale + "\tw = " + w + "\th = " + h); BufferedImage out = new BufferedImage(w, h, in.getType()); Graphics2D g2 = out.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(buttons[0].getBackground()); g2.fillRect(0, 0, w, h); AffineTransform at = AffineTransform.getScaleInstance(scale, scale); g2.drawRenderedImage(in, at); g2.dispose(); return out; } private JPanel getNorthPanel(BufferedImage bi) { buttons[0] = new JButton(new ImageIcon(bi)); buttons[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resizeIcons(); } }); JPanel panel = new JPanel(); panel.add(buttons[0]); return panel; } private JPanel getSouthPanel(BufferedImage bi1, BufferedImage bi2) { buttons[1] = new JButton(new ImageIcon(bi1)); buttons[2] = new JButton(new ImageIcon(bi2)); JPanel panel = new JPanel(); panel.add(buttons[1]); panel.add(buttons[2]); return panel; } private void loadImages() { String prefix = "file:tumbling duke/T"; int[] ids = { 2, 6, 9 }; images = new BufferedImage[ids.length]; for(int j = 0; j < images.length; j++) try { URL url = new URL(prefix + ids[j] + ".gif"); images[j] = ImageIO.read(url); } catch(MalformedURLException mue) { System.err.println("url: " + mue.getMessage()); } catch(IOException ioe) { System.err.println("read: " + ioe.getMessage()); } } public static void main(String[] args) { new IconResizeTest(); } }
Sham Grandhe
Ranch Hand
Posts: 73
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi Craig Wood,
Thank you, your example will help me a lot.
regards
sham
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
simple web browser in java
How to andomly pick a button in a 4X4 GridLayout
what types of image is suported in java
click ' a picture'
Problem with the JButton.
More...