有什么方法可以在Applet的paint()方法之外进行绘制吗?

发布于 2024-12-29 16:16:32 字数 1701 浏览 0 评论 0原文

我有一个正在尝试绘制的小程序,但我想在小程序的 Paint() 方法之外进行绘制。目前我有一个实体类,它将图像的文件名和屏幕坐标作为参数。构造函数调用 Sprite 类来处理资源加载和类似的事情。

所以我有一个名为 MainMenuState 的类,最初应该处理我的小程序主菜单的键盘输入(我正在开发 2D 游戏),但我意识到最好也将绘图代码放在那里。我想要做的是在构建主菜单状态时绘制主菜单背景。这是我到目前为止所拥有的:

Game.java (扩展 Applet)

public void init() {
    this.setSize(500, 500);

    // New games have the MainMenuState by default because the game starts at the main menu.
    setState(new MainMenuState(this));

    Entity background = new Entity("../Assets/Menus/titleScreen.png", 0, 0);
    background.draw(this.getGraphics());

}

MainMenuState.java (扩展抽象 State 类)

public MainMenuState(Game game) {
    super(game);
    Graphics g = game.getGraphics();
    Entity background = new Entity("../Assets/Menus/titleScreen.png", 0, 0);
    background.draw(g);
}

小程序运行并且绘制方法都被正确调用,但是小程序从不将图像绘制到屏幕上。目前我的小程序的paint()方法是空的。如何在小程序的 Paint() 方法之外将图像绘制到屏幕上?

谢谢。

更新:好的。我修改了 MainMenuState 类,以使用离屏缓冲区的图形上下文调用背景的 draw() 方法,然后将其绘制到屏幕上。这可行,但每当我调整窗口大小时我都会闪烁。

Game.java

public void init() {
    this.setSize(500, 500);

    offScreen = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    graphics = offScreen.getGraphics();

    // New games have the MainMenuState by default because the game starts at the main menu.
    setState(new MainMenuState(this));
}

public void paint(Graphics g) {
    g.drawImage(offScreen, 0, 0, this);
}

public void update(Graphics g) {
    paint(g);
}

MainMenuState.java 是相同的,除了这个: background.draw(game.getOffScreenGraphics());

更新 2:当然,小程序在运行时是否总是闪烁调整大小?可能就是这样。

I have an applet that I'm trying to paint but I want to do it outside of the applet's paint() method. Currently I have an Entity class which takes an image's filename and screen coordinates as arguments. The constructor makes a call to a Sprite class which handles resource loading and stuff like that.

So I have a class called MainMenuState which was originally supposed to handle keyboard input for the main menu of my applet (I'm working on a 2D game) but I realized that it would be better to put drawing code in there as well. What I want to do is draw the main menu background when the main menu state is constructed. Here's what I have so far:

Game.java (extends Applet)

public void init() {
    this.setSize(500, 500);

    // New games have the MainMenuState by default because the game starts at the main menu.
    setState(new MainMenuState(this));

    Entity background = new Entity("../Assets/Menus/titleScreen.png", 0, 0);
    background.draw(this.getGraphics());

}

MainMenuState.java (extends abstract State class)

public MainMenuState(Game game) {
    super(game);
    Graphics g = game.getGraphics();
    Entity background = new Entity("../Assets/Menus/titleScreen.png", 0, 0);
    background.draw(g);
}

The applet runs and the draw methods are all being called correctly but the applet never paints the image to the screen. Currently my applet's paint() method is empty. How can I draw images to the screen outside of the applet's paint() method?

Thanks.

Update: Alright. I modified my MainMenuState class to call the background's draw() method using the graphics context of an off-screen buffer, which I then paint to the screen. This works but I get flickering whenever I resize the window.

Game.java

public void init() {
    this.setSize(500, 500);

    offScreen = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
    graphics = offScreen.getGraphics();

    // New games have the MainMenuState by default because the game starts at the main menu.
    setState(new MainMenuState(this));
}

public void paint(Graphics g) {
    g.drawImage(offScreen, 0, 0, this);
}

public void update(Graphics g) {
    paint(g);
}

MainMenuState.java is the same except for this: background.draw(game.getOffScreenGraphics());

Update 2: Of course, do applets always flicker when they resize? That could be it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2025-01-05 16:16:32

切勿使用 getGraphics() ,因为获取的 Graphics 对象永远无法保证持久存在。如果您想在“绘制之外”绘制,我建议您在 BufferedImage 中绘制,然后当您希望显示该图像时,在 paint 方法内部绘制该图像。为了显示某些东西,它最终必须在paint方法中绘制,句号。

Never use getGraphics() since the Graphics object obtained is never guaranteed to persist. I suggest that you draw in a BufferedImage if you want to draw "outside of paint", and then draw that image inside of the paint method when you wish to display it. To display something it must eventually be drawn in the paint method, period.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文