有什么办法来绘制一个Applet的paint()方法之外吗? [英] Is there any way to draw outside of an Applet's paint() method?

查看:204
本文介绍了有什么办法来绘制一个Applet的paint()方法之外吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要画一个小程序,但我想这样做applet的paint()方法之外。目前,我有一个实体类,它需要一个图像的文件名和屏幕坐标作为参数。该构造使得到处理资源加载之类的东西,一个Sprite类的调用。

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.

所以我有一个叫MainMenuState类,它最初应该处理键盘输入我的小应用程序的主菜单(我工作的2D游戏),但我意识到,这将是更好地把绘画code在那里。我想要做的就是绘制主菜单背景主菜单状态下被创建时。这是我到目前为止有:

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 (扩展小程序)

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类)

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);
}

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

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?

感谢。

更新:好的。我修改了MainMenuState类调用使用一个离屏缓冲,然后我画到屏幕上的图形上下文背景的draw()方法。这工作,但我得到闪烁每当我调整窗口的大小。

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是除了这是一样的: background.draw(game.getOffScreenGraphics());

更新2:当然,你applet必闪烁,当他们调整?这可能是它。

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

推荐答案

不要使用的getGraphics(),因为得到的图形对象是永远不会保证持续。我建议你​​在一个BufferedImage绘制,如果你想画画外,然后绘制该图像的油漆方法里面,当你希望显示它。要显示的东西,必须最终在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.

这篇关于有什么办法来绘制一个Applet的paint()方法之外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆