[英]How can I completely repaint the canvas of a Java frame?
I am using AWT in Java to create animated frame.我在 Java 中使用 AWT 来创建动画帧。 It is composed of a
Frame
and a Canvas
, as well as some other components.它由
Frame
和Canvas
以及其他一些组件组成。 The code basically is:代码基本上是:
public class MyFrame extends Frame implements Runnable{
private Canvas c = new Canvas();
public MyFrame(){
c.setSize(100, 100);
this.setSize(100, 100);
this.add(c);
}
}
Now I want to draw a part of an image on the canvas.现在我想在画布上绘制图像的一部分。 I tried using the frame's paint method:
我尝试使用框架的paint方法:
public void paint(Graphics g){
Graphics canvasG = c.getGraphics();
canvasG.drawImage(img, x1, y1, w1, h1, x2, y2, w2, h2, null);
{
This draws the part of the picture, over the part that has been drawn previously.这将绘制图片的一部分,覆盖先前绘制的部分。 I, however, want to repaint the canvas completely.
但是,我想完全重新绘制画布。
Wow.哇。 To answer your question:
回答你的问题:
super.paint(g)
. super.paint(g)
。 This will redraw the Canvas erasing things that need to be erased.getGraphics()
when you need, it, but just don't forget to dispose of it when you're done so as not to run out of system resources.getGraphics()
获取图形对象,但不要忘记在完成后处理它,以免耗尽系统资源。 Have fun.玩得开心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.