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

javascript - three.js lot of meshes optimization -

smartface.io - Proper way to change color scheme for whole application -

Email notification in google apps script -