为什么 PaintComponent 仅在某些 JPanel 上被调用?

发布于 2025-01-17 05:08:13 字数 8935 浏览 3 评论 0原文

我正在尝试向不同的 JPanel 添加背景(此后它们将被称为 Window)。这些Windows是我创建的类,并使它们继承JPanel。然后根据程序的状态将一个窗口设置为程序 JFrame 的内容面板。当在某些 Windows 中设置背景而在其他 Windows 中未设置背景时,就会出现问题。背景设置是使用 PaintComponent(Graphics g) 方法执行的,但尽管我尝试修复该错误,但没有成功。

这是我认为对那些想要帮助的人可能有用的代码:

主循环:

public class Game{

//here comes other stuff (constructor, main, other methods...)

    private void run(){
        while(true){
            if(GameState.changed){
                Screen.getInstance().seeWindow(state);
                GameState.changed = false;
            }else {
                Screen.getInstance().requestFocus(state);
            }
        }
    }
}

屏幕类:

package view;

import game.GameState;
import view.wins.*;

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

public class Screen extends JFrame {
    private final int WIDTH;
    private final int HEIGHT;

    private static Screen instance = null;
    private JComponent titleWindow, menuWindow, settingsWindow;

    private Screen(){
        WIDTH = 1152;
        HEIGHT = 768;

        setTitle("Game");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-WIDTH/2, (dim.height - 50)/2-HEIGHT/2);
        Dimension size = new Dimension(WIDTH, HEIGHT);

        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);

        setResizable(false);
        setVisible(true);
    }

    public static Screen getInstance() {
        if(instance == null){
            instance = new Screen();
        }
        return instance;
    }

    public void seeWindow(GameState state){
        switch(state){
            case TITLE -> setContentPane(getTitleWindow());
            case MENU -> setContentPane(getMenuWindow());
            case SETTINGS -> setContentPane(getSettingsWindow());
        }
        pack();
    }

    public void requestFocus(GameState state){
        switch (state){
            case TITLE -> getTitleWindow().requestFocus();
            case MENU -> getMenuWindow().requestFocus();
            case SETTINGS -> getSettingsWindow().requestFocus();
        }
    }

    private JComponent getTitleWindow(){
        if(titleWindow == null){
            titleWindow = new TitleWindow();
        }
        return titleWindow;
    }

    private JComponent getMenuWindow(){
        if(menuWindow == null){
            menuWindow = new MenuWindow();
        }
        return menuWindow;
    }

    private JComponent getSettingsWindow(){
        if(settingsWindow == null){
            settingsWindow = new SettingsWindow();
        }
        return settingsWindow;
    }
}

窗口抽象类:

package view.wins;

import utilz.GFXManager;
import view.Screen;

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

public abstract class Window extends JComponent implements Observer {
    private Image background;

    public Window(String background){
        setLayout(new BorderLayout());
        setPreferredSize(Screen.getInstance().getPreferredSize());

        setBackground(background);
        setFocusable(true);
    }

    protected void setBackground(String backgroundName){
        this.background = GFXManager.getInstance().getImage("backgrounds/" + backgroundName + ".png");
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, null);
    }

}

正确设置背景的窗口:

package view.wins;

import game.GameState;
import game.Game;
import jdk.swing.interop.SwingInterOpUtils;
import logic.TitleLogic;
import utilz.GFXManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;

public class TitleWindow extends Window implements Observer {
    private final String BELOW_TITLE_TEXT;
    private final ImageIcon TITLE_ICON;

    private JLabel lblTitleIcon, lblBelowTitle;
    private KeyController keyController;

    public TitleWindow(){
        super("title_background");
        BELOW_TITLE_TEXT = "Press enter to start";
        TITLE_ICON = new ImageIcon(GFXManager.getInstance().getImage("texts/title.png"));

        TitleLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());
        addKeyListener(new KeyController());

        add(getLblTitleIcon(), BorderLayout.CENTER);
        add(getLblBelowTitle(), BorderLayout.SOUTH);
    }

    private JLabel getLblTitleIcon(){
        if(lblTitleIcon == null){
            lblTitleIcon = new JLabel(TITLE_ICON);
        }
        return lblTitleIcon;
    }

    private JLabel getLblBelowTitle(){
        if(lblBelowTitle == null){
            lblBelowTitle = new JLabel(BELOW_TITLE_TEXT, SwingConstants.CENTER);
            lblBelowTitle.setFont(new Font("MS Gothic", Font.PLAIN, 24));
            lblBelowTitle.setForeground(new Color(30,230,120));
        }
        return lblBelowTitle;
    }

    private KeyController getKeyController(){
        if(keyController == null){
            keyController = new KeyController();
        }
        return keyController;
    }

    @Override
    public void update(Observable o, Object arg) {
        if(TitleLogic.getInstance().isTickColorChange()){
            getLblBelowTitle().setForeground(new Color(120, 30, 230));
        }else{
            getLblBelowTitle().setForeground(new Color(30,230,120));
        }
    }

    private class KeyController extends KeyAdapter {
        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getKeyChar() == '\n'){
                Game.getInstance().setState(GameState.MENU);
            }else if(e.getKeyChar() == 'c'){
                TitleLogic.getInstance().tickColorChange();
            }
        }
    }
}

未设置背景的窗口:

package view.wins;

import game.GameState;
import game.Game;
import logic.MenuLogic;
import view.objs.Button;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

public class MenuWindow extends Window implements Observer {
    private JPanel btnPanel;
    private Button btnStartNewGame, btnLoadGame, btnSettings, btnExit;
    private Controller controller;

    public MenuWindow(){
        super("title_background");
        MenuLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());

        add(getBtnPanel(), BorderLayout.CENTER);
    }

    private JPanel getBtnPanel(){
        if(btnPanel == null){
            btnPanel = new JPanel(new GridLayout(4,1));

            btnPanel.add(getBtnStartNewGame());
            btnPanel.add(getBtnLoadGame());
            btnPanel.add(getBtnSettings());
            btnPanel.add(getBtnExit());
        }
        return btnPanel;
    }

    private Button getBtnStartNewGame(){
        if(btnStartNewGame == null){
            btnStartNewGame = new Button("mediumLong", "Start new game", getController());
        }
        return btnStartNewGame;
    }

    private Button getBtnLoadGame(){
        if(btnLoadGame == null){
            btnLoadGame = new Button("mediumLong", "Load game", getController());
        }
        return btnLoadGame;
    }

    private Button getBtnSettings(){
        if(btnSettings == null){
            btnSettings = new Button("mediumLong", "Settings", getController());
        }
        return btnSettings;
    }

    private Button getBtnExit(){
        if(btnExit == null){
            btnExit = new Button("mediumLong", "Exit", getController());
        }
        return btnExit;
    }

    private Controller getController(){
        if(controller == null){
            controller = new Controller();
        }
        return controller;
    }

    @Override
    public void update(Observable o, Object arg) {

    }

    private class Controller extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource().equals(getBtnStartNewGame())){

            }else if(e.getSource().equals(getBtnLoadGame())){

            }else if(e.getSource().equals(getBtnSettings())){
                Game.getInstance().setState(GameState.SETTINGS);
            }else if(e.getSource().equals(getBtnExit())){
                System.exit(0);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            ((Button)e.getSource()).changeHighlight();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            ((Button)e.getSource()).changeHighlight();
        }
    }
}

按钮是我的类自己的。

如果有人想自己测试它或查看更多代码这里是github存储库。

我尝试了一切,甚至更多。我注意到 MenuWindow 的图形未初始化,因此问题可能会出现,因为该窗口未渲染。我不知道。

I'm trying to add a background to different JPanels (henceforth they will be called Window). These Windows are classes I created and make them inherit JPanel. Then depending on the state of the program one Window is set as the content panel of the program's JFrame. The problem comes when in some Windows the background gets set and in others not. The background setting is performed ussing the paintComponent(Graphics g) method, but despite I've tried to fix the bug, I didnt' success.

Here is the code I think might be useful for those who wanna help:

Main Loop:

public class Game{

//here comes other stuff (constructor, main, other methods...)

    private void run(){
        while(true){
            if(GameState.changed){
                Screen.getInstance().seeWindow(state);
                GameState.changed = false;
            }else {
                Screen.getInstance().requestFocus(state);
            }
        }
    }
}

Screen class:

package view;

import game.GameState;
import view.wins.*;

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

public class Screen extends JFrame {
    private final int WIDTH;
    private final int HEIGHT;

    private static Screen instance = null;
    private JComponent titleWindow, menuWindow, settingsWindow;

    private Screen(){
        WIDTH = 1152;
        HEIGHT = 768;

        setTitle("Game");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-WIDTH/2, (dim.height - 50)/2-HEIGHT/2);
        Dimension size = new Dimension(WIDTH, HEIGHT);

        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);

        setResizable(false);
        setVisible(true);
    }

    public static Screen getInstance() {
        if(instance == null){
            instance = new Screen();
        }
        return instance;
    }

    public void seeWindow(GameState state){
        switch(state){
            case TITLE -> setContentPane(getTitleWindow());
            case MENU -> setContentPane(getMenuWindow());
            case SETTINGS -> setContentPane(getSettingsWindow());
        }
        pack();
    }

    public void requestFocus(GameState state){
        switch (state){
            case TITLE -> getTitleWindow().requestFocus();
            case MENU -> getMenuWindow().requestFocus();
            case SETTINGS -> getSettingsWindow().requestFocus();
        }
    }

    private JComponent getTitleWindow(){
        if(titleWindow == null){
            titleWindow = new TitleWindow();
        }
        return titleWindow;
    }

    private JComponent getMenuWindow(){
        if(menuWindow == null){
            menuWindow = new MenuWindow();
        }
        return menuWindow;
    }

    private JComponent getSettingsWindow(){
        if(settingsWindow == null){
            settingsWindow = new SettingsWindow();
        }
        return settingsWindow;
    }
}

Window abstract class:

package view.wins;

import utilz.GFXManager;
import view.Screen;

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

public abstract class Window extends JComponent implements Observer {
    private Image background;

    public Window(String background){
        setLayout(new BorderLayout());
        setPreferredSize(Screen.getInstance().getPreferredSize());

        setBackground(background);
        setFocusable(true);
    }

    protected void setBackground(String backgroundName){
        this.background = GFXManager.getInstance().getImage("backgrounds/" + backgroundName + ".png");
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, null);
    }

}

A Window where the background is correctly set:

package view.wins;

import game.GameState;
import game.Game;
import jdk.swing.interop.SwingInterOpUtils;
import logic.TitleLogic;
import utilz.GFXManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;

public class TitleWindow extends Window implements Observer {
    private final String BELOW_TITLE_TEXT;
    private final ImageIcon TITLE_ICON;

    private JLabel lblTitleIcon, lblBelowTitle;
    private KeyController keyController;

    public TitleWindow(){
        super("title_background");
        BELOW_TITLE_TEXT = "Press enter to start";
        TITLE_ICON = new ImageIcon(GFXManager.getInstance().getImage("texts/title.png"));

        TitleLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());
        addKeyListener(new KeyController());

        add(getLblTitleIcon(), BorderLayout.CENTER);
        add(getLblBelowTitle(), BorderLayout.SOUTH);
    }

    private JLabel getLblTitleIcon(){
        if(lblTitleIcon == null){
            lblTitleIcon = new JLabel(TITLE_ICON);
        }
        return lblTitleIcon;
    }

    private JLabel getLblBelowTitle(){
        if(lblBelowTitle == null){
            lblBelowTitle = new JLabel(BELOW_TITLE_TEXT, SwingConstants.CENTER);
            lblBelowTitle.setFont(new Font("MS Gothic", Font.PLAIN, 24));
            lblBelowTitle.setForeground(new Color(30,230,120));
        }
        return lblBelowTitle;
    }

    private KeyController getKeyController(){
        if(keyController == null){
            keyController = new KeyController();
        }
        return keyController;
    }

    @Override
    public void update(Observable o, Object arg) {
        if(TitleLogic.getInstance().isTickColorChange()){
            getLblBelowTitle().setForeground(new Color(120, 30, 230));
        }else{
            getLblBelowTitle().setForeground(new Color(30,230,120));
        }
    }

    private class KeyController extends KeyAdapter {
        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getKeyChar() == '\n'){
                Game.getInstance().setState(GameState.MENU);
            }else if(e.getKeyChar() == 'c'){
                TitleLogic.getInstance().tickColorChange();
            }
        }
    }
}

A Window where the background isn't set:

package view.wins;

import game.GameState;
import game.Game;
import logic.MenuLogic;
import view.objs.Button;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

public class MenuWindow extends Window implements Observer {
    private JPanel btnPanel;
    private Button btnStartNewGame, btnLoadGame, btnSettings, btnExit;
    private Controller controller;

    public MenuWindow(){
        super("title_background");
        MenuLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());

        add(getBtnPanel(), BorderLayout.CENTER);
    }

    private JPanel getBtnPanel(){
        if(btnPanel == null){
            btnPanel = new JPanel(new GridLayout(4,1));

            btnPanel.add(getBtnStartNewGame());
            btnPanel.add(getBtnLoadGame());
            btnPanel.add(getBtnSettings());
            btnPanel.add(getBtnExit());
        }
        return btnPanel;
    }

    private Button getBtnStartNewGame(){
        if(btnStartNewGame == null){
            btnStartNewGame = new Button("mediumLong", "Start new game", getController());
        }
        return btnStartNewGame;
    }

    private Button getBtnLoadGame(){
        if(btnLoadGame == null){
            btnLoadGame = new Button("mediumLong", "Load game", getController());
        }
        return btnLoadGame;
    }

    private Button getBtnSettings(){
        if(btnSettings == null){
            btnSettings = new Button("mediumLong", "Settings", getController());
        }
        return btnSettings;
    }

    private Button getBtnExit(){
        if(btnExit == null){
            btnExit = new Button("mediumLong", "Exit", getController());
        }
        return btnExit;
    }

    private Controller getController(){
        if(controller == null){
            controller = new Controller();
        }
        return controller;
    }

    @Override
    public void update(Observable o, Object arg) {

    }

    private class Controller extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource().equals(getBtnStartNewGame())){

            }else if(e.getSource().equals(getBtnLoadGame())){

            }else if(e.getSource().equals(getBtnSettings())){
                Game.getInstance().setState(GameState.SETTINGS);
            }else if(e.getSource().equals(getBtnExit())){
                System.exit(0);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            ((Button)e.getSource()).changeHighlight();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            ((Button)e.getSource()).changeHighlight();
        }
    }
}

Button is a class of my own.

If anyone wants to test it for his own or see more code here is the github repository.

I tried everything and more. I've noticed that the Graphics of MenuWindow aren't initialized, so the problem could come because that window isn't rendered. I don't know.

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

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

发布评论

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

评论(1

空城缀染半城烟沙 2025-01-24 05:08:13

这……

private void run(){
    while(true){
        if(GameState.changed){
            Screen.getInstance().seeWindow(state);
            GameState.changed = false;
        }else {
            Screen.getInstance().requestFocus(state);
        }
    }
}

是个坏主意。除了“疯狂循环”本身通常是一个坏主意之外,Swing 也不是线程安全的并且是单线程的。

这意味着,如果它在事件调度线程的上下文中运行,它将阻止它并阻止其处理任何新事件。如果它不在 EDT 中运行,您将面临导致任意数量的图形故障或其他“脏线程”问题的风险。

再加上像这样的“疯狂循环”也会消耗 CPU 周期,您会增加巨大的性能开销,但几乎没有任何收益。

首先查看 Swing 中的并发

Screen.getInstance().requestFocus(state); 也是对 KeyListener 已知限制的一种破解,通过使用 KeyListener 可以更好地解决这些限制a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html" rel="nofollow noreferrer">键绑定 API

考虑到所有这些, Kavaliro 应该看起来更像...

package game;

import view.Screen;

public class Kavaliro {
    private static Kavaliro instance = null;

    private GameState state;

    private Kavaliro() {
        state = GameState.TITLE;
        Screen.getInstance().seeWindow(state);
    }

    public static Kavaliro getInstance() {
        if (instance == null) {
            instance = new Kavaliro();
        }
        return instance;
    }

    public static void main(String[] args) {
        Kavaliro game = Kavaliro.getInstance();
    }

    public void setState(GameState state) {
        Screen.getInstance().seeWindow(state);
    }
}

这也不是在 Java 中创建单例的好方法。

public static Kavaliro getInstance() {
    if (instance == null) {
        instance = new Kavaliro();
    }
    return instance;
}

您可以查看 Java 单例设计模式最佳实践示例什么是实现单例模式的有效方法在Java中?或者你可以只使用依赖注入(可能还有一些对依赖注入与单例


现在,正如我所说,KeyListener 通常是监视用户键盘输入的一个糟糕选择,这意味着 TitleWindow< /code> 应该更改为更像...

package view.wins;

import game.GameState;
import game.Kavaliro;
import game.Utilities;
import logic.TitleLogic;
import utilz.GFXManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;

public class TitleWindow extends Window implements Observer {
    private final String BELOW_TITLE_TEXT;
    private final ImageIcon TITLE_ICON;

    private JLabel lblTitleIcon, lblBelowTitle;

    public TitleWindow() {
        super("title_background");
        BELOW_TITLE_TEXT = "Press enter to start";
        TITLE_ICON = new ImageIcon(GFXManager.getInstance().getImage("texts/title.png"));

        TitleLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());

        Utilities.addKeyBinding(this, Utilities.keyStrokeFor(Utilities.Input.ENTER), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Kavaliro.getInstance().setState(GameState.MENU);
            }
        });
        Utilities.addKeyBinding(this, Utilities.keyStrokeFor(Utilities.Input.TITLE_TOGGLE), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TitleLogic.getInstance().tickColorChange();
            }
        });

        add(getLblTitleIcon(), BorderLayout.CENTER);
        add(getLblBelowTitle(), BorderLayout.SOUTH);
    }

    private JLabel getLblTitleIcon() {
        if (lblTitleIcon == null) {
            lblTitleIcon = new JLabel(TITLE_ICON);
        }
        return lblTitleIcon;
    }

    private JLabel getLblBelowTitle() {
        if (lblBelowTitle == null) {
            lblBelowTitle = new JLabel(BELOW_TITLE_TEXT, SwingConstants.CENTER);
            lblBelowTitle.setFont(new Font("MS Gothic", Font.PLAIN, 24));
            lblBelowTitle.setForeground(new Color(30, 230, 120));
        }
        return lblBelowTitle;
    }

    @Override
    public void update(Observable o, Object arg) {
        if (TitleLogic.getInstance().isTickColorChange()) {
            getLblBelowTitle().setForeground(new Color(120, 30, 230));
        } else {
            getLblBelowTitle().setForeground(new Color(30, 230, 120));
        }
    }
}

现在,为了让生活更轻松,我编写了一个快速的“实用程序”类...

package game;

import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
import javax.swing.KeyStroke;

public class Utilities {

    public static enum KeyState {
        PRESSED, RELEASED
    }

    public static enum Input {
        ENTER(KeyEvent.VK_ENTER), TITLE_TOGGLE(KeyEvent.VK_C);

        private int keyEvent;

        private Input(int keyEvent) {
            this.keyEvent = keyEvent;
        }

        protected KeyStroke getKeyStroke(int modifiers, KeyState keyState) {
            return KeyStroke.getKeyStroke(keyEvent, 0, keyState == KeyState.RELEASED ? true : false);
        }
    }

    public static void addKeyBinding(JComponent parent, KeyStroke keyStroke, Action action) {
        addKeyBinding(parent, keyStroke.toString(), keyStroke, action);
    }

    public static void addKeyBinding(JComponent parent, String name, KeyStroke keyStroke, Action action) {
        InputMap inputMap = parent.getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = parent.getActionMap();

        inputMap.put(keyStroke, name);
        actionMap.put(name, action);
    }

    public static KeyStroke keyStrokeFor(Input key) {
        return keyStrokeFor(key, 0, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(Input key, int modifiers) {
        return keyStrokeFor(key, modifiers, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(Input key, int modifiers, KeyState keyState) {
        return key.getKeyStroke(modifiers, keyState);
    }

    public static KeyStroke keyStrokeFor(int key) {
        return keyStrokeFor(key, 0, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(int key, int modifiers) {
        return keyStrokeFor(key, modifiers, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(int key, int modifiers, KeyState keyState) {
        return KeyStroke.getKeyStroke(key, 0, keyState == KeyState.RELEASED ? true : false);
    }

}

这处理了很多样板/重复代码,但也提供了一种“限制”可能的输入的方法API。请注意,Input enum 允许使用 ENTERTITLE_TOGGLE 作为 keyStrokeFor 的可行选项方法。

请注意,TITLE_TOGGLE“隐藏”了所使用的击键,但同时它也是相当自记录的。有很多其他方法可以构建这些概念,这只是一个例子。

而且……

public class Button extends JLabel {

会从我这里得到一个非常大的,不,。 API 中已经有一个按钮组件,您应该使用它。它甚至支持诸如翻转之类的功能,例如 示例示例,以及您可以使用的其他功能的真实性我们将花费大量时间进行重建。

请参阅如何使用按钮、复选框和单选按钮 。而且,是的,如果您确实愿意,您可以删除“外观和感觉”填充背景和边框。


我不了解你,而且我不使用 Intellij,但是 return ImageIO.read(new File(GFX_PATH + name)); 对我来说坏了。

像这样的资源确实应该嵌入到应用程序的运行时上下文中(技术术语“包含在 Jar 中”)。当“工作目录”上下文与 res 文件夹的位置不同时,这将防止在运行时尝试查找资源时出现问题。

您应该使用...

return ImageIO.read(getClass().getResource(GFX_PATH + name));

但是如何配置 Intelllji 以将 res 文件夹的上下文包含到您的应用程序上下文中超出了我的范围。


public void seeWindow(GameState state){
    // Use a CardLayout
    switch(state){
        case TITLE -> setContentPane(getTitleWindow());
        case MENU -> setContentPane(getMenuWindow());
        case SETTINGS -> setContentPane(getSettingsWindow());
    }
    pack();
}

嗯,目光短浅。相反,您应该使用 CardLayout 来为您完成此操作,并且通常会可靠地工作。

请参阅 如何使用 CardLayout
了解更多详情

This...

private void run(){
    while(true){
        if(GameState.changed){
            Screen.getInstance().seeWindow(state);
            GameState.changed = false;
        }else {
            Screen.getInstance().requestFocus(state);
        }
    }
}

is a bad idea. Apart from the fact that a "wild loop" is just generally a bad idea on its own, Swing is also not thread safe and is single threaded.

This means that if this is running within the context of the Event Dispatching Thread, it will block it and prevent from ever been able to process any new events. If it's not running in the EDT, you're risking causing any number of graphic glitches or other "dirty thread" issues.

Add into the fact that a "wild loop" like this will also consume the CPU cycles, you're adding a tremendous performance overhead for little to no gain.

Start by taking a look at Concurrency in Swing

Screen.getInstance().requestFocus(state); is also a hack around the, known, limitations of KeyListener which are better solved through the use the key bindings API

Taking all that into account, Kavaliro should look something more like...

package game;

import view.Screen;

public class Kavaliro {
    private static Kavaliro instance = null;

    private GameState state;

    private Kavaliro() {
        state = GameState.TITLE;
        Screen.getInstance().seeWindow(state);
    }

    public static Kavaliro getInstance() {
        if (instance == null) {
            instance = new Kavaliro();
        }
        return instance;
    }

    public static void main(String[] args) {
        Kavaliro game = Kavaliro.getInstance();
    }

    public void setState(GameState state) {
        Screen.getInstance().seeWindow(state);
    }
}

This is also not a good way to make a singleton in Java.

public static Kavaliro getInstance() {
    if (instance == null) {
        instance = new Kavaliro();
    }
    return instance;
}

You could take a look at Java Singleton Design Pattern Best Practices with Examples and What is an efficient way to implement a singleton pattern in Java? OR you could just make use of Dependency Injection (and probably some research into dependency injection vs singleton)


Now, as I said, KeyListener is a generally a poor choice of monitoring user keyboard input, this means that TitleWindow should change, to something a little more like...

package view.wins;

import game.GameState;
import game.Kavaliro;
import game.Utilities;
import logic.TitleLogic;
import utilz.GFXManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;

public class TitleWindow extends Window implements Observer {
    private final String BELOW_TITLE_TEXT;
    private final ImageIcon TITLE_ICON;

    private JLabel lblTitleIcon, lblBelowTitle;

    public TitleWindow() {
        super("title_background");
        BELOW_TITLE_TEXT = "Press enter to start";
        TITLE_ICON = new ImageIcon(GFXManager.getInstance().getImage("texts/title.png"));

        TitleLogic.getInstance().addObserver(this);

        setLayout(new BorderLayout());

        Utilities.addKeyBinding(this, Utilities.keyStrokeFor(Utilities.Input.ENTER), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Kavaliro.getInstance().setState(GameState.MENU);
            }
        });
        Utilities.addKeyBinding(this, Utilities.keyStrokeFor(Utilities.Input.TITLE_TOGGLE), new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TitleLogic.getInstance().tickColorChange();
            }
        });

        add(getLblTitleIcon(), BorderLayout.CENTER);
        add(getLblBelowTitle(), BorderLayout.SOUTH);
    }

    private JLabel getLblTitleIcon() {
        if (lblTitleIcon == null) {
            lblTitleIcon = new JLabel(TITLE_ICON);
        }
        return lblTitleIcon;
    }

    private JLabel getLblBelowTitle() {
        if (lblBelowTitle == null) {
            lblBelowTitle = new JLabel(BELOW_TITLE_TEXT, SwingConstants.CENTER);
            lblBelowTitle.setFont(new Font("MS Gothic", Font.PLAIN, 24));
            lblBelowTitle.setForeground(new Color(30, 230, 120));
        }
        return lblBelowTitle;
    }

    @Override
    public void update(Observable o, Object arg) {
        if (TitleLogic.getInstance().isTickColorChange()) {
            getLblBelowTitle().setForeground(new Color(120, 30, 230));
        } else {
            getLblBelowTitle().setForeground(new Color(30, 230, 120));
        }
    }
}

Now, to make life easier, I wrote a quick "utility" class...

package game;

import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import static javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
import javax.swing.KeyStroke;

public class Utilities {

    public static enum KeyState {
        PRESSED, RELEASED
    }

    public static enum Input {
        ENTER(KeyEvent.VK_ENTER), TITLE_TOGGLE(KeyEvent.VK_C);

        private int keyEvent;

        private Input(int keyEvent) {
            this.keyEvent = keyEvent;
        }

        protected KeyStroke getKeyStroke(int modifiers, KeyState keyState) {
            return KeyStroke.getKeyStroke(keyEvent, 0, keyState == KeyState.RELEASED ? true : false);
        }
    }

    public static void addKeyBinding(JComponent parent, KeyStroke keyStroke, Action action) {
        addKeyBinding(parent, keyStroke.toString(), keyStroke, action);
    }

    public static void addKeyBinding(JComponent parent, String name, KeyStroke keyStroke, Action action) {
        InputMap inputMap = parent.getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = parent.getActionMap();

        inputMap.put(keyStroke, name);
        actionMap.put(name, action);
    }

    public static KeyStroke keyStrokeFor(Input key) {
        return keyStrokeFor(key, 0, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(Input key, int modifiers) {
        return keyStrokeFor(key, modifiers, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(Input key, int modifiers, KeyState keyState) {
        return key.getKeyStroke(modifiers, keyState);
    }

    public static KeyStroke keyStrokeFor(int key) {
        return keyStrokeFor(key, 0, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(int key, int modifiers) {
        return keyStrokeFor(key, modifiers, KeyState.PRESSED);
    }

    public static KeyStroke keyStrokeFor(int key, int modifiers, KeyState keyState) {
        return KeyStroke.getKeyStroke(key, 0, keyState == KeyState.RELEASED ? true : false);
    }

}

This takes care of a lot of the boiler plate/repeating code, but also provides a means to "limit" the possible inputs to the API. Note the Input enum allows for ENTER and TITLE_TOGGLE as viable options for the keyStrokeFor methods.

Note that TITLE_TOGGLE "hides" the key stroke which is used, but it is pretty self documenting at the same time. There are lots of other ways you could build out these concepts, this is just an example.

And...

public class Button extends JLabel {

would get a very large, no, from me. There is already a button component within the API and you should use it. It even supports things like roll over, for example and example, as well as a verity of other functionalities, which you're going to be spending a lot of time re-building.

See How to Use Buttons, Check Boxes, and Radio Buttons. And, yes, you can remove the Look And Feel fill background and borders if you really want to.


I don't know about you, and I don't use Intellij, but return ImageIO.read(new File(GFX_PATH + name)); broke for me.

Resources like these should really be embedded within the application's runtime context (tech babble for "included in the Jar"). This will prevent issues with trying to locate the resources at runtime when the "working directory" context is not the same as the location of the res folder.

You should be using...

return ImageIO.read(getClass().getResource(GFX_PATH + name));

but how you configure Intellji to include the context of the res folder into your application context is beyond me.


public void seeWindow(GameState state){
    // Use a CardLayout
    switch(state){
        case TITLE -> setContentPane(getTitleWindow());
        case MENU -> setContentPane(getMenuWindow());
        case SETTINGS -> setContentPane(getSettingsWindow());
    }
    pack();
}

is, well, short sighted. Instead you should be using a CardLayout which will do this for you, and in away which generally will work reliably.

See How to Use CardLayout
for more details

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