Java 为什么我的图像不显示在面板中?

Java 为什么我的图像不显示在面板中?,java,image,panel,frame,Java,Image,Panel,Frame,我想为我的游戏设置一个背景 场景: 首先,我必须从一个文本文件中读取,然后根据这些文本绘制我的瓷砖地图和图像。其次,我的地图是3600*2400像素,它比我的屏幕大,所以我必须滚动它。第三,在我的屏幕角落必须有一张迷你地图,告诉我我在哪里。(我想我应该使用面板和awt.container) 这是我的密码: public class bkg extends Panel implements ImageObserver { //i Initialize my variables here //

我想为我的游戏设置一个背景

场景: 首先,我必须从一个文本文件中读取,然后根据这些文本绘制我的瓷砖地图和图像。其次,我的地图是3600*2400像素,它比我的屏幕大,所以我必须滚动它。第三,在我的屏幕角落必须有一张迷你地图,告诉我我在哪里。(我想我应该使用面板和
awt.container

这是我的密码:

   public class bkg extends Panel implements ImageObserver {
//i Initialize my variables here
// then read my images with image buffer in constructor
public static void main(String[] args) {
    //my three panels and frame settings
    Frame frame = new Frame();
    frame.setLayout(null);
    frame.setSize(1350, 700);
    frame.setTitle("age of empires");
    frame.setVisible(true);
    frame.setLayout(null);
    Panel p1 = new Panel();
    Panel p2 = new Panel();
    p2.setLayout(null);
    JPanel p3 = new JPanel();
    p3.setLayout(null);
    p1.setSize(1350, 700); // i divide my screen in to 3 parts , one biggest panel, and two small. the biggest one is for main map
    p1.setLayout(null);
    p2.setBackground(Color.cyan);//just wanna test showing my panel.
    p2.setSize(675, 350);
    p3.setSize(675, 350);
    p1.setLocation(0, 0);
    p2.setLocation(0, 350);// this small panel must be under the main pannel.
    p3.setLocation(675, 350);// this is with p2.
    frame.add(p1);
    p1.add(p2);
    p2.add(p3);
    tilemap = new int[60][75];// it creat my tilemap in console.
    filereader();// it reads the text file.

}
@Override
public  void paint(Graphics g) {
    super.paint(g);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            int mod_i = 100 * i;// based on images sizes
            int mod_j = 50 * j;// based on images sizes
            //now i start to draw images base on the text file numbers
            switch (tilemap[i][j]) {
            case 1:
                g.drawImage(tree, mod_i, mod_j, null);
                break;
            .........

            }
        }
    }

}
公共类bkg扩展面板实现ImageObserver{
//我在这里初始化变量
//然后用构造器中的图像缓冲区读取我的图像
公共静态void main(字符串[]args){
//我的三个面板和框架设置
框架=新框架();
frame.setLayout(空);
框架设置尺寸(1350700);
框架。片名(“帝国时代”);
frame.setVisible(true);
frame.setLayout(空);
面板p1=新面板();
面板p2=新面板();
p2.设置布局(空);
JPanel p3=新的JPanel();
p3.设置布局(空);
p1.setSize(1350700);//我把屏幕分成三部分,一个最大的面板和两个小的面板。最大的一部分是主地图
p1.设置布局(空);
p2.setBackground(Color.cyan);//只想测试显示我的面板。
p2.设置尺寸(675350);
p3.设定尺寸(675350);
p1.设定位置(0,0);
p2.setLocation(0350);//这个小面板必须在主面板下面。
p3.setLocation(675350);//这是p2。
帧。添加(p1);
p1.添加(p2);
p2.添加(p3);
tilemap=newint[60][75];//它在控制台中创建我的tilemap。
filereader();//它读取文本文件。
}
@凌驾
公共空间涂料(图g){
超级油漆(g);
对于(int i=0;i

问题:我的代码似乎连画法都看不见。它没有在我的面板上绘制任何图像。有什么问题吗?

您的代码充满了错误,我从一开始就创建了一个分为3个区域的GUI

  • 必须通过调用SwingUtilities invokeLater方法启动Swing应用程序。这确保Swing应用程序在上启动

  • Java类名以大写字母开头,Java方法名和变量名以小写字母开头

  • 不要将所有内容都放在主方法中。将代码分解成能很好地完成一件事的方法。我的run方法创建JFrame。我的createMainPanel方法创建主面板

  • 我曾经定位过JPanels。是的,使用绝对定位似乎更容易,但你不能将你的应用程序移动到任何其他屏幕分辨率不同的计算机上

  • 我决定到此为止。您将创建更多的方法和/或类来进一步定义3个JPanel。在这些JPanel方法/类中,您将重写paintComponent方法,而不是paint方法

    祝你在游戏中好运

    这是密码

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class AgeOfEmpires implements Runnable {
    
        private JFrame frame;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new AgeOfEmpires());
        }
    
        @Override
        public void run() {
            frame = new JFrame();
            frame.setTitle("Age of Empires");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createMainPanel());
    
            frame.pack();
            frame.setVisible(true);
        }
    
        private JPanel createMainPanel() {
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
    
            JPanel upperPanel = new JPanel();
            upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    
            JPanel tilePanel = new JPanel();
            tilePanel.setBackground(Color.CYAN);
            tilePanel.setPreferredSize(new Dimension(800, 300));
    
            upperPanel.add(tilePanel);
    
            JPanel lowerPanel = new JPanel();
            lowerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    
            JPanel miniMapPanel = new JPanel();
            miniMapPanel.setBackground(Color.BLUE);
            miniMapPanel.setPreferredSize(new Dimension(400, 300));
    
            JPanel unknownPanel = new JPanel();
            unknownPanel.setBackground(Color.GREEN);
            unknownPanel.setPreferredSize(new Dimension(400, 300));
    
            lowerPanel.add(miniMapPanel);
            lowerPanel.add(unknownPanel);
    
            mainPanel.add(upperPanel, BorderLayout.CENTER);
            mainPanel.add(lowerPanel, BorderLayout.SOUTH);
    
            return mainPanel;
        }
    
    }
    

    如果paint负责绘制,那么为什么不在主方法中调用它呢?我这样做了,但它需要我一个参数,然后需要我将绘制更改为静态!然后给我静态绘制的错误:|在应用程序中,不需要调用绘制方法,当它执行时,它会自动搜索绘制方法ytnx,你会花时间让我明白这件事…我真的很感激:)我应该在这里写绘画组件吗?你能解释更多关于SwingUtilities的信息吗?是Swing的一个实用方法集合。invokeLater方法是最常用的方法。通常,当你扩展JPanel以便在JPanel上绘画时,你会覆盖paintComponent方法。看一看好的,看我的文章,看看我是如何在JPanel上绘制的。