JFrame中的BufferedImage不显示

Jon*_*eel 1 java swing bufferedimage jframe drawimage

试图将图像打印到窗口中.一切都运行没有错误,如果我用另一个图形类替换drawImage也可以.但是,窗口缺少图像,我不知道为什么.同样,JFrame的东西和图形可以很好地绘制其他图形,但只是不在这里绘制图像.谢谢.

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class GraphicsMovement2 extends JApplet{
    BufferedImage image = null;

    public static void main(String args[]){
        BufferedImage image = null;
        try {
            File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
            ImageInputStream imgInpt = new FileImageInputStream(file);
            image = ImageIO.read(file);
        }
        catch(FileNotFoundException e) {
            System.out.println("x");
        }
        catch(IOException e) {
            System.out.println("y");
        }


        JApplet example = new GraphicsMovement2();
        JFrame frame = new JFrame("Movement");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(example);
        frame.setSize(new Dimension(1366,768));       //Sets the dimensions of panel to appear when run
        frame.setVisible(true);
    }
    public void paint (Graphics page){
    page.drawImage(image, 100, 100, 100, 100, Color.RED, this);
  }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 7

你已经定义了image两次......

BufferedImage image = null;

public static void main(String args[]){
    BufferedImage image = null;
Run Code Online (Sandbox Code Playgroud)

这实际上意味着当你到达paint方法时,它就是null你没有初始化实例变量.

您将遇到的另一个问题是您尝试从静态引用加载图像image但未声明为static.最好将此逻辑移动到构造函数或实例方法中.

JApplet当你添加a时JFrame,不要用作你的容器,你最好使用类似的东西JPanel.在将容器添加到容器中时,它将有所帮助.

你必须打电话 super.paint(g) ...事实上,不要覆盖paint顶级容器的方法,如JFrameJApplet.使用类似的东西JPanel,改写paintComponent方法.顶级容器不是双缓冲的.

这些paint方法做了很多重要的工作,而且它更容易使用JComponent#paintComponent...但不要忘记打电话super.paintComponent

更新

您需要image在上下文中定义它将被使用.

因为您将其声明image为实例字段GraphicsMovement2,所以您需要一个实例GraphicsMovement2才能引用它.

但是,在你的main方法中static,你也声明了一个名为的变量image.

paint方法GraphicsMovement2无法查看您声明的变量main,只能看到实例字段(即null).

为了解决这个问题,你需要将图像的加载移动到实例的上下文中GraphicsMovement2,这可能是最好的(在你的上下文中),但是将图像加载到构造函数中GraphicsMovement2

public GraphicsMovement2() {
    try {
        File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
        ImageInputStream imgInpt = new FileImageInputStream(file);
        image = ImageIO.read(file);
    }
    catch(FileNotFoundException e) {
        System.out.println("x");
    }
    catch(IOException e) {
        System.out.println("y");
    }
}
Run Code Online (Sandbox Code Playgroud)

以下两个例子将产生相同的结果......

在此输入图像描述

简单的方法

public class TestPaintImage {

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

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

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

    public class ImagePane extends JPanel {

        public ImagePane() {
            setLayout(new BorderLayout());
            ImageIcon icon = null;
            try {
                icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            add(new JLabel(icon));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

困难的方式

public class TestPaintImage {

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

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

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

    public class ImagePane extends JPanel {

        private BufferedImage background;

        public ImagePane() {
            try {
                background = ImageIO.read(new File("/path/to/your/image"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

花点时间阅读教程