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
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Swing / AWT / SWT
how to streach an image all over the button
Peter Primrose
Ranch Hand
Posts: 755
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi there,
I got an image where I would like to strech it all over the button.
in the code below - it place it in a flow mode (and i would like it to be BorderLayout).
exit = new JButton();
exit.setIcon(new ImageIcon("images/Exit.gif"));
thanks for any help
peter
Craig Wood
Ranch Hand
Posts: 1535
posted 19 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 javax.imageio.ImageIO; import javax.swing.*; public class ScalingExample implements ActionListener { JButton left, right; public ScalingExample() { BufferedImage image = getImage(); left = new JButton(new ImageIcon(image)); left.addActionListener(this); right = new JButton(new ImageIcon(image)); right.addActionListener(this); } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if(button == left) scaleLeft(button); if(button == right) scaleRight(button); } private void scaleLeft(JButton button) { // assuming the button icon is an ImageIcon Image image = ((ImageIcon)button.getIcon()).getImage(); int w = button.getWidth(); int h = button.getHeight(); double width = image.getWidth(button); double height = image.getHeight(button); double xScale = w / width; double yScale = h / height; //double scale = Math.min(xScale, yScale); // scaleToFit double scale = Math.max(xScale, yScale); // scaleToFill int sw = (int)(scale * width); int sh = (int)(scale * height); // assuming you want a jpg image int type = BufferedImage.TYPE_INT_RGB; BufferedImage scaled = new BufferedImage(sw, sh, type); Graphics2D g2 = scaled.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); AffineTransform at = AffineTransform.getScaleInstance(scale, scale); g2.drawImage(image, at, button); g2.dispose(); button.setIcon(new ImageIcon(scaled)); } private void scaleRight(JButton button) { // assuming the button icon is an ImageIcon Image image = ((ImageIcon)button.getIcon()).getImage(); int w = button.getWidth(); int h = button.getHeight(); double width = image.getWidth(button); double height = image.getHeight(button); double xScale = w / width; double yScale = h / height; //double scale = Math.min(xScale, yScale); // scaleToFit double scale = Math.max(xScale, yScale); // scaleToFill int sw = (int)(scale * width); int sh = (int)(scale * height); Image scaled = image.getScaledInstance(sw, sh, Image.SCALE_AREA_AVERAGING); MediaTracker tracker = new MediaTracker(button); tracker.addImage(scaled, 0); // load the new image try { tracker.waitForID(0); } catch(InterruptedException ie) { System.err.println("interrupt: " + ie.getMessage()); } button.setIcon(new ImageIcon(scaled)); } private BufferedImage getImage() { BufferedImage image = null; try { URL url = getClass().getResource("images/redfox.jpg"); image = ImageIO.read(url); } catch(MalformedURLException mue) { System.err.println("url: " + mue.getMessage()); } catch(IOException ioe) { System.err.println("read: " + ioe.getMessage()); } return image; } private JPanel getPanel() { JPanel panel = new JPanel(new GridLayout(1,0)); panel.add(left); panel.add(right); return panel; } public static void main(String[] args) { ScalingExample ex = new ScalingExample(); JFrame f = new JFrame("click button to scale image"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(ex.getPanel()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Peter Primrose
Ranch Hand
Posts: 755
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
ohhhh....thank you sir
A feeble attempt to tell you about our stuff that makes us money
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
displaying image over button
Display arrow button over label icon on focus
JavaFX Button: You'd think this would be simple....
Refresh problem in applet
fit the image to the button size
More...