3

I am reading the custom painting tutorial in Java which talks about paint and repaint methods. In this tutorial, repaint method is called with multiple arguments as variable values used in calculations in the method.

So, what is the purpose of these arguments? and can we send any number of arguments? Do these arguments override the statements which set the values of these variables in the method itself?

For example: Now, for the below code, can I call repaint with the arguments of robot_x and robot_y to directly repaint the shape?

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

Code Snippet from the Oracle Website

 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);
        } 
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ambidextrous
  • 882
  • 2
  • 12
  • 26
  • I don't see you calling `repaint` and I don't see `paint` anywhere either...The code is entirely out of context. Which version of `repaint` are you talking about, there's 5 `repaint` methods available to classes that extend `JComponent`? – MadProgrammer Jul 29 '14 at 03:50
  • @MadProgrammer I am not calling the repaint in the above code. I have just added that for reference. I mean the code in the tutorial website. I have just added the snippet for it. – Ambidextrous Jul 29 '14 at 03:52
  • Do you want to know about varargs? [read here](http://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java) – Braj Jul 29 '14 at 03:53
  • Start by making sure you have read through and understand the [JavaDocs for `Component#repaint`](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#repaint(int,%20int,%20int,%20int)). When using these methods, the clipping rectangle is modified so only the area you specify is available to be within in the `Graphics` context, this means you are still free to paint outside this area, but it will have no effect. Also, make sure that you "clear" the previous area first, otherwise you end up with ghosting artifacts. – MadProgrammer Jul 29 '14 at 03:53
  • What this means is, unless you painting process is optimised to take advantage of this, it may have little to no effect on the time it takes to actually paint the area. – MadProgrammer Jul 29 '14 at 03:54
  • @MadProgrammer Okay, I will read this and get back to you. Thanks – Ambidextrous Jul 29 '14 at 03:55
  • Don't be to concerned with optimisation where you don't need any... – MadProgrammer Jul 29 '14 at 04:03

1 Answers1

3

Start by making sure you have read through and understand the JavaDocs for Component#repaint.

When using these methods, the clipping rectangle is modified so only the area you specify is available to be within in the Graphics context, this means you are still free to paint outside this area, but it will have no effect.

Also, make sure that you "clear" the previous area first, otherwise you will end up with ghosting artifacts.

What this means is, unless you painting process is optimised to take advantage of this, it may have little to no effect on the time it takes to actually paint the area.

So, in short, yes, calling repaint(int, int, int, int), if done correctly will allow you to repaint only a portion of the UI, but you need to make considerations to allow for the fact that what you are trying to paint now might have been painted somewhere else and will need to also be repainted...

Quoting from 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

And

"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.

Basically, what this is saying is, unless you have a need to optimise the painting process, it might not be worth while.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366