背景:在 Swing 中制作游戏。这是简单的回合基础游戏。不是很多事情发生。因此,我认为我不需要实现 Game Tick。相反,我的想法是当一个组件被更改或需要更新时,只需简单地重新验证/重新绘制该组件,而不是重新绘制整个屏幕。
我有一个 GameJPanel,它目前包含所有组件。这个 JPanel 是包含重新验证/重新绘制等组件的组件。我想我可以制作包含 GameJPanel 和我的 OptionJPanel 的 JLayeredPane。在 GameJPanel 上有一个按钮,当按下它时会导致 OptionJPanel 显示在它的顶部,并且它的 JPanel 50% 透明(因此它会影响它使 GameJPanel 变暗)。
然而,一旦我这样做了,发生的事情就是 GameJPanel 开始替换 OptionJPanel 组件(因为事件......等;重新绘制组件)。
所以目前我不知道该怎么做。我在想,如果我有某种游戏标记,我就不会遇到这个问题,但是,我不是 100% 确定。我有点担心如果我实现了游戏中的事件会导致 GameJPanel 组件显示半秒然后被替换的游戏。有一些事件会导致组件无需手动重新绘制(例如 JLabel setText(); 的快速示例)
我尝试过使用 CardLayout 但我无法弄清楚如何在后台看到 GameJPanel 时让 OptionJPanel 位于 GameJPanel 之上(我尝试设置背景颜色 setOpaque(false)...,试图限制 Option JPanel 大小但我认为 CardLayout 会拉伸它(不是 100% 确定))这样做时我得到的只是灰色背景。
我不想走 CardLayout 路线,因为将来我还计划将组件放在 GameJPanel 的顶部(比如有人单击按钮,在不同层上有另一个面板有一个组件滑入或滑出等)。我大量使用 CardLayout 和 GameJPanel 中的其他组件来交换屏幕,但不需要让其他组件在显示的后面显示出来。
关于如何解决这个问题的任何想法都是很好的,甚至是显示这一点的示例代码。
如上所述,您将使用 JDialog,这是一个易于制作(类似于制作 JFrame)且易于放置的组件。只需将它“相对于”JFrame 放置,例如,
myDialog.setLocationRelativeTo(myJFrame);
Run Code Online (Sandbox Code Playgroud)
...它会自动以 JFrame 为中心。棘手的部分是使底层 JFrame 变暗,为此您需要使用添加到 JFrame 根窗格的 JGlassPane,一组具有使用 alpha 复合值的背景颜色。棘手的部分是在不引起副作用的情况下绘制较暗的背景,为此,请阅读 Rob Camick(StackOverflow 用户camickr)关于使用 alpha 合成绘制 Swing 的优秀教程,您可以在此处找到:Java Tips Weblog: Backgrounds透明
此类程序的示例如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
// 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
}
}
}
Run Code Online (Sandbox Code Playgroud)
GUI 最初看起来像这样:
但是当对话框显示并且玻璃面板变暗时,它看起来像这样: