用Java实现一个简单的游戏(图形相关)

发布于 2024-11-14 10:27:44 字数 310 浏览 2 评论 0原文

我对 Java 游戏编程很陌生,尤其是在图形方面,因此我想寻求一些关于如何在 Java 中以图形方式实现以下游戏的建议。

游戏非常简单,它显示一个正方形,该正方形被进一步分成2x2的盒子,游戏的玩法是将总共44个筹码放入这4个盒子中,并且用户应该能够从其中拖放筹码盒子到另一个。

就是这样!我的问题:

  1. 是否有现成的库可以用来绘制由 4 个盒子和芯片组成的正方形?
  2. 如果 1) 的答案是否定的,那么我可以遵循任何教程来自己对它们进行编程吗?
  3. 如何以图形方式实现拖放部分?

非常感谢。

I am new to game programming in Java, especially on the graphics front, hence I would like to seek some advice on how to implement the following game graphically in Java.

The game is very simple, it displays a square which is further divided into a 2x2 boxes, and the game playing is to put a total of 44 chips into these 4 boxes, and the user should be able to drag and drop the chips from one box to another.

That's it! My questions:

  1. is there ready-made library I can use for drawing the square consists of the 4 boxes as well as the chips?
  2. if the answer to 1) is no, then is there any tutorial I can follow to program them myself?
  3. How to implement the drag and drop part graphically?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-11-21 10:27:44

芯片可以通过添加到 JLabel 的图标来表示。

正方形可以用 JPanel 来表示。

首先阅读 Swing 教程中有关如何使用图标。还有其他感兴趣的部分:如何使用面板、使用布局管理器、如何编写 MouseListener、可能是有关拖放的部分。

Chips can be represented by an Icon added to a JLabel.

Squares can be represented by a JPanel.

Start by reading the section from the Swing tutorial on How to Use Icons. There are other section of interest as well: How to Use Panels, Using Layout Managers, How to Write a MouseListener, The section on drag and drop maybe.

谈情不如逗狗 2024-11-21 10:27:44

我将使用画布并覆盖油漆(图形g)并使用它绘制各种元素。然后您可以使用计时器或游戏循环调用 repaint()。

public class MyCanvas extends Canvas
{
    public void gameLoop()
    {
        //Don't do it this way, this is just a quick example.
        //Instead look up better game loop options.
        while (true)
        {
            repaint(); //automatically calls paintComponent
            Thread.yield();
        }
    }

    //Put all the stuff that gets drawn in here.
    //@Override
    public void paint(Graphics g)
    {
        super.paint(g);

        for (int i = 0; i < chips.size(); i++)
        {
            chips.get(i).draw(g);
        }
    }
}

public class Chip
{
    private int x;
    private int y;

    public void draw(Graphics g)
    {
        g.setColor(Color.BROWN);
        g.fillRect(x, y, 50, 50);
        //etc.
    }
}

I would use a Canvas and override paint(Graphics g) and draw your various elements using that. Then you can call repaint() with a timer or a game loop.

public class MyCanvas extends Canvas
{
    public void gameLoop()
    {
        //Don't do it this way, this is just a quick example.
        //Instead look up better game loop options.
        while (true)
        {
            repaint(); //automatically calls paintComponent
            Thread.yield();
        }
    }

    //Put all the stuff that gets drawn in here.
    //@Override
    public void paint(Graphics g)
    {
        super.paint(g);

        for (int i = 0; i < chips.size(); i++)
        {
            chips.get(i).draw(g);
        }
    }
}

public class Chip
{
    private int x;
    private int y;

    public void draw(Graphics g)
    {
        g.setColor(Color.BROWN);
        g.fillRect(x, y, 50, 50);
        //etc.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文