如何导入图像

时间:2017-03-10 12:05:02

标签: java image

亲爱的stackoverflow社区,

如何导入图像? 我试过这段代码:

主要课程:

private DisplayImage panel;

public ImportImage() throws IOException, InterruptedException {   
    this.setSize(new Dimension(1500, 1500));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    panel = new DisplayImage();

    this.add(panel);

    this.setVisible(true);
}

public static void main(String[] args) throws IOException, InterruptedException {
    ImportImage iI = new ImportImage();
}

和小组类:

BufferedImage img;

public DisplayImage() throws IOException, InterruptedException {
    String imgPath = "imageimport.png";
    img = ImageIO.read(getClass().getResourceAsStream(imgPath));
    sleep(300);
    setVisible(true);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawImage(img, 0, 0, null);
}

当我启动程序时,它什么都不输出,甚至没有输出错误。 它运行无穷无尽,直到我自己停止程序才停止。 Image与main和panel类位于同一文件夹中。 对此代码或更好的建议的更正无关紧要。 抱歉英语不好,这不是我的母语,但我还在学习/学习它。

3 个答案:

答案 0 :(得分:0)

ACHTUNG这个答案错了​​! Looking at the API I see my error

您已将g.drawImage中的宽度和高度设置为0.将它们设置为图像的大小。

您可以从img.getWidth()img.getHeight()

获得宽度和高度

顺便说一下。让线程睡觉有什么意义?

答案 1 :(得分:0)

假设你的代码如下,它对我有用

package com.importimage;

import java.awt.Dimension;
import java.io.IOException;

import javax.swing.JFrame;

/**
 * @author elias.shaik
 *
 */
public class ImportImage extends JFrame{
    private DisplayImage panel;

    public ImportImage() throws IOException, InterruptedException {   
        this.setSize(new Dimension(1500, 1500));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        panel = new DisplayImage();

        this.add(panel);

        this.setVisible(true);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        ImportImage iI = new ImportImage();
    }
}

package com.importimage;

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

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * @author elias.shaik
 *
 */
public class DisplayImage extends JPanel{
    BufferedImage img;

    public DisplayImage() throws IOException, InterruptedException {
        String imgPath = "mavenSrpingStructure.PNG";
        img = ImageIO.read(getClass().getResourceAsStream(imgPath));
        Thread.sleep(300);
        setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(img, 0, 0, null);
    }
}

答案 2 :(得分:0)

您可以在图片加载后设置面板的首选尺寸。

setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

应用

import java.awt.event.*;
import javax.swing.*;

public class App implements Runnable {
    @Override
    public void run() {
        JFrame f = new JFrame("Image Frame");
        ImagePanel p = new ImagePanel("resources/Batman.jpg");

        p.addComponentListener(new ComponentListener() {
            @Override public void componentShown(ComponentEvent e) { }
            @Override public void componentMoved(ComponentEvent e) { }
            @Override public void componentHidden(ComponentEvent e) { }

            @Override
            public void componentResized(ComponentEvent e) {
                if (e.getComponent() instanceof ImagePanel) {
                    f.setTitle(((ImagePanel) e.getComponent()).getFilename());
                }
            }
        });

        f.setContentPane(p);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

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

ImagePanel

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private BufferedImage image;
    private String filename;

    public String getFilename() {
        return filename;
    }

    public ImagePanel(String path) {
        super();
        try {
            updateImage(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updateImage(String path) throws IOException {
        image = ImageIO.read(getClass().getResourceAsStream(path));

        if (image != null) {
            setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

            Path p = Paths.get(path);
            filename = p.getFileName().toString();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            g.clearRect(0, 0, getWidth(), getHeight());
            g.drawImage(image, 0, 0, null);
        }
    }
}