Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Might be your <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html" rel="nofollow noreferrer"><code>LayoutManger</code></a>/lack there of thats interfering as <code>JLabel</code> should return correct size according to the content displayed.</p> <p>Please post <a href="http://sscce.org" rel="nofollow noreferrer">SSCCE</a> to show specific problems.</p> <p>Though regardless a large image will still need to be scaled, here is a nice method I usually use:</p> <pre><code>public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception { BufferedImage bi; bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(img, 0, 0, w, h, null); g2d.dispose(); return bi; } </code></pre> <p>here is an example:</p> <p><img src="https://i.stack.imgur.com/JiMkP.png" alt="enter image description here"></p> <pre><code>import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class Test { public Test() { initComponents(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } private void initComponents() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BufferedImage img = null; try { BufferedImage tmp = ImageIO.read(new URL("http://photos.appleinsider.com/12.08.30-Java.jpg")); img = scaleImage(200, 200, tmp); } catch (Exception ex) { ex.printStackTrace(); } JLabel label = new JLabel(new ImageIcon((Image) img)); frame.add(label); frame.pack(); frame.setVisible(true); } public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception { BufferedImage bi; bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(img, 0, 0, w, h, null); g2d.dispose(); return bi; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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