这是我的代码:
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)
我究竟做错了什么?