Creating custom components in Java AWT -
i trying create custom component using java awt or swing rectangle number of components inside of it, including other rectangles. this:
╔══════╗ ║ ┌┐ ║ ║ ├┘ ║ ║ ║ ╚══════╝
and needs component can preferably draw 1 instruction. myframe.add(new mycomponent())
.
what best way this? there way can using rectangle
, or should go jpanel
or swing?
i recomend extending jpanel
, overriding it's paintcomponent()
method. see another answer of mine on that.
basically, when rectangle 'drawn' on panel, want save member of jpanel
. then, in paintcomponent
method, draw of rectangles have saved in jpanel
.
this how implement 'draw' method:
list<rectangle> recs; list<stroke> strokes; list<color> colors; public void drawrectangle(rectangle newr, stroke stroke, color c){ recs.add(newr); strokes.add(stroke); colors.add(c); }
and, paint component similar to:
protected void paintcomponent(graphics g){ super.paintcomponent(g); (int = 0; < recs.size(); ++) { g.setcolor(colors.get(i)); g.setstroke(strokes.get(i)); g.drawrectangle(recs); } }
Comments
Post a Comment