Java 小程序上的paint() 设置为无限循环。我如何避免我的方法做同样的事情?
我正在创建一个游戏,我希望重新绘制一些图像,而其他图像保持不变。我已将我的方法放在 java 小程序上的 paint()
中,但这似乎是在无限循环中访问这些方法。
如何创建一个“驱动程序方法”来访问我的方法,同时也使用 draw()
?
public void paint (Graphics g)
{
bufferGraphics.clearRect (0, 0, dim.width, dim.height);
//mainScreen ();
g.drawImage (offscreen, 0, 0, this);
} // end Paint method
public void update (Graphics g)
{
paint (g);
}
public void main (String[] args)
{
game ();
}
I'm creating a game and I want some of the image to be repainted while others to stay constant. I had put my methods in the paint()
on java applet but this seems to access the methods in an endless loop.
How do I create a "driver method" that will access my methods but also use the draw()
at the same time?
public void paint (Graphics g)
{
bufferGraphics.clearRect (0, 0, dim.width, dim.height);
//mainScreen ();
g.drawImage (offscreen, 0, 0, this);
} // end Paint method
public void update (Graphics g)
{
paint (g);
}
public void main (String[] args)
{
game ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用 AWT 时,会覆盖 Paint() 和 update()。既然您刚刚学习绘画,为什么不编写一个 Swing 小程序并扩展 JApplet,因为如今 Swing 更常用呢?然后通过扩展 JPanel 或 JComponent 来完成自定义绘制。然后将此组件添加到 JApplet 的内容窗格中,就像它是 JFrame 一样。
请阅读 Swing 教程中有关 自定义绘画 的部分以获取更多示例使用 Swing 进行绘画。
Overriding paint() and update() is done when using AWT. Since you are just learning about painting why not write a Swing applet and extend JApplet, since Swing is more commonly used these days? Then custom painting is done by extending JPanel or JComponent. Then you add this component to the content pane of the JApplet, just like it was a JFrame.
Read the section from the Swing tutorial on Custom Painting for more examples of painting with Swing.