我的覆盖绘画方法没有被调用

编程入门 行业动态 更新时间:2024-10-25 18:32:12
本文介绍了我的覆盖绘画方法没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个JPanel打算充当游戏的HUD,自然地,我重写了paint方法来进行自己的自定义显示,但确实会调用此方法,但是只有在调整大小或最大化,最小化框架时,而不是当我的游戏循环告诉它repaint()时.由于另外两个面板完全重新粉刷,对我来说,这尤其奇怪.

I have a JPanel that is meant to act as a HUD for my game, naturally, I have overridden the paint method to do my own custom display, this does get called, but only upon resizing or maximizing, minimizing the frame, and not when my game loop tells it to repaint(). It seems particularly strange to me on account of my two other panels being repainted completely fine.

这是我的HUD课:

package base; import java.awt.Color; import java.awt.Graphics; import javax.swing.BoxLayout; import javax.swing.JPanel; public class HUD extends JPanel { private Shiphud[] shiphuds; public HUD(Ship[] ships) { shiphuds = new Shiphud[ships.length]; this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); for (int i = 0; i < shiphuds.length; i++) { shiphuds[i] = new Shiphud(ships[i]); this.add(shiphuds[i]); } } @Override public void paint(Graphics g) { for (int i = 0; i < shiphuds.length; i++) { shiphuds[i].repaint(); } } public void run() { repaint(); } private class Shiphud extends JPanel { private Ship ship; public Shiphud(Ship ship) { this.ship = ship; } @Override public void paint(Graphics g) { if (ship != null) { g.setColor(Color.BLACK); g.fillRect(0, 0, this.getWidth(), this.getHeight()); int fullbar = (int) (this.getWidth() * 0.8); g.setColor(Color.GREEN); g.fillRoundRect(getWidth() / 10, getHeight() / 10, (int) ((ship.energy / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.blue); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.4), (int) ((ship.fuel / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.YELLOW); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.6), (int) ((ship.ammo / 1000) * fullbar), getHeight() / 6, 10, 10); g.setColor(Color.MAGENTA); g.fillRoundRect(getWidth() / 10, (int) (getHeight() * 0.8), (int) ((ship.special / 1000) * fullbar), getHeight() / 6, 10, 10); System.out.println("here" + System.currentTimeMillis()); } } } }

它在我的游戏类更新中与其他两个面板一起被调用

It gets called in my Game classes update along with my other two panels

public void update() { ... display.run(); display2.run(); hud.run(); }

哪个被我的JFrame调用

Which gets called by my JFrame

public void runFrame() { Thread thread = new Thread() { @Override public void run() { gameLoop(); } }; thread.start(); } public void gameLoop() { while (true) { long beginTime = System.nanoTime(); game.update(); long timeTaken = System.nanoTime() - beginTime; long timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000; // in milliseconds if (timeLeft < 10) { timeLeft = 10; // set a minimum } try { // Provides the necessary delay and also yields control so that other thread can do work. Thread.sleep(timeLeft); } catch (InterruptedException ex) { } } }

很长一段时间以来,我一直想弄清楚这个问题,但我不明白,任何帮助都会得到高度重视

Been trying to figure this for a long time now, and I just don't get it, any help would be highly appreaciated

推荐答案

JPanel是轻量级的容器. 为了让孩子画画,您需要调用super.paint (g)在覆盖的paint()方法中.

JPanel is a lightweight container. In order for it's children to get painted, you need to call super.paint(g) in the overridden paint() method.

更多推荐

我的覆盖绘画方法没有被调用

本文发布于:2023-11-03 15:57:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!