user interface - Java, Drawing on an JPanel with colors -


i coded program can insert coordinates , size of sqaure , draw on jpanel. used:

panel.getgraphics().drawrect(x,y,h,b); 

now want add color-chooser that:

color farbe = jcolorchooser.showdialog(null, "color-chooser", null); 

so how draw square color? tried doesn't work:

panel.getgraphics().drawrect(x,y,h,b, farbe); 

panel.getgraphics() not how custom painting should performed.

see painting in awt , swing , performing custom painting more details

start creating custom component extends jpanel, override it's paintcomponent method (and make sure call super.paintcomponent before custom painting).

place "drawing" logic within it. panel should provide setter , getter allows external classes change color of rectangle been draw, way, when paintcomponent called, can change color of graphics context accordingly.

also, take @ 2d graphics more details how painting done

don't forget call repaint when color changed encourage panel repainted


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -