具有多次重绘调用的 Swing 组件的自定义绘制 [英] Custom painting of a Swing component with multiple calls to repaint

查看:55
本文介绍了具有多次重绘调用的 Swing 组件的自定义绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void moveSquare(int x, int y) {
    int OFFSET = 1;
    if ((squareX!=x) || (squareY!=y)) {
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        squareX=x;
        squareY=y;
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
    } 
}

在上面的代码中(完整代码可以在Demo App中找到的执行自定义绘画"Java教程),第一个repaint方法应该在前一个正方形的位置绘制一个正方形,第二个repaint应该在新正方形的位置绘制另一个正方形.但这实际上并没有发生.取而代之的是,之前的方块消失了,新的方块被绘制出来.

In the above code (complete code can be found in the Demo App of the "Performing Custom Painting" Java Tutorial), the first repaint method should paint a square at the position of the previous square, and the second repaint should paint another square at position of the new square. But this is actually not happening. Instead, the previous square disappears and the new one is painted.

当前一个消失时,新的正方形如何被绘制?

How does the new square get painted while the previous one disappears?

推荐答案

在您调用 repaint() 后,它不会立即重新绘制组件.但是它在EDT的事件队列中添加了再次绘制组件的请求.

After you call repaint() it do not repaint the component instantly. But it add the request to paint the component again in the event queue in EDT.

下面解释了每行代码中发生的事情..

What happens in each code line is expained below..

repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);

标记由正方形包围的区域 (squareX,squareY,squareW+OFFSET,squareH+OFFSET) 将要重新绘制.但它不会被重新绘制,直到 RepaintManager 这样做..

Mark the area bounded by the square (squareX,squareY,squareW+OFFSET,squareH+OFFSET) is going to be repaint. But it do not get repainted until RepaintManager do so..

squareX=x;
squareY=y;

更改squareXsquareY 的值.但它不会改变要重新绘制的较早标记的区域.现在,要重新绘制的区域是以前的值.

Change the value of squareX and squareY. But it do not change the earlier marked region to be repainted. Now also, the region to be repainted is previous values.

repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);

标记由正方形包围的区域 (squareX,squareY,squareW+OFFSET,squareH+OFFSET) 将要重新绘制.现在有两个部分 RepaintManager 必须重新绘制.以前的广场和新的广场.但它不会被重新绘制,直到 RepaintManager 这样做..

Mark the area bounded by the square (squareX,squareY,squareW+OFFSET,squareH+OFFSET) is going to be repaint. Now there are two parts that RepaintManager has to repaint. Previous square and new square. But it do not get repainted until RepaintManager do so..

最后,当时机成熟时,RepaintManager 绘制组件.

Finally when the time comes, RepaintManager paints the component.

protected void paintComponent(Graphics g) {
    super.paintComponent(g);       
    g.drawString("This is my custom Panel!",10,20);
    g.setColor(Color.RED);
    g.fillRect(squareX,squareY,squareW,squareH);
    g.setColor(Color.BLACK);
    g.drawRect(squareX,squareY,squareW,squareH);
} 

现在组件只绘制 2 个区域.(前一个方块和新方块)但红色方块只会在新方块内绘制.在旧广场上没有什么可画的.所以之前画的东西会被抹掉..

Now the component is painting only 2 areas. (Previous square and the new square) But the red square will be drawn only within the new square. In the old square there is nothing to draw. So previously drawn things will be erased..

实际上虽然 repaint() 有 2 个方法调用,但是 paintComponents() 只会被调用一次.要重绘的总面积由 RepaintManager 处理,paintComponents() 只调用一次..

Actually though there are 2 method calls for repaint(), paintComponents() will be called only once. The total area to be repainted is handled by RepaintManager and paintComponents() is called only once..

这篇关于具有多次重绘调用的 Swing 组件的自定义绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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