如何使用java2d在jFrame中显示图片?

时间:2013-07-01 17:23:10

标签: java netbeans bitmap java-2d

我是Java和Netbeans的新手。在许多其他语言中,这是一件简单的事情。但在打破了我的大脑思维之后,我做不到。我的疑问很容易解释。 如何在运行时,使用java2D在commom JFrame中显示位图(存储在硬盘驱动器上)?我需要编辑,或创建什么?这样做很简单吗?

提前致谢...

3 个答案:

答案 0 :(得分:1)

基本过程是使用Graphics#drawImage来渲染先前加载的图像。

为了达到这个目的,你需要做很多事情......

  • 要绘制的图像
  • 要在
  • 上绘画的表面

幸运的是,这些在Swing中相对容易实现

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ShowMyImage {

    public static void main(String[] args) {
        new ShowMyImage();
    }

    public ShowMyImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                ImagePane pane = new ImagePane();
                try {
                    pane.setImg(ImageIO.read(new File("C:\\hold\\thumbnails\\_MTCGAC__Pulling_Cords_by_Dispozition.png")));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(pane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage img;

        public ImagePane() {
        }

        public void setImg(BufferedImage value) {
            if (img != value) {
                img = value;
                repaint();
            }
        }

        public BufferedImage getImg() {
            return img;
        }

        @Override
        public Dimension getPreferredSize() {
            BufferedImage img = getImg();
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            BufferedImage img = getImg();
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight()- img.getHeight()) / 2;

                g2d.drawImage(img, x, y, this);
            }
            g2d.dispose();
        }
    }
}

答案 1 :(得分:0)

如下所述,您需要扩展JPanel类并覆盖paintComponent方法。

public class DrawImageClass extends JPanel {
    private static final long serialVersionUID = 1L;
    private Image image;
    public DrawImageClass(Image image) throws HeadlessException {
        super();
        this.image = image;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(image, null, null);
    }
}

然后创建JFrame并将此类添加到其中。

public class App {
    private static final int WIDTH=480, HEIGHT = 640;
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(new Dimension(HEIGHT, WIDTH));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            DrawImageClass panel = new DrawImageClass(ImageIO.read(new File(App.class.getResource("/1.png").getPath())));
            panel.setPreferredSize(new Dimension(HEIGHT, WIDTH));
            frame.add(panel);
            frame.setVisible(true);
        } catch (Exception e) {
            System.out.println("wrong path or smth other");
        }  
    }
}

项目资源文件夹中的图片示例

答案 2 :(得分:-1)

您可以使用java中的基本AWT包执行此操作。您将需要扩展JFrame类并覆盖其paintComponent方法以在那里显示图像。即。

public class MyFrame extends JFrame {
    private java.awt.Image bitmap = ImageIO.read(new File("myBitmap.bmp"));

    @Override
    public void paintComponent(java.awt.Graphics graphics) {
        int x = 0;
        int y = 0;
        int w = bitmap.getWidth(null);
        int h = bitmap.getHeight(null);
        graphics.drawImage(bitmap, x, y, w, h, this);
    }
}

您可以简单地将其实例化

MyFrame myFrame = new MyFrame();
myFrame.setPreferredSize(new Dimension(100, 100));
myFrame.setVisible(true);

这是一个基本的例子。