制作变量,将图像加载到变量中,并要求paintComponent在我的JPanel中显示图像。我看不出自己的错误。
public class Main extends JFrame {
public static void main(String[] args) {
JFrame frame = new Main();
frame.setSize(1524, 715);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Animatieproject Stijn Mannaerts: Free Kick");
frame.setContentPane(new Voetbalveld());
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
class Voetbalveld extends JPanel {
private ImageIcon afbIntro;
public Voetbalveld() {
afbIntro = new ImageIcon("scr/afbeeldingen/Intro.jpg");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
afbIntro.paintIcon(null, g, 0, 0);
}
}
答案 0 :(得分:0)
这不是绘画的方式。为什么不使用Graphics.drawImage()呢?查看本教程:http://docs.oracle.com/javase/tutorial/uiswing/painting/
答案 1 :(得分:0)
您的代码似乎对我而言。确保您不希望src
代替scr
。接下来,如果您的图像文件夹位于项目的源文件夹中,那么您可能想尝试这样做:
afbIntro = new ImageIcon(Voetbalveld.class.getResourceAsStream("scr/afbeeldingen/Intro.jpg"));
或
afbIntro = new ImageIcon(Voetbalveld.class.getResourceAsStream("afbeeldingen/Intro.jpg"));
取决于您的文件结构。第二个示例中的路径需要相对于文件Main.java
。
答案 2 :(得分:0)
框架类
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
Jframe frame = new JFrame("Animatieproject Stijn Mannaerts: Free Kick");
frame.setSize(1524, 715);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Voetbalveld());
frame.setVisible(true);
}
}
小组类
import javax.swing.*;
import java.awt.*;
public class Voetbalveld extends JPanel
{
public void paintComponent(Graphics g)
{
ImageIcon afbIntro = new ImageIcon("scr/afbeeldingen/Intro.jpg");
/*The following are two methods for image sizing,
*Use the one that best fits your code:
*
*g.drawImage(afbIntro.getImage(), x, y, null);
*Fill in the arguments for x and y to locate your upper left corner
*The image will be in it's original size with the designated upper left corner
*
*g.drawImage(afbIntro.getImage(), x, y, w, h, null);
*Fill in the arguments for w and h to set the width and height of your image
*The image will be in it's scaled size (w and h) and starting at the
*designated upper left corner (x and y)
*/
}
}
答案 3 :(得分:0)
你拼写src
错了。你有scr
。
"scr/afbeeldingen/Intro.jpg"
修复