java - Swing Unable to draw circle -
so program wrote supposed draw circle everytime click on panel. reason have semicircle in top right hand corner upon startup, , can't draw circle. can see what's wrong it? circle should 20 px in diameter, drawn clicked point @ center.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class quizactionsinitial extends jframe { public jbutton redbutton = new jbutton("red"); public jbutton clearbutton = new jbutton("clear"); boolean isred = false; int x1,y1; boolean clear = false; circlepanel mypanel; public quizactionsinitial() { mypanel = new circlepanel(); add(mypanel, borderlayout.south); jpanel southpanel = new jpanel(new flowlayout()); clearbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ clear = true; } }); mypanel.addmouselistener(new circlelistener()); southpanel.add(redbutton); southpanel.add(clearbutton); add(southpanel, borderlayout.north); pack(); setvisible(true); } // end constructor public class circlepanel extends jpanel { public circlepanel() { setpreferredsize(new dimension(400,300)); setborder(borderfactory.createlineborder(color.blue, 2)); } public void paintcomponent(graphics gc){ super.paintcomponent(gc); gc.filloval(x1-10,y1-10,20,20); } } // end class circlepanel // end class circlepanel public class circlelistener extends mouseadapter{ public void mouseclicked(mouseevent e){ if (clear = false){ x1 = e.getx(); y1 = e.gety(); } repaint(); clear = false; } } public static void main(string args[]) { new quizactionsinitial(); } // end main } // end class quizactionsinitial
int x1,y1;
initialise values 0
, draw initial circle @ -10x-10
trying using java.awt.point
class instead, , when it's null
, don't paint anything...
//int x1,y1; private point point; //... public void paintcomponent(graphics gc){ super.paintcomponent(gc); if (point != null) { gc.filloval(point.x-10,point.y-10,20,20); } } //... public void mouseclicked(mouseevent e){ if (!clear){ point = evt.getpoint(); } clear = false; repaint(); }
oh, , if (clear = false){
assignment (making clear
equal false
, if
statement fail)
Comments
Post a Comment