【问题标题】:How to show pictures in jFrame, using java2d?如何使用java2d在jFrame中显示图片?
【发布时间】:2013-07-01 17:23:10
【问题描述】:

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

提前谢谢...

【问题讨论】:

  • 但是在我脑筋急转弯之后......我不能在说你不能之前你至少应该尝试一下。
  • 我真的很努力,先生。我不是一个很好的搜索者,你可以看到......但我所有的尝试都是失败的。 (并原谅我的英语)

标签: java netbeans bitmap java-2d


【解决方案1】:

基本流程是使用Graphics#drawImage 渲染您之前加载的图像。

为了实现这一目标,您需要做很多事情......

  • 要绘制的图像
  • 要绘制的表面

幸运的是,这两个都在 Swing 中相对容易实现

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();
        }
    }
}

【讨论】:

  • 好吧,先生。有了这个我明白了!我只需要学习一点关于 Exceptions 和 Graphics2D 类的知识。顺便说一句,我谢谢你。
【解决方案2】:

如下所述,您需要扩展 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");
        }  
    }
}

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

【讨论】:

  • +1 用于扩展JPanel; +1 覆盖paintComponent; -1 用于使用setPreferredSize; -1 不调用super.paintComponent; -1 表示不向drawImage 方法提供ImageObserver; -1 不使用JFrame#pack; -1 表示缺少initial thread; -1 表示不打印异常(好吧,这是一个挑剔的)
  • 你疯了 :) 这是理解如何做的简洁的代码片段。它奏效了
【解决方案3】:

您可以使用 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);

这当然是一个基本的例子。

【讨论】:

  • -1,JFrame 没有paintComponent() 方法。自定义绘画是通过覆盖 JPanel(或 JComponent)的 paintComponent() 方法来完成的。然后将面板添加到框架中。您通常还会在方法的开头调用 super.paintComponent()。
  • 所以... 对我来说是新的。它会覆盖父方法吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-02
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多