swing - JSlider changeListener won't update - Java -


i have fractal tree generator , trying make slider control number of iterations, can not work. also, layout gets messed whenever repaint() method called. thoughts on how fix this?

public class fractaltree extends jpanel implements changelistener {      static jslider slider = new jslider(0,12);      static  int slideval=7;     public fractaltree()     {         super();         slider.addchangelistener(this);     }      public void paint(graphics g)     {         g.setcolor(color.green);         drawtree(g, 400, 750, 200, math.toradians(-90), math.toradians(45), slideval); //don't let # of iterations exceed 12, useless     }      private void drawtree(graphics g, int x1, int y1, double l, double t, double dt, double iterations) {         if (iterations > 0) {             int x2 = x1 + (int) (l * math.cos(t));             int y2 = y1 + (int) (l * math.sin(t));             g.drawline(x1, y1, x2, y2);             drawtree(g, x2, y2, l / 1.5, t + dt, math.pi / 4, iterations - .5);             drawtree(g, x2, y2, l / 1.5, t - dt, math.pi / 4, iterations - .5);         }     }      @override     public void statechanged(changeevent e) {         slideval=slider.getvalue();         repaint();     }      public static void main(string[] args) {         jframe t = new jframe("some swaggy fractal shit");         fractaltree tree = new fractaltree();         slider.setvalue(slideval);         slider.setminortickspacing(1);         slider.setpaintticks(true);         slider.setpaintlabels(true);          tree.add(slider);         t.add(tree);         t.setdefaultcloseoperation(windowconstants.exit_on_close);         t.setresizable(false);         t.setlocationbyplatform(true);         t.setsize(800, 800);         t.setbackground(color.black);         t.setvisible(true);     } } 

two main problems:

  1. you're overriding paint instead of paintcomponent.
  2. you're not calling super.paintcomponent(g) (or in case, super.paint(g)) first thing in overridden method.

this need have:

@override public void paintcomponent(graphics g) {      super.paintcomponent(g);     g.setcolor(color.green);     drawtree(g, 400, 750, 200, math.toradians(-90), math.toradians(45), slideval); } 

other things consider:

  • add slider frame on position borderlayout.page_start instead of panel. if add panel, risk drawing slider is.
  • set background color on panel , not on frame.
  • no need call super() in constructor, it's automatic.
  • setresizable(false) on frame should avoided. no need restrict user's space.
  • call pack() on frame instead of setsize(...). latter dependent on local graphics configuration.
    • you need override panel's getpreferredsize method return correct size drawing.
  • your pixel calculation should adjusted tree aligned upper left corner of panel, , not start arbitrary location on bottom, causes lose lot of screen real estate:

enter image description here

response comments

why paintcomponent should used?

see these:

public void paint(graphics g)

this method delegates work of painting 3 protected methods: paintcomponent, paintborder, , paintchildren. they're called in order listed ensure children appear on top of component itself. [...] subclass wants specialize ui (look , feel) delegate's paint method should override paintcomponent.

you saw if override paint , added slider panel, got issues slider painting because ignored paintchildren.

what calling superclass constructor does?

best answer jls:

jls 8.8.7. constructor body

if constructor body not begin explicit constructor invocation , constructor being declared not part of primordial class object, constructor body implicitly begins superclass constructor invocation "super();", invocation of constructor of direct superclass takes no arguments.

so calling super() nothing.


Comments