输出未正确显示在GUI上

发布于 2025-02-04 12:17:21 字数 1995 浏览 0 评论 0原文

我正在尝试连续显示4个瓷砖。但是,似乎只有最后一个瓷砖显示在GUI上,我不确定为什么。我已经在互联网上查看,我找不到我能理解的任何解决方案。 这是我的代码:

import java.awt.*;
import javax.swing.*;

// creates a GUI and displays the game tiles made in gameTile.java
public class testGameTileClass { 
    gameTile tile1;
    gameTile tile2;
    gameTile tile3;
    gameTile tile4;

    public testGameTileClass() {
        // sets up GUI
        JFrame frame = new JFrame("Game Tiles");
        JPanel panel = new JPanel();
        frame.setSize(1280, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);   
        testConstructor1(frame);
        frame.setVisible(true);
    }    

    public void testConstructor1(JFrame frame) {
        tile1 = new gameTile(10, 10, 64, Color.RED.darker());
        frame.add(tile1);
        tile2 = new gameTile(84, 10, 64, Color.GREEN);
        frame.add(tile2);
        tile3 = new gameTile(148, 10, 64, Color.BLUE);
        frame.add(tile3);
        tile4 = new gameTile(212, 10, 64, Color.ORANGE);
        frame.add(tile4);
    }

    public static void main(String[] args) {
        new testGameTileClass();
    }
}

这是配子类代码:

    import javax.swing.*;
import java.awt.*;

public class gameTile extends JPanel {
    private Color color;        //color of gameTile
    private int x, y, size;     // x and y coords, and size of gameTile
    public gameTile (int x_, int y_, int size_, Color color_) {
        x = x_;
        y = y_;
        size = size_;
        color = color_;        
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2));
        g.setColor(color);
        g.fillRect(x, y, size, size);
        g2.setColor(Color.BLACK);
        g2.drawRect(x, y, size, size);
    }
}

这是我的结果。有什么方法可以解决此问题吗?

I'm trying to display 4 tiles in a row. However, only the last tile seems to be displayed on the GUI and I'm not sure why. I've looked around the internet and I cant find any solution I can comprehend.
here is my code:

import java.awt.*;
import javax.swing.*;

// creates a GUI and displays the game tiles made in gameTile.java
public class testGameTileClass { 
    gameTile tile1;
    gameTile tile2;
    gameTile tile3;
    gameTile tile4;

    public testGameTileClass() {
        // sets up GUI
        JFrame frame = new JFrame("Game Tiles");
        JPanel panel = new JPanel();
        frame.setSize(1280, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);   
        testConstructor1(frame);
        frame.setVisible(true);
    }    

    public void testConstructor1(JFrame frame) {
        tile1 = new gameTile(10, 10, 64, Color.RED.darker());
        frame.add(tile1);
        tile2 = new gameTile(84, 10, 64, Color.GREEN);
        frame.add(tile2);
        tile3 = new gameTile(148, 10, 64, Color.BLUE);
        frame.add(tile3);
        tile4 = new gameTile(212, 10, 64, Color.ORANGE);
        frame.add(tile4);
    }

    public static void main(String[] args) {
        new testGameTileClass();
    }
}

and here is the gameTile class code:

    import javax.swing.*;
import java.awt.*;

public class gameTile extends JPanel {
    private Color color;        //color of gameTile
    private int x, y, size;     // x and y coords, and size of gameTile
    public gameTile (int x_, int y_, int size_, Color color_) {
        x = x_;
        y = y_;
        size = size_;
        color = color_;        
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2));
        g.setColor(color);
        g.fillRect(x, y, size, size);
        g2.setColor(Color.BLACK);
        g2.drawRect(x, y, size, size);
    }
}

This is my result. Is there any way to fix this?

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

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

发布评论

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

评论(1

鸢与 2025-02-11 12:17:21

Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。请密切注意执行自定义绘画

我继续创建您要创建的GUI。

创建秋千应用程序时,我使用模型 - 视图对照器(MVC)模式。这种模式使我可以分开我的担忧,并专注于一次应用程序的一小部分。

我使用了您的游戏瓷砖想法,并创建了一个普通的Java Getter/Setter类来容纳瓷砖。在这个简单的示例中,我所需要的只是颜色和矩形信息。在一个更复杂的示例中,您可以创建一个BufferedImage,而不是一种颜色。

我在单独的模型类中收集了四个配子实例。模型类将在另一个单独的普通Java Getter/Setter类中保存该应用程序的所有信息。

创建模型后,编写视图很简单。您有一个图纸jpanel在其中绘制所有游戏图块。

在这个简单的示例中,没有控制器。控制器由一个或多个ActionActionListener类组成。

这是完整的可运行代码。我完成了所有其他类内的类,以便我可以将代码作为一个块发布。您应该将类​​分为包中的不同文件。对于我的大多数摇摆项目,我创建单独的模型,视图和控制器软件包。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGameTileExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestGameTileExample());
    }

    private final GameModel model;

    public TestGameTileExample() {
        this.model = new GameModel();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Game Tiles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        frame.add(new DrawingPanel(model), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private final GameModel model;

        public DrawingPanel(GameModel model) {
            this.model = model;
            this.setPreferredSize(new Dimension(1200, 720));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (GameTile tile : model.getTiles()) {
                g.setColor(Color.BLACK);
                Rectangle r = tile.getTile();
                g.fillRect(r.x, r.y, r.height, r.width);
                g.setColor(tile.getColor());
                g.fillRect(r.x + 2, r.y + 2, r.height - 4, r.width - 4);
            }
        }
    }

    public class GameModel {

        private final GameTile[] tiles;

        public GameModel() {
            this.tiles = new GameTile[4];
            tiles[0] = new GameTile(10, 10, 64, 64, Color.RED.darker());
            tiles[1] = new GameTile(84, 10, 64, 64, Color.GREEN);
            tiles[2] = new GameTile(148, 10, 64, 64, Color.BLUE);
            tiles[3] = new GameTile(212, 10, 64, 64, Color.ORANGE);
        }

        public GameTile[] getTiles() {
            return tiles;
        }

    }

    public class GameTile {

        private final Color color;

        private final Rectangle tile;

        public GameTile(int x, int y, int width, int height, Color color) {
            this.tile = new Rectangle(x, y, width, height);
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

        public Rectangle getTile() {
            return tile;
        }

    }

}

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Performing Custom Painting section.

I went ahead and created the GUI you were trying to create.

enter image description here

When I create a Swing application, I use the model-view-controller (MVC) pattern. This pattern allows me to separate my concerns and focus on one small part of the application at a time.

I used your game tile idea and created a plain Java getter/setter class to hold a tile. In this simple example, all I needed was the color and rectangle information. In a more complex example, you could create a BufferedImage with a more detailed image than just one color.

I collected four GameTile instances in a separate model class. The model class would hold all the information for the application in another separate plain Java getter/setter class.

Writing the view was straightforward after creating the model. You have one drawing JPanel where you draw all the game tiles.

In this simple example, there's no controller. The controller is made up of one or more Action or ActionListener classes.

Here's the complete runnable code. I made all the additional classes inner classes so I could post the code as one block. You should separate the classes into different files within a package. For most of my Swing projects, I create separate model, view, and controller packages.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGameTileExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestGameTileExample());
    }

    private final GameModel model;

    public TestGameTileExample() {
        this.model = new GameModel();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Game Tiles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        frame.add(new DrawingPanel(model), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        private final GameModel model;

        public DrawingPanel(GameModel model) {
            this.model = model;
            this.setPreferredSize(new Dimension(1200, 720));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (GameTile tile : model.getTiles()) {
                g.setColor(Color.BLACK);
                Rectangle r = tile.getTile();
                g.fillRect(r.x, r.y, r.height, r.width);
                g.setColor(tile.getColor());
                g.fillRect(r.x + 2, r.y + 2, r.height - 4, r.width - 4);
            }
        }
    }

    public class GameModel {

        private final GameTile[] tiles;

        public GameModel() {
            this.tiles = new GameTile[4];
            tiles[0] = new GameTile(10, 10, 64, 64, Color.RED.darker());
            tiles[1] = new GameTile(84, 10, 64, 64, Color.GREEN);
            tiles[2] = new GameTile(148, 10, 64, 64, Color.BLUE);
            tiles[3] = new GameTile(212, 10, 64, 64, Color.ORANGE);
        }

        public GameTile[] getTiles() {
            return tiles;
        }

    }

    public class GameTile {

        private final Color color;

        private final Rectangle tile;

        public GameTile(int x, int y, int width, int height, Color color) {
            this.tile = new Rectangle(x, y, width, height);
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

        public Rectangle getTile() {
            return tile;
        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文