gpt4 book ai didi

java - 如何在 Swing 中创建 "Game Option/Pause Screen"?

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:02:59 24 4
gpt4 key购买 nike

背景:用 Swing 制作游戏。这是简单的回合制游戏。没有发生太多事情。因此,我认为我不需要实现 Game Tick。相反,我的想法是当一个组件发生变化或需要更新时,只需简单地重新验证/重新绘制该组件,而不是重新绘制整个屏幕。

我有一个 GameJPanel,目前它上面有所有组件。这个 JPanel 是包含重新验证/重新绘制等组件的那个。我想我可以制作包含 GameJPanel 和我的 OptionJPanel 的 JLayeredPane。在 GameJPanel 上有一个按钮,当按下它时会导致 OptionJPanel 显示在它上面并使其 JPanel 50% 透明(因此它会产生使 GameJPanel 变暗的效果)。

但是,一旦我这样做,发生的事情是 GameJPanel 开始替换 OptionJPanel 组件(因为事件...等等;组件的重新绘制)。

所以目前我不知道该怎么做。我在想,如果我有某种游戏滴答声,我就不会遇到这个问题,但是,我不是 100% 确定。我有点担心,如果我实现游戏中的事件会导致 GameJPanel 组件显示半秒然后被替换的 gametick。有一些事件会导致组件在不手动执行的情况下自行重绘(例如 JLabel setText(); 的快速示例)

enter image description here作为我努力追求的一个例子。

我尝试过使用 CardLayout,但我无法弄清楚如何在后台看到 GameJPanel 时让 OptionJPanel 位于 GameJPanel 之上(我尝试设置背景颜色,setOpaque(false)...,试图限制选项 JPanel 大小,但我认为 CardLayout 会拉伸(stretch)它(不是 100% 确定))这样做时我得到的只是灰色背景。

我不想走 CardLayout 路线,因为将来我还计划将组件放在 GameJPanel 的顶部(就像有人点击一个按钮,让另一个面板在不同的层上有一个组件滑入或滑出等) .我在 GameJPanel 中大量使用 CardLayout 和我的其他组件来交换屏幕,但不需要让显示的组件后面的其他组件显示出来。

任何关于如何解决这个问题的想法都很棒,甚至是展示这一点的示例代码。

最佳答案

如上所述,您将使用 JDialog,这是一个易于制作(类似于制作 JFrame)且易于放置的组件。只需将它“相对于”JFrame 放置,例如

myDialog.setLocationRelativeTo(myJFrame);

... 它会自动以 JFrame 为中心。棘手的部分是使底层 JFrame 变暗,为此您需要使用添加到 JFrame 根 Pane 的 JGlassPane,其中一组具有使用 alpha 复合值的背景颜色。棘手的部分是在不引起副作用的情况下绘制较暗的背景,要做到这一点,请阅读 Rob Camick(StackOverflow 用户 camickr)关于使用 alpha 合成在 Swing 中绘制的优秀教程,您可以在此处找到:Java Tips Weblog: Backgrounds with Transparency

此处显示了此类程序的示例:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class DialogEg {
// path to example image used as "game" background
private static final String IMG_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/7/76/Jump_%27n_Bump.png";

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}

private static void createAndShowGui() {
// get the "game" background image, or exit if fail
BufferedImage img = null;
try {
URL imgUrl = new URL(IMG_PATH);
img = ImageIO.read(imgUrl);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}

// pass "game" image into main JPanel so that it will be drawn
DeMainPanel mainPanel = new DeMainPanel(img);
JFrame frame = new JFrame("Dialog Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel); // add main JPanel to JFrame
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}

// main JPanel
@SuppressWarnings("serial")
class DeMainPanel extends JPanel {
private BufferedImage img; // background image

// JButton action that shows the JDialog and darkens the glasspane
private PauseAction pauseAction = new PauseAction("Pause");

public DeMainPanel(BufferedImage img) {
super();
this.img = img;
add(new JButton(pauseAction));
}

// draw the "game" background image within the JPanel if not null
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}

// size this JPanel to match the image's size
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
int width = img.getWidth();
int height = img.getHeight();
return new Dimension(width, height);
}
}

// Action / ActionListener for JButton -- shows JDialog and darkens glasspane
@SuppressWarnings("serial")
class PauseAction extends AbstractAction {
private static final int ALPHA = 175; // how much see-thru. 0 to 255
private static final Color GP_BG = new Color(0, 0, 0, ALPHA);
private DeDialogPanel deDialogPanel = new DeDialogPanel(); // jpanel shown in JDialog

public PauseAction(String name) {
super(name);
}

@Override
public void actionPerformed(ActionEvent e) {
// comp is our JButton
Component comp = (Component) e.getSource();
if (comp == null) {
return;
}

// create our glass pane
JPanel glassPane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
// magic to make it dark without side-effects
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
// more magic below
glassPane.setOpaque(false);
glassPane.setBackground(GP_BG);

// get the rootpane container, here the JFrame, that holds the JButton
RootPaneContainer win = (RootPaneContainer) SwingUtilities.getWindowAncestor(comp);
win.setGlassPane(glassPane); // set the glass pane
glassPane.setVisible(true); // and show the glass pane

// create a *modal* JDialog
JDialog dialog = new JDialog((Window)win, "", ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(deDialogPanel); // add its JPanel to it
dialog.setUndecorated(true); // give it no borders (if desired)
dialog.pack(); // size it
dialog.setLocationRelativeTo((Window) win); // ** Center it over the JFrame **
dialog.setVisible(true); // display it, pausing the GUI below it

// at this point the dialog is no longer visible, so get rid of glass pane
glassPane.setVisible(false);

}
}

// JPanel shown in the modal JDialog above
@SuppressWarnings("serial")
class DeDialogPanel extends JPanel {
private static final Color BG = new Color(123, 63, 0);

public DeDialogPanel() {
JLabel pausedLabel = new JLabel("PAUSED");
pausedLabel.setForeground(Color.ORANGE);
JPanel pausedPanel = new JPanel();
pausedPanel.setOpaque(false);
pausedPanel.add(pausedLabel);

setBackground(BG);
int eb = 15;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
setLayout(new GridLayout(0, 1, 10, 10));
add(pausedPanel);
add(new JButton(new FooAction("RESUME")));
add(new JButton(new FooAction("RESTART")));
add(new JButton(new FooAction("EXIT TO MAP")));
}

// simple action -- all it does is to make the dialog no longer visible
private class FooAction extends AbstractAction {
public FooAction(String name) {
super(name);
}

@Override
public void actionPerformed(ActionEvent e) {
Component comp = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose(); // here -- dispose of the JDialog
}
}
}

GUI 最初看起来像这样:

enter image description here

但是当对话框显示并且玻璃面板变暗时,它看起来像这样:

enter image description here

关于java - 如何在 Swing 中创建 "Game Option/Pause Screen"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45777039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com