Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course you can resize the image there are many different ways like <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance%28int,%20int,%20int%29" rel="nofollow noreferrer"><code>Image#getScaledInstance(int width,int height,int hints)</code></a>, but <a href="http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html" rel="nofollow noreferrer">this has its perils</a>. </p> <p>The main problem being:</p> <blockquote> <p>Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.</p> </blockquote> <p>I would not recommend using it but <a href="http://www.java2s.com/Code/JavaAPI/java.awt/ImagegetScaledInstanceintwidthintheightinthints.htm" rel="nofollow noreferrer">here</a> is a nice example.</p> <p>Alternatively you can use this method:</p> <pre><code>import javax.swing.ImageIcon; import java.awt.image.BufferedImage; import java.awt.Image; import java.awt.Color; import java.awt.Graphics2D; import java.io.File; import javax.imageio.ImageIO; import java.awt.RenderingHints; public class ImgUtils { public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) { BufferedImage bi = null; try { ImageIcon ii = new ImageIcon(filename);//path to image bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null); } catch (Exception e) { e.printStackTrace(); return null; } return bi; } } </code></pre> <p>you'd use it like:</p> <pre><code>final BufferedImage img=new ImgUtils().scaleImage(200,200,"c:/test.jpg"); //create label with image as background JLabel label=new JLabel(new ImageIcon((Image)img)); </code></pre> <p><strong>UPDATE:</strong></p> <p>Here is a small example I made:</p> <p><img src="https://i.stack.imgur.com/eMnEH.png" alt="enter image description here"></p> <pre><code>import java.awt.BorderLayout; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class JavaApplication117 { //change this to your own static String filename="c:/test.jpg"; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new JavaApplication117().createAndShowUI(); } }); } private void createAndShowUI() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponents(frame); frame.setResizable(false); frame.pack(); frame.setVisible(true); } private void initComponents(JFrame frame) { final BufferedImage img = new ImgUtils().scaleImage(200, 200, filename); //create label with image as background JLabel label = new JLabel(new ImageIcon((Image) img)); frame.getContentPane().add(label, BorderLayout.CENTER); } } class ImgUtils { public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) { BufferedImage bi = null; try { ImageIcon ii = new ImageIcon(filename);//path to image bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null); } catch (Exception e) { e.printStackTrace(); return null; } return bi; } } </code></pre> <p><strong>References:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/7252983/resizing-image-java-getscaledinstance">resizing image java getScaledInstance</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload