java - 重绘的多参数版本如何在 Java 中工作?

标签 java swing graphics operator-overloading repaint

我正在阅读 custom painting Java 教程,其中讨论了绘制和重绘方法。在本教程中,使用多个参数调用 repaint 方法作为方法中计算中使用的变量值。

那么,这些争论的目的是什么?我们可以发送任意数量的参数吗? 这些参数是否覆盖了在方法本身中设置这些变量值的语句?

例如: 现在,对于下面的代码,我可以用 robot_x 和 robot_y 的参数调用 repaint 来直接重绘形状吗?

        int robot_x=0;
        int robot_y=0;     
        int robot_radius=50;
        ArrayList<Integer> robot_list= new ArrayList<Integer>();
        robot_list=positionRobot(robot_x,robot_y);
        robot_x=robot_list.get(0);
        robot_y=robot_list.get(1);
        drawRobot(g,robot_x,robot_y,robot_radius);

        private void drawRobot(Graphics g, int x, int y, int radius)
       {
        g.setColor(Color.GREEN);
        g.fillOval(x, y, radius, radius);   
       }

来自 Oracle 网站的代码片段

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

最佳答案

首先确保您已通读并理解 JavaDocs for Component#repaint .

当使用这些方法时,剪切矩形被修改,因此只有您指定的区域可用于 Graphics 上下文中,这意味着您仍然可以在该区域之外自由绘画,但它不会有任何影响。

此外,请确保先“清除”之前的区域,否则最终会出现重影现象。

这意味着,除非您优化绘画过程以利用这一点,否则它可能对实际绘画该区域所需的时间几乎没有影响。

所以,简而言之,是的,调用 repaint(int, int, int, int),如果正确完成将允许您仅重绘 UI 的一部分,但您需要考虑考虑到您现在尝试绘制的内容可能已在其他地方绘制,并且还需要重新绘制...

引自Painting in AWT and Swing

A program must assume that a call to paint() implies that the area defined by the graphic's clip rectangle is "damaged" and must be completely repainted

"Smart" Painting

While the AWT attempts to make the process of rendering components as efficient as possible, a component's paint() implementation itself can have a significant impact on overall performance. Two key areas that can affect this process are:

Using the clip region to narrow the scope of what is rendered.
Using internal knowledge of the layout to narrow the scope of what children are painted (lightweights only).
If your component is simple -- for example, if it's a pushbutton -- then it's not worth the effort to factor the rendering in order to only paint the portion that intersects the clip rectangle; it's preferable to just paint the entire component and let the graphics clip appropriately. However, if you've created a component that renders complex output, like a text component, then it's critical that your code use the clip information to narrow the amount of rendering.

Further, if you're writing a complex lightweight container that houses numerous components, where the component and/or its layout manager has information about the layout, then it's worth using that layout knowledge to be smarter about determining which of the children must be painted. The default implementation of Container.paint() simply looks through the children sequentially and tests for visibility and intersection -- an operation that may be unnecessarily inefficient with certain layouts. For example, if a container layed out the components in a 100x100 grid, then that grid information could be used to determine more quickly which of those 10,000 components intersect the clip rectangle and actually need to be painted.

基本上,这就是说,除非您需要优化绘画过程,否则可能不值得。

关于java - 重绘的多参数版本如何在 Java 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007946/

相关文章:

java - 找不到符号: implements Drawable

java - android中的路径交叉点

java - 日期格式到日期时间格式器

java - 安卓程序无法运行

java - 从数据库检索到 JLabel

java Swing 重复使用 JFrame.pack() - 效率如何?

java - actionPerformed 跳过一步

java - 如何使用Java streams和reduce()获取Map的key和value的计算总和?

java - Jar 不使用包含的类或命名空间冲突

java - 翻转和旋转图像。仿射变换在 java 中无法正常工作