1

所以我正在尝试用java制作一个游戏,它将显示一个图像网格作为背景,现在它只是草,偶尔有石头作为我的图像,当它工作时,我得到一个32的大网格显示32 张图片符合预期,但大约三分之二的时间,我的面板中没有出现任何内容!

为了解决问题,我已经消除了很多功能,当我运行代码时,即使没有每秒重新绘制几次,它仍然只能在某些时候工作。

主班

import javax.swing.JFrame;


public class Main {



    public static void main(String[] args) {
        //Creating a new frame for the whole game and everything
        JFrame frame = new JFrame("Lawlz this is my game");
        frame.setSize(800,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);

        //adding the game panel to the main frame
        frame.add(new GameFrame());
    }

}

游戏框架类(制作面板的地方) 这是几乎所有内容都被注释掉的代码。即使是动画和播放器类,但它仍然不会显示任何东西大约一半的时间。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GameFrame extends JPanel {//implements ActionListener {

    Timer mainTimer;
    Player player;
    Random rand = new Random();
    Grid GameGrid = new Grid(25, 25);

    int enemyCount = 5;

    //Constructer!
    public GameFrame()
    {
        setFocusable(true);
        //player = new Player(100,100);
    }

    //Does all of the drawing for the game
    public void paint(Graphics g)
    {
        //Sends to the JPanel class (super class)
        super.paint(g);

        //gets a graphics 2d object
        Graphics2D g2d = (Graphics2D)g;

        GameGrid.draw(g2d);
    }
}

GameGrid 类,它基本上包含许多“网格空间”对象,每个对象基本上只是一个图像。在这一点上,它还负责打印出使用 draw() 方法绘制的大部分内容

import java.awt.Graphics2D;
import java.util.ArrayList;


public class Grid {

    ArrayList<GridSpace> grid = new ArrayList<GridSpace>();
    int width;
    int height;

    public Grid(int w, int h)
    {
        width = w;
        height = h;

        //Temporary for creating the x and y values by using the size of the images
        int gw = new GridSpace(0,0).getGridImg().getWidth(null);
        int gh = new GridSpace(0,0).getGridImg().getHeight(null);

        for(int i = 0; i < h; i++)
        {
            for(int n = 0; n < w; n++)
            {
                grid.add(new GridSpace(n*gw,i*gh));   //GridSpace(n*ImageWidth, i*ImageHeight)
            }
        }
    }

    public void draw(Graphics2D g2d)
    {
        for(int i = 0; i < width; i++)
        {
            for(int n = 0; n < height; n++)
            {
                GridSpace tempSpace = GetGridSpaceByID(GetCoordinates(n,i)); //gets the GridSpace object for this corrdinate
                g2d.drawImage(tempSpace.getGridImg(),tempSpace.x,tempSpace.y,null);
            }
        }
    }

    public void test(Graphics2D g2d)
    {
        GridSpace tempSpace = GetGridSpaceByID(GetCoordinates(0,0));
        g2d.drawImage(tempSpace.getGridImg(),tempSpace.x,tempSpace.y,null);
    }

    public GridSpace GetGridSpaceByID(int n)
    {
        return grid.get(n);
    }

    public int GetCoordinates(int x, int y)
    {
        return x + y * width;
    }


}

最后,基本上只存储图像的 GridSpace 对象

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.util.Random;

import javax.swing.ImageIcon;


public class GridSpace {
    Random r = new Random();
    ImageIcon ic;
    int x = 0;
    int y = 0;

    public GridSpace(int x,int y)
    {
        this.x = x;
        this.y = y;

        //Stores an image for this grid space
        //ic = new ImageIcon("C:\\Users\\Ben\\Desktop\\pic1.png");
        if(r.nextInt(10) < 9)
        {
            ic = new ImageIcon("C:\\Users\\Ben\\Desktop\\Video Game\\grass1.jpg");
        }
        else
        {
            ic = new ImageIcon("C:\\Users\\Ben\\Desktop\\Video Game\\grass2.jpg");
        }
    }

    public Image getGridImg()
    {
        //Image image = ic.getImage();
        return ic.getImage();
    }
}
4

3 回答 3

2

由于您当前正在添加GameFrame调用后添加面板

frame.setVisible(true);

在执行调整大小等事件之前,它不会被绘制。您应该将此调用setVisible作为方法中的最后一个main方法。也使用paintComponent而不是paint利用 Swing 的优化绘制模型。

必修链接:在 AWT 和 Swing 中绘画

于 2012-12-16T17:59:43.320 回答
2

如果您在主线程中调用 Swing 相关代码,也会出现其中一些问题。尝试在 EDT(事件调度线程)中执行此操作。

为此,请将您的Main课程更改为:

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                //Creating a new frame for the whole game and everything
                JFrame frame = new JFrame("Lawlz this is my game");
                frame.setSize(800,800);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setResizable(false);

                //adding the game panel to the main frame
                frame.add(new GameFrame());
            }
        });
    }
}

有关更多详细信息,请参见此处

于 2012-12-16T18:05:17.553 回答
2
  1. GameFrame(可能更准确地称为GamePanel)应该覆盖getPreferredSize()以返回适合游戏区域的维度。框架本身有自己的装饰,并且会更大。
  2. main 中的调用顺序是错误的。呼叫setVisible(true)作为最后一部分。
  3. 另请注意在 EDT 上启动 GUI 的建议。

这是我当前的“框架演示”模板中的一个主要内容,它显示了最后两点并提供了一些其他好的提示。

public static void main(String[] args) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            // the GUI as seen by the user (without frame)
            JPanel gui = new JPanel(new BorderLayout());
            gui.setBorder(new EmptyBorder(2,3,2,3));

            // TODO!
            gui.setPreferredSize(new Dimension(400,100));
            gui.setBackground(Color.WHITE);

            JFrame f = new JFrame("Demo");
            f.add(gui);
            // Ensures JVM closes after frame(s) closed and
            // all non-daemon threads are finished
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            // See http://stackoverflow.com/a/7143398/418556 for demo.
            f.setLocationByPlatform(true);

            // ensures the frame is the minimum size it needs to be
            // in order display the components within it
            f.pack();
            // should be done last, to avoid flickering, moving,
            // resizing artifacts.
            f.setVisible(true);
        }
    };
    // Swing GUIs should be created and updated on the EDT
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    SwingUtilities.invokeLater(r);
}
于 2012-12-17T01:35:36.853 回答