java - What does the OFFSET do in repaint()-method with varargs? -


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

this snippet java-tutorials. it's tutorial painting in java. https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html

i don't understand offset exactly doing.
or happens inside repaint()-method.

i know, necessary paint square correctly, because noticed, without offset, square missing side, not painted, or not deleted first repaint()-method.

i let program write variables (used painting rectangle (squarex...and on)) in console. width , heigth 20 , not 21 (width + offset). when read variables inside paintcomponent()-method.
why don't understand, why square drawn correctly first time, every other time gets repainted without offset, drawn incorrectly.

and can't inside repaint()-method (at least don't know how it)

another little question: repaint method "delete / overwrite" object (in case square) wants draw, if variables color, position haven't changed?

this paintcomponent()-method doint.

g.setcolor(color.red); g.fillrect(squarex,squarey,squarew,squareh); g.setcolor(color.black); g.drawrect(squarex,squarey,squarew,squareh); 

what don't understand is, why first repaint()-method deletes old square. squarex/y/w/h same before. afterwards new coordinates mouse click, , square gets painted @ new location.
why same code delete in first repaint() , in second 1 creates new square?

sorry, if english bad. i'm germany.

thanks in advance answers!!!

i don't understand, why square drawn correctly first time,

the paintcomponent(...) method invoked without clipping, entire (250 x 200) area of panel repainted.

but every other time gets repainted without offset, drawn incorrectly.

when click on panel paintcomponent(...) method invoked clipping 2 repaint(...) requests consolidated single clipped painting request make painting more efficient.

for example, square painted @ (50, 50). if click @ (80, 80) area repainted be: (50, 50, 101, 101), minimum area needed clear old square , paint new square.

you can see size of clipped area change adding following paintcomponent() method:

system.out.println(g.getclipbounds()); 

note: simple painting don't need fancy. invoke single repaint() statement after resetting x/y values , entire panel repainted.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -