Image is not being confined to a direct area

Tim Tebow :

Image is not being confined to a direct area in this java code below. I want the java image to be displayed in its entirety in a 400 width by 400 height image. I tried to do that by frame.setSize(400, 400); and it is not working. My code includes drawImage which is what I was told I had to put in the code for this to work. I don't what to do next.

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();

        final BufferedImage image = ImageIO.read(new File("/Users/johnzalubski/Desktop/c.jpg"));

        JPanel pane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, null);
            }
        };


        frame.add(pane);
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
        return frame;
    }


}
Gilbert Le Blanc :

I reworked your Swing sandbox code a bit. Here's what I came up with.

Image Display

The image is distorted because I made it fit a 400 x 400 drawing panel. You should reduce the image and maintain the aspect ratio.

Here are the changes I made.

  1. I added the call to the SwingUtilities invokeLater method to put the Swing components on the Event Dispatch Thread.

  2. I made the drawing panel a class, so I could set the preferred size. You set the size of the drawing panel, not the JFrame. Who cares how large or small the JFrame is?

  3. I put the JFrame method calls in the correct order.

And here's the code.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class SwingSandbox implements Runnable {

    public static void main(String[] args) throws IOException {
        SwingUtilities.invokeLater(new SwingSandbox());
    }

    private BufferedImage image;

    public SwingSandbox() {
        try {
            image = ImageIO.read(new File("C:\\Users\\Owner\\OneDrive\\Pictures\\Saved Pictures\\StockMarketGame.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Image Display");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        DrawingPanel panel = new DrawingPanel(image);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private BufferedImage image;

        public DrawingPanel(BufferedImage image) {
            this.image = image;
            this.setPreferredSize(new Dimension(400, 400));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 400, 400, null);
        }

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344790&siteId=1