java - Can't draw oval on JPanel -
i'm trying draw ovals on jpanel when mouse clicked. code doesn't call paintcomponent, nothing happens on jpanel. part i'm missing?
public class main extends jframe implements mouselistener{ jpanel thepanel = new jpanel(){ @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.red); (circle c : circles){ g.filloval(c.x, c.y, c.diameter, c.diameter); system.out.println(c.x + "a"); } } }; jframe frame=new jframe(); int x,y; arraylist<circle >circles = new arraylist<circle>(); public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new main(); } }); } public main(){ frame.setsize(512,512); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.addmouselistener(this); frame.add(thepanel); frame.setvisible(true); } @override public void mouseclicked(mouseevent e) { system.out.println(e.getx()); circle c = new circle(); c.x=e.getx(); c.y=e.gety(); c.diameter=10; circles.add(c); repaint(); }
circle class
class circle { public int x, y, diameter; }
i didn't use getters , setters don't think that's problem.
if change repaint()
thepanel.repaint()
, should able see circles being added.
they appear little off-position, because getting frame-coordinates frame's mouse listener, trying paint in panel-coordinates.
edit:
camickr pointed out in comment, have 2 jframe
s: 1 instantiated new jframe()
, , 1 instantiated new main()
. reason repaint
not having desired effect: 1 calling repaint
on not 1 looking at. camickr suggests not inherit main
jframe
, advice.
Comments
Post a Comment