【问题标题】:How do I display an image on a frame using paintComponent如何使用paintComponent在框架上显示图像
【发布时间】:2013-08-08 13:40:23
【问题描述】:

这是我的代码:

package com.dani.Game;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Game extends JFrame {

BufferedImage normal;

public static void main(String[] args) {
    Game game = new Game();
}

public Game() {

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    try {
    normal = ImageIO.read(new File("C:\\ImageOne.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    ImagePanel graphics = new ImagePanel();

    this.add(graphics);
    this.setVisible(true);

}

public class ImagePanel extends JPanel {

   public void paintComponent(Graphics g) {

    super.paint(g);
    g.drawImage(normal,normal.getWidth(), normal.getHeight(), this);

  }
}       

更新:

当我尝试这个时,我得到了这个异常:

Exception in thread 
     "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.AppContext.get(Unknown Source)
    at javax.swing.RepaintManager.currentManager(Unknown Source)
    at javax.swing.RepaintManager.currentManager(Unknown Source)
    at javax.swing.RepaintManager.currentManager(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at com.course.swing.headPrac$Drawing.paintComponent(headPrac.java:99)
    at javax.swing.JComponent.paint(Unknown Source)

我做错了什么?

【问题讨论】:

  • ye 还有什么样的异常?你甚至没有提供异常类。

标签: exception graphics jframe jpanel paintcomponent


【解决方案1】:

无论如何,由于paint和paintComponent之间的调用循环,这将导致StackOverflow。 将super.paint(g) 替换为super.paintComponent(g) 作为开始。

无论如何,它的工作形式是我

包com.dani.Game; 导入 java.awt.BorderLayout; 导入 java.awt.Graphics; 导入 java.awt.image.BufferedImage; 导入java.io.File; 导入 java.io.IOException; 导入 javax.imageio.ImageIO; 导入 javax.swing.JFrame; 导入 javax.swing.JPanel; 公共类游戏扩展 JFrame { BufferedImage 正常; 公共静态无效主要(字符串[]参数){ 游戏游戏 = 新游戏(); } 公共游戏(){ this.setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 尝试 { 正常 = ImageIO.read(new File("d:\\providers.gif")); } 捕捉(IOException e){ e.printStackTrace(); } ImagePanel 图形 = 新 ImagePanel(); this.add(graphics, BorderLayout.CENTER); this.setVisible(true); } 公共类 ImagePanel 扩展 JPanel { 公共无效paintComponent(图形g){ super.paintComponent(g); g.drawImage(normal, 0, 0, normal.getWidth(), normal.getHeight(), this); } } }

【讨论】:

  • Antoniossss ,当我替换它并运行它时,它不会给出异常,但是当它打开框架时,它是空的
  • 所以我是对的例外。现在尝试将框架的 layoutmanager 更改为 BorderLayout 并使用 BorderLayout.CENTER 约束将组件添加到其中。
【解决方案2】:

我对您的代码进行了一些更改以使其正常工作。

  • 我在 main 方法中调用了 SwingUtilities 的 invokeLater 方法,以确保 Swing GUI 在事件调度线程 (EDT) 上启动。 Swing GUI 的组件必须在 EDT 上定义和使用。

  • 我使用 JFrame 而不是扩展 JFrame。只有当您想要修改 Swing 组件的方法之一时,您才可以扩展它。否则,您使用 Swing 组件。

  • 我在图像文件名中使用了正斜杠。对于 Windows,Java 会将正斜杠转换为反斜杠。

  • 我添加了对 JFrame pack 方法的调用,以便 JFrame 将自身调整为图像大小。

  • 我在 ImagePanel 构造函数中添加了对 setPreferredSize 方法的调用,以便 JPanel 成为图像的大小。

  • 我将您在 paintComponent 方法中的 super 调用更正为 super.paintComponent(g)。

  • 我更正了您的 drawImage 方法以使用图像的原点,而不是大小。

这是代码。

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

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

public class Game implements Runnable {

    BufferedImage   normal;

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

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            normal = ImageIO.read(new File("C:/ImageOne.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        ImagePanel graphics = new ImagePanel();

        frame.add(graphics);
        frame.pack();
        frame.setVisible(true);
    }

    public class ImagePanel extends JPanel {

        public ImagePanel() {
            setPreferredSize(new Dimension(normal.getWidth(),
                    normal.getHeight()));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(normal, 0, 0, this);
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多