- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Swing 中构建游戏,我知道我可以使用其他工具,但这仅用于学习目的。我面临的问题与线程有关。
基本上,在对 JPanel(GamePanel 类)进行任何绘制之前,主线程会运行 JFrame(Window 类)的 setVisible 属性。在我做了一些研究之后,我发现我可以使用 SwingUtilities.invokeLater()
方法解决这个问题,该方法应该给游戏线程一些时间来准备在 JPanel 上制作的绘图。
我还尝试将 setVisible (true)
属性放置在不同的线程中,并使该线程 hibernate 2、3 秒。这两种方法有时有效,有时无效。
我不知道这是否有什么不同,但我的操作系统是:macOS Catalina。
导致此线程问题的简单示例。
游戏启动器
public class GameLauncher {
public GameLauncher(){
new Window("MyGame");
}
public static void main(String[] args) {
new GameLauncher();
}
}
窗口
public class Window extends JFrame {
public Window(String title){
setTitle(title);
setContentPane(new GamePanel(1280, 1280));
setLocationRelativeTo(null);
setIgnoreRepaint(true);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setVisible(true);
SwingUtilities.invokeLater(()->setVisible(true));
}
}
游戏面板
package com.progresso.games;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class GamePanel extends JPanel implements Runnable {
public int width;
public int height;
private BufferedImage bufferedImage;
private Thread gameThread;
private boolean isRunning = false;
Graphics2D graphics2D = null;
public GamePanel(int width, int height){
this.width = width;
this.height = height;
setPreferredSize(new Dimension(width, height));
setFocusable(true);
requestFocus();
}
@Override
public void addNotify() {
super.addNotify();
if(gameThread == null){
gameThread = new Thread(this, "GameThread");
gameThread.start();
}
}
private void init(){
isRunning = true;
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics2D = (Graphics2D) bufferedImage.getGraphics();
}
@Override
public void run() {
init();
while (isRunning) {
update();
render(graphics2D);
draw();
}
}
private void update(){ }
private void render(Graphics2D g){
if(g != null){
g.setColor(Color.BLACK);
g.fillRect(0,0, width, height);
}
}
private void draw(){ }
}
最佳答案
对 JFrame 进行子类化并不是一个好主意。相反,扩展 JPanel,并在其 PaintCpomponent() 方法中绘制您的内容。尝试这样的事情(不完整的代码):
public class GamePanel extends JPanel {
public GamePanel() {
// Create panel content here
}
@Override
public void paintComponent( Graphics g ) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0, width, height);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
// create the component to display in the frame
JPanel panel = new GamePanel();
;
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
});
}
}
关于java - 在进行任何绘图之前调用 setVisible 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60020128/
我有一个代码,当我单击 jButton 时,它应该设置为不可见,而另一个已设置为 FALSE 的 JButton 应该为 TRUE,setVisible(true) 不起作用。 buttonGrid[
我看到了一些关于这个的帖子,我明白了这个问题。但是怎么绕过去呢?我有 ListView 和可以展开的项目,但是一旦 View 消失,它就不会再可见,除非它有可用空间。如何创造这个空间? private
JFrame 在循环中无法正确显示。代码:- import javax.swing.*; import java.awt.event.*; import java.awt.*; import j
下面的代码应该重置框架gameFrame: private void reset() { moveCount = 0; gameFrame.setVisible(false);
请看下图来理解问题: 如您所见,有一个 RelativeLayout 包含一个自定义 View 和一个 LinearLayout。 在它们之间,还有另一个 View,Visibility 被设置为 G
我有一个 JScrollPane包含 JPanel然后包含一些自定义组件,它们是 JPanel 的扩展(它们都是同一类型)。此 JScrollPane 在包含 JFrame 之前设置为不可见显示。 当
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我是 JFrame 的新手,我正在尝试做一个项目,如果按“注销”按钮,则可以完美执行以下代码, public void actionlogout() { lButton.addActionLi
我试图隐藏除 1 之外的所有 jList,它是从 jcomboBox (cbLists) 的索引中获得的,它并没有真正隐藏 jLists,就像禁用而不是隐藏一样,我需要它隐藏所有内容,就像完全不可见一
你能帮我吗?我正在尝试创建一个菜单,只需按一下按钮即可显示项目。我想我应该使菜单项visible:false,然后在MainActivity中切换这个属性。但我做不到正确的事。我需要 3 个新菜单项。
我想知道,这样做会更有效率吗: setVisible(false) // if the component is invisible 或者像这样: if(isVisible()){
简单介绍一下我在做什么。 我根据用户登录的帐户类型授予不同的功能。 用户的帐户类型将通过 Firebase 数据库中的 Firebase 引用检索。如果您查看此图像,就会看到箭头指示。这是显示帐户类型
我在处理程序中隐藏了一个按钮(这是在我使其在应用程序的另一种状态下可见之后)。处理程序从正在运行的线程接收消息,然后更新 GUI。 问题是附近的(不是全部)按钮和 TextView 也从屏幕上消失了。
我有一个带有 jcheckbox 和 jtextfield 的 jframe(它有更多组件)。 我将标签设置为 setVisible(false),当复选框被选中时,它应该使标签可见。它确实如此,但您
这是我的布局:
我是 android 开发的新手。我在 LinearLayout 中包含一个网格,组成网格的每个项目都是一个按钮。当用户按下这些按钮中的任何一个时,我希望此 LinearLayout 不可见。 这是我
我在抽屉中使用 NavigationView 并且 Menu 中有一个项目具有指向 的 app:actionLayout 属性>LinearLayout 包含一个 TextView,并且 TextVi
我有一个 LinearLayout,我希望能够通过单击“更多详细信息”链接来显示/隐藏它。我通过调用来做到这一点 moreDetailsSection.setVisibility(View.VISIB
在 JavaFX 中,如果我有一个包含 2 个 VBox 元素的场景,并且每个 VBox 中都有多个 Label。 如果我将顶部的VBox设置为invisible,为什么底部的VBox不上移顶部的场景
所以我一直在论坛中寻找如何做到这一点,但我发现没有任何效果。当我在图像按钮上调用 setVisibility() 时,该按钮不受影响。下面是 onCreate 方法中的代码,当我运行应用程序时,两个按
我是一名优秀的程序员,十分优秀!