背景:在Swing中制作游戏。这是简单的回合制基础游戏。没有很多事情发生。因此,我认为我不需要实施Game Tick。相反,我的想法是当组件发生更改或需要更新时,只需简单地即时重新验证/重新绘制该组件,而不是重新绘制整个屏幕。
我有一个GameJPanel,目前上面有所有组件。这个JPanel是包含重新验证/重新绘制等组件的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的顶部放置组件(就像有人单击一个按钮,在不同层上放置另一个面板,使组件滑入或滑出等)。
我在GameJPanel中使用CardLayout和其他组件来交换屏幕,但并不需要在显示的组件后面放置其他组件。
关于如何执行此操作的任何想法都将是出色的,甚至是显示此操作的示例代码。
最佳答案
如上所述,您将使用JDialog,这是一个易于制作(类似于制作JFrame)并且易于放置的组件。只需将其“相对于” JFrame放置,例如,
myDialog.setLocationRelativeTo(myJFrame);
...,它将自动在JFrame上居中。棘手的部分是使基础JFrame变暗,为此,您需要使用添加到JFrame根窗格中的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最初看起来像这样:
但是,当对话框显示并且玻璃窗格变暗时,它看起来像这样: