Tuesday, 15 March 2011

java - Paint Like JComponent Drawing Pad -



java - Paint Like JComponent Drawing Pad -

i have class pad_draw extending jcomponent. constructor

public pad_draw() { this.setdoublebuffered(true); this.setlayout(null); };

the paintcomponent methpod :

public void paintcomponent( graphics g ) { graphics2d = (graphics2d)g; graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); graphics2d.setpaint(color.white); graphics2d.fillrect(0, 0, getsize().width, getsize().height); }

in jframe adding scrollpane:

jscrollpane padscroller = new jscrollpane(); padscroller.setwheelscrollingenabled(true); padscroller.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always); padscroller.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_always); padscroller.setpreferredsize(new dimension(200, 100)); padscroller.setminimumsize(new dimension(200, 100)); padscroller.setmaximumsize(new dimension(200, 100)); padscroller.setviewportview(drawpad); content.add(padscroller, borderlayout.center);

but add together content in frame , set size of frame. drawing pad taking whole size whatever needs.

i want specified size (200,100) maintained , want windows paint application has. should able increment size extending corner. extend corner scrollbar gets activated. can give me thought how accomplish it?

the default layout manager jframe, borderlayout not respect preferred sizes of components.

you layout manager does, such boxlayout, , override getpreferredsize in padscroller component:

jscrollpane padscroller = new jscrollpane() { @override public dimension getpreferredsize() { homecoming new dimension(200, 100); } };

regarding increasing size, have @ using mouseadapter , updating property in mousedragged method.

java layout paint jcomponent

No comments:

Post a Comment