java - paintComponent method doesn't draw anything when I pass class's variable -
my java code:
import java.awt.basicstroke; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.ellipse2d; import javax.swing.jpanel; public class concentriccircles2d extends jpanel { double myx = 0; double myy = 0; int mywidth = getwidth(); int myheight = getheight(); public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setpaint(color.blue); g2.setstroke(new basicstroke(5.0f)); g2.draw(new ellipse2d.double(myx, myy, mywidth, myheight)); }
when use local variable inside paintcomponent method works fine. how can solve problem? (i create panel on separate class.)
mywidth
, myheight
values set when concentriccircles2d
object instantiated. has 0 weight , height @ point.
so sentence
ellipse2d.double(myx, myy, mywidth, myheight)
is equal
ellipse2d.double(0, 0, 0, 0)
and paint no ellipse @ all.
replace sentence
g2.draw(new ellipse2d.double(myx, myy, mywidth, myheight));
with original
g2.draw(new ellipse2d.double(0, 0, getwidth(), getheight()));
and ellipse centered in parent jframe
drawn , dimensions change if resize parent.
Comments
Post a Comment