[英]How to draw on a JPanel using Graphics
I am working on a project and i have already made a menu and a JPanel
but now i want to draw over the JPanel
and add some HUD to my game.我正在做一个项目,我已经制作了一个菜单和一个
JPanel
但现在我想在JPanel
上绘制并在我的游戏中添加一些 HUD。
I am providing a picture with the final panel of my game and i want to add at the left a blue rectangle with the player heads and at the right another rectangle with actions, money etc but i want to construct them outside my Window class where i construct the frame and all of the JPanels.我正在为我的游戏的最后一个面板提供一张图片,我想在左侧添加一个带有玩家头部的蓝色矩形,在右侧添加另一个带有动作、金钱等的矩形,但我想在我的 Window 类之外构建它们构建框架和所有 JPanel。 Generally now that the player has chosen to play a local game i want to be able to draw and erase on this panel as the game continues.
通常,现在玩家选择玩本地游戏,我希望能够在游戏继续时在此面板上绘制和擦除。
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(1);
return;
}
Graphics g = null;
window.render();
if (gameState == STATE.MultiPlay) {
handler.render(g);
hud.render(g);
}
if (gameState == STATE.LocalPlay) {
handler.render(g);
hud.render(g);
}
}
The code i provided is the render method i am using at my game engine class.我提供的代码是我在游戏引擎类中使用的渲染方法。 With my game engine i have made this method circles 10 times per second and i wanted from here to instantiate the
Graphics
class and then go on with the other class and rendering everything i want to draw over that JPanel
.使用我的游戏引擎,我让这个方法每秒循环 10 次,我想从这里实例化
Graphics
类,然后继续使用另一个类并渲染我想在该JPanel
绘制的所有内容。
How can i draw over that JPanel
using Graphics
or something like that?我如何使用
Graphics
或类似的东西绘制该JPanel
?
You must use Graphics object like that:您必须像这样使用 Graphics 对象:
Graphics g= bs.getDrawGraphics();
I think it will be usefull, have a nice day.我认为它会很有用,祝你有美好的一天。
PD: If you don't want to do that, you can also override the method paint(Graphcis g) and use the parameter. PD:如果你不想这样做,你也可以覆盖方法paint(Graphcis g)并使用参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.