java - Why is paintComponent() continuously and asynchronously being called without explicit repaint() call? -
so question has 2 parts, think may related, , it's abstract. briefly, here's i'm doing:
i have jframe
jpanel
, child
jpanels
each 3 jbuttons
on it. created jcomponent
called glasspanel
jframe
(i.e. myjframe.setglasspane(glasspanel)
), allows me paint on jpanels , buttons.
(1) triggered clicking 3 buttons on jpanel
, glasspanel
set visible
(which appears call paintcomponent()
). relates first question.
(2) in paintcomponent()
draw , paint rectangles , images, using double buffer, onto glasspanel
. relates second question.
here's relevant glasspanel class code (this not sscce because abstract question now):
import java.awt.basicstroke; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.line2d; import javax.swing.jcomponent; public class glasspanel extends jcomponent { @override protected void paintcomponent(graphics g) { super.paintcomponent(g); setdoublebuffered(true); graphics2d g2 = (graphics2d) g; g2.drawrect(x,y,width,height); g2.fillrect(x,y,width,height); g2.drawimage(img, x, y, this); } }
by placing system.out.print
statement inside paintcomponent() method, tell it being called continuously , asynchronously. how think call made, see (1). also, let's i'm absolutely there no call repaint()
anywhere in code (i've checked many, many times). basis of first question.
the first time click 3 buttons, goes smoothly. rectangle , images both drawn immediately. however, when click next 3 buttons (at point, glasspanel
has been setvisible(true)
, first rectangle , image still on screen, painted on first 3 buttons), second rectangle , image only load partially. when click away jframe
, onto eclipse window i've run program from, number of calls paintcomponent()
rapidly increases same amount each time , the partially loaded image(s) , rectangle(s) , show in background jframe
. when click jframe, number of calls goes again exact amount). basis of second question.
update: here's read:
also, when gui covered window , becomes uncovered, painting system invokes paintcomponent method painting area equal newly uncovered area.
my questions are:
(1) why might paintcomponent()
called without repaint()
? or, similar question, might calling paintcomponent()?
update: after doing little math, believe it's being called every component (all buttons , panels) continuously. still, no call repaint()...
(2) why images loaded partially until take focus jframe window?
note have tried many things: (a) creating own doublebuffer , using no double buffer (i know it's animation), (b) overriding , not overriding paintcomponent()
, (c) drawing , not drawing image (the rectangle still takes time load), (d) making absolutely sure there no repaint()
, (e) using , not using swingutilities.invokelater(new runnable() { public void run() { //stuff});
, (f) done if
statement setvisible(true) once.
i can try , past sscce if must, think these more abstract. thanks!
well, think i've answered both questions. first, why paintcomponent() being called continuously, was not being called continuously. being called components when first shows gui. when eclipse window covers , uncovers it, gets called more times.
for second, has clipbounds
of graphics2d
object/thing. found out how clipbounds
changing each paint call, when set clip @ start of paintcomponent()
method, images show immediately. (btw, looks great!).
with twist: after image displayed, each click of button image. haven't figured out exactly, though. looks repainting same image on old images.
so have figure out how keep old images draw new ones when appropriate , draw/add new ones onto glasspanel
.
update: calling repaint()
after each button clicked helps little. still causes image flicker somewhat, if adding layer, button pressed, , returns normal when user lets go.
Comments
Post a Comment