java - Multiple calls of paintComponent() -
i have these 2 classes:
public class pencil extends jcomponent implements mouselistener, mousemotionlistener{ plansa plansa; graphics g; public pencil(plansa newcanvas){ this.plansa = newcanvas; this.plansa.setflagshape(false); } @override public void mousedragged(mouseevent arg0) { plansa.setmousedragged(arg0); this.plansa.setflagshape(false); plansa.paintcomponent(plansa.getgraphics()); } @override public void mouseclicked(mouseevent e) { } @override public void mousepressed(mouseevent arg0) { plansa.setmousepressed(arg0); } @override public void mousereleased(mouseevent arg0) { // todo auto-generated method stub plansa.setmousereleased(arg0); this.plansa.setflagshape(true); plansa.paintcomponent(plansa.getgraphics()); } @override public void mouseentered(mouseevent e) { } @override public void mouseexited(mouseevent e) { } @override public void mousemoved(mouseevent e) { } }
and one:
public class plansa extends jpanel{ image image; pencil pencil; //... void init(){ this.setshape("freeline"); this.setcolor(color.black); this.size=1; this.type="round"; this.fill=false; this.setbackground(color.white); pencil = new pencil(this); addmouselistener(pencil); addmousemotionlistener(pencil); flagpaint = true; flagshape = false; } public plansa(){ this.setsize(800, 600); init(); } //... @override public void paintcomponent(graphics g) { g.setcolor(currentcolor); graphics2d g2d = (graphics2d) g; g2d.setstroke(setbrush(size,type)); super.paintcomponent(g); switch(shape){ default: break; case "freeline":{ g.drawline(xdragged, ydragged, xcurrent, ycurrent); break; } case "rectangle":{ if(flagshape == true){ g.drawrect(xpressed, ypressed, math.max(xcurrent-xpressed,xpressed-xcurrent), math.max(ycurrent-ypressed,ypressed-ycurrent)); if(fill == true) g.fillrect(xpressed, ypressed, math.max(xcurrent-xpressed,xpressed-xcurrent), math.max(ycurrent-ypressed,ypressed-ycurrent)); } break; } case "circle":{ if(flagshape == true){ int radius = (int)math.sqrt(math.max(xcurrent-xpressed,xpressed-xcurrent)*math.max(xcurrent-xpressed,xpressed-xcurrent)+math.max(ycurrent-ypressed,ypressed-ycurrent)*math.max(ycurrent-ypressed,ypressed-ycurrent)); g.drawoval(xpressed, ypressed, radius, radius); if(fill == true) g.filloval(xpressed, ypressed, radius, radius); } break; } case "oval":{ if(flagshape == true){ g.drawoval(xpressed, ypressed, math.max(xcurrent-xpressed,xpressed-xcurrent), math.max(ycurrent-ypressed,ypressed-ycurrent)); if(fill == true) g.filloval(xpressed, ypressed, math.max(xcurrent-xpressed,xpressed-xcurrent), math.max(ycurrent-ypressed,ypressed-ycurrent)); } break; } case "line":{ if(flagshape == true){ g.drawline(xpressed, ypressed, xcurrent, ycurrent); } break; } } } //... }
my problem every time call paintcomponent() method, jpanel clears , item remains 1 drawn. there way avoid this?
store objects in arraylist
, iterate through when drawing.
you have them implement custom interface, drawable
example, store in arraylist<drawable>
.
Comments
Post a Comment