如何显示JFrame的图片,使用的Java2D? [英] How to show pictures in jFrame, using java2d?

查看:242
本文介绍了如何显示JFrame的图片,使用的Java2D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java和Netbeans工作新。在许多其他语言中,这是一个简单的东西做的。但打破了我的大脑思考后,我不能。我的疑问是简单的解释。
我怎么能显示位图(存储在硬盘驱动器)运行时,在黎民JFrame中,使用的Java2D?我需要编辑,并可以创建什么?难道是简单的事?

I'm new on working with Java and Netbeans. In many others languages, it's a simple stuff to do. But after broke my brain thinking, I couldn't. My doubt is simple to explain. How can I show bitmaps (stored on Hard Drive) in runtime, in a commom JFrame, using java2D? What I need to edit, and or create? Is it simple to do?

在此先感谢...

推荐答案

的基本过程是使用图形#的drawImage 来使你有previously加载图像。

The basic process is use Graphics#drawImage to render an image you have previously loaded.

有许多事情你为了实现此需要...

There are a number of things you need in order to achieve this...


  • 的图像画

  • A面画上

幸运的是,这两个都比较容易在Swing实现

Luckily, both these are relatively easy to achieve in Swing

  • See ImageIO for reading images, Reading/Loading an Image
  • See Performing Custom Painting for more details about how to perform painting in 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();
        }
    }
}

这篇关于如何显示JFrame的图片,使用的Java2D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆