【问题标题】:Why do my images not display in my panels?为什么我的图像不显示在我的面板中?
【发布时间】:2015-06-12 12:12:51
【问题描述】:

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

场景: 首先,我必须从文本文件中读取,然后根据该文本在其上绘制我的瓦片地图和图像。其次,我的地图是 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方法都看不到。它不会在我的面板上绘制任何图像。有什么问题?

【问题讨论】:

  • 如果paint负责绘制,那为什么不在main方法中调用呢?
  • 我做到了,但它要我争论,然后要我把油漆变成静态的!然后给我关于静态油漆的错误:|在应用中不需要调用paint方法,执行时会自动搜索paint方法

标签: java image panel frame


【解决方案1】:

你的代码错误太多,我从头开始创建一个分为 3 个区域的 GUI。

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

  2. Java 类名以大写字母开头。 Java 方法名和变量名以小写字母开头。

  3. 不要把所有东西都放在你的 main 方法中。把你的代码分解成能做好一件事的方法。我的 run 方法创建了 JFrame。我的 createMainPanel 方法创建了主面板。

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

我决定在这一点上停下来。您将创建更多方法和/或类来进一步定义 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;
    }

}

【讨论】:

  • tnx,你花时间向我解释这些事情......我真的很感激:)
  • 我应该在这里写油漆组件吗?你能解释更多关于 SwingUtilities 的信息吗?
  • SwingUtilities 是 Swing 实用方法的集合。 invokeLater 方法是最常用的方法。通常,当您扩展 JPanel 以便在 JPanel 上绘图时,您会覆盖 paintComponent 方法。看看我的文章Retro Snake Game,看看我是如何在 JPanel 上绘图的。
猜你喜欢
  • 2015-05-04
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 2019-12-10
  • 2022-08-13
相关资源
最近更新 更多