为什么我的图片没有显示在我的面板中?

时间:2015-06-12 12:12:52

标签: java image panel frame

我想为我的游戏设置背景。

情境: 首先,我必须从文本文件中读取,然后根据该文本在其上绘制我的瓷砖地图和图像。其次,我的地图是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;
            .........

            }
        }
    }

}

问题:似乎我的代码甚至看不到paint方法。它不会在我的面板上绘制任何图像。有什么问题?

1 个答案:

答案 0 :(得分:0)

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

Age of Empires GUI

  1. 您必须通过调用SwingUtilities invokeLater方法启动Swing应用程序。这可确保Swing应用程序在Event Dispatch thread (EDT)

  2. 上启动
  3. Java类名称以大写字母开头。 Java方法名称和变量名称以小写字母开头。

  4. 不要将所有内容都放在main方法中。将代码分解为完成一件事的方法。我的run方法创建了JFrame。我的createMainPanel方法创建主面板。

  5. 我使用Swing layout managers来定位JPanel。是的,使用绝对定位似乎更容易,但您无法将应用程序移动到具有不同屏幕分辨率的任何其他计算机。

  6. 我决定在这一刻停下来。您将创建更多方法和/或类以进一步定义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;
        }
    
    }
    
相关问题