- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我启动了一个小而简单的程序,并且有一个奇怪的错误,我无法在网上找出或找到原因。
我的程序现在所做的就是构建一个 JFrame,构建一个 JPanel,然后从线程将图像绘制到 JPanel 上。问题是,除非我在框架中单击,左键单击,右键单击,甚至单击鼠标滚轮使图像显示,否则图像不会显示,但调整框架大小不会使图像显示。我已经在面板和框架上尝试过 validate(),并且尝试将 repaint() 放入线程调用的代码中,但是当 repaint() 位于代码中时图像会闪烁。
我在实际绘制图像的代码中放置了一条打印语句,但该语句在我单击框架之前永远不会显示。我已经使用调试器进行了调试,但代码似乎被正确调用。如果我将 print 语句放在从主线程的 update() 函数调用的代码中的任何位置,则无需单击即可正确显示图像。
这是我的代码,任何建议将不胜感激:
框架
public class GameFrame extends JFrame{
int w = 1300;
int h = 740;
public GameFrame() {
this.setSize(w, h);
setTitle("Frame");
GamePanel panel = new GamePanel(w, h);
this.add(panel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame();
}
}
小组
public class GamePanel extends JPanel implements Runnable{
//core classes
GameRenderer renderer;
GameManager manager;
GameLoader loader;
//graphics stuff
private Graphics dbg;
private Image dbImage = null;
private int panelWidth;
private int panelHeight;
//game updating stuff
Thread animator;
boolean running;
public GamePanel(int w, int h) {
this.setSize(w, h);
this.setVisible(true);
this.setFocusable(true);
this.panelWidth = w;
this.panelHeight = h;
renderer = new GameRenderer();
manager = new GameManager();
loader = new GameLoader();
startGame();
}
private void startGame() {
animator = new Thread(this, "Animator");
animator.start();
running = true;
}
public void run() {
while(running) {
gameAction();
}
}
//everything that needs to be updated continuously
private void gameAction() {
//updateGame(); // Need code here to update everything
gameRender(); // Draw to the double buffer.
paintScreen(); // Draw double buffer to screen.
}
/**
* Draws the game image to the buffer.
*
*/
private void gameRender() {
if(dbImage == null) {
dbImage = createImage(this.panelWidth, this.panelHeight);
return;
}
dbg = dbImage.getGraphics();
renderer.draw((Graphics2D) dbg, panelWidth, panelHeight);
}
/**
* Draws the game image to the screen by drawing the buffer.
*/
private void paintScreen() {
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && (dbImage != null)) {
g.drawImage(dbImage, 0, 0, null);
g.dispose();
}
} catch (Exception e) { System.out.println("Graphics context error: " + e); }
}
}
图像渲染器
public class GameRenderer {
ImageManipulator im = new ImageManipulator();
/**
* Actually draw everything that needs to be drawn
* Layering also happens here
*/
public void draw(Graphics2D g,int screenWidth, int screenHeight) {
BufferedImage tileImg = im.loadImage("Images/tile.png");
Tile tile = new Tile(tileImg, 30, 30);
tile.draw(g);
}
}
带有我试图绘制的图像的类
public class Tile {
BufferedImage tileImg;
//x and y values based on tile grid - z based on height up
int tileX;
int tileY;
int tileZ;
//actual coordinate of top left corner of tile *image*
int pixelX;
int pixelY;
public Tile(BufferedImage img, int pixelX, int pixelY) {
this.pixelX = pixelX;
this.pixelY = pixelY;
this.tileImg = img;
}
public void draw(Graphics g) {
g.drawImage(tileImg, pixelX, pixelY, null);
}
}
同样,任何建议或想法都会有帮助。我不知道可能是什么原因造成的。另外,有人知道为什么这可能适用于包含打印语句但不包含打印语句吗?
最佳答案
您误解了如何在 Swing 组件中进行自定义绘制。调用 this.getGraphics()
是不正确的。绘制 Swing 组件的正确方法是重写其 paintComponent
方法。请注意,该方法中的第一行必须调用super.paintComponent
:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (dbImage != null) {
g.drawImage(dbImage, 0, 0, this);
}
}
另请注意,您不得尝试处置 Graphics 对象,因为它不是您创建的,而且它处于 Swing 的控制之下,而不是您的控制之下。
有关完整详细信息,请参阅 Painting in AWT and Swing教程。
当您想要重新绘制游戏时,只需调用面板的 repaint方法。因此,您的 gameAction 方法应该用 repaint() 替换paintScreen(),它将自动调用面板的paintComponent 方法(以及一些其他方法;正如该教程所解释的,绘画不是一件简单的事情)。
您需要输入一个简短的sleep在 run()
方法的循环内部调用。如果您尝试不断更新和重新绘制游戏,程序将没有时间实际绘制任何内容。例如,如果您希望游戏每秒更新五次(即每 200 毫秒):
try {
while (running) {
gameAction();
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
关于java - JPanel 在单击鼠标之前不会绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27594289/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!