Note that there are some explanatory texts on larger screens.

plurals
  1. POResizing Images for full-screen applications?
    primarykey
    data
    text
    <p>I've got a full-screen JFrame and I'd like to create an background for it by filling a JLabel with an image and matching it's size to the dimension of the screen.</p> <p>Whilst I can create a JLabel that matches the screen's size I can't seem to resize my image to fit. In essence, I want to end up with a full-screen JFrame that is completely filled with an image (regardless of the image's or the screen's dimensions).</p> <p>Current code is below, thanks!</p> <pre><code>public void addBackground(ImageIcon imgIcon) { Image img = imgIcon.getImage(); BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); BufferedImage resizeBuffImg = resize(buffImg, screenSize.width, screenSize.height); Image finalImg = Toolkit.getDefaultToolkit().createImage(resizeBuffImg.getSource()); ImageIcon back = new ImageIcon(finalImg); System.out.println("IMAGE WIDTH: " + back.getIconWidth()); System.out.println("IMAGE HEIGHT: " + back.getIconHeight()); JLabel backgroundLabel = new JLabel(back); getContentPane().add(backgroundLabel); } public BufferedImage resize(BufferedImage image, int width, int height) { int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType(); BufferedImage resizedImage = new BufferedImage(width, height, type); Graphics2D g = resizedImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(image, 0, 0, width, height, null); g.dispose(); return resizedImage; } </code></pre> <p>EDIT - Got it! Using Image's getScaledInstance() works. It's not perfect, but I'm intending to scale down large images rather than expand smaller ones.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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