Can't get change listener to work with slider










0















I was supposed to make a screen saver application that draws a certain amount of random lines every five seconds, and we were supposed to include a UI to allow the user to input the amount of lines they want drawn. I figured a slider would be easiest but I can't get the slider to change the value of the lines variable that is used in the loop that draws the lines. The value is stuck at 250 no matter where i put the slider



public class Q4 extends JComponent implements ActionListener, ChangeListener{
private Random rand=new Random();
private Timer time=new Timer(5000,this);
private int lines;
JSlider line=new JSlider(0,500);;
public Q4()
JFrame frame=new JFrame();
frame.setSize(1080,720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(this);
JSlider line=new JSlider(0,500);
line.addChangeListener(this);
line.setMajorTickSpacing(50);
line.setMinorTickSpacing(25);
line.setPaintTicks(true);
line.setPaintLabels(true);
frame.add(line, BorderLayout.NORTH);


@Override
public void paintComponent(Graphics g)
time.start();
int x=0;
while(x<lines)
int x1=rand.nextInt(getWidth())+1;
int y1=rand.nextInt(getHeight())+1;
int x2=rand.nextInt(getWidth())+1;
int y2=rand.nextInt(getHeight())+1;
g.drawLine(x1, y1, x2, y2);
x++;

System.out.println(lines);


@Override
public void actionPerformed(ActionEvent e)
repaint();

@Override
public void stateChanged(ChangeEvent e)
lines=line.getValue();










share|improve this question


























    0















    I was supposed to make a screen saver application that draws a certain amount of random lines every five seconds, and we were supposed to include a UI to allow the user to input the amount of lines they want drawn. I figured a slider would be easiest but I can't get the slider to change the value of the lines variable that is used in the loop that draws the lines. The value is stuck at 250 no matter where i put the slider



    public class Q4 extends JComponent implements ActionListener, ChangeListener{
    private Random rand=new Random();
    private Timer time=new Timer(5000,this);
    private int lines;
    JSlider line=new JSlider(0,500);;
    public Q4()
    JFrame frame=new JFrame();
    frame.setSize(1080,720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.add(this);
    JSlider line=new JSlider(0,500);
    line.addChangeListener(this);
    line.setMajorTickSpacing(50);
    line.setMinorTickSpacing(25);
    line.setPaintTicks(true);
    line.setPaintLabels(true);
    frame.add(line, BorderLayout.NORTH);


    @Override
    public void paintComponent(Graphics g)
    time.start();
    int x=0;
    while(x<lines)
    int x1=rand.nextInt(getWidth())+1;
    int y1=rand.nextInt(getHeight())+1;
    int x2=rand.nextInt(getWidth())+1;
    int y2=rand.nextInt(getHeight())+1;
    g.drawLine(x1, y1, x2, y2);
    x++;

    System.out.println(lines);


    @Override
    public void actionPerformed(ActionEvent e)
    repaint();

    @Override
    public void stateChanged(ChangeEvent e)
    lines=line.getValue();










    share|improve this question
























      0












      0








      0








      I was supposed to make a screen saver application that draws a certain amount of random lines every five seconds, and we were supposed to include a UI to allow the user to input the amount of lines they want drawn. I figured a slider would be easiest but I can't get the slider to change the value of the lines variable that is used in the loop that draws the lines. The value is stuck at 250 no matter where i put the slider



      public class Q4 extends JComponent implements ActionListener, ChangeListener{
      private Random rand=new Random();
      private Timer time=new Timer(5000,this);
      private int lines;
      JSlider line=new JSlider(0,500);;
      public Q4()
      JFrame frame=new JFrame();
      frame.setSize(1080,720);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.add(this);
      JSlider line=new JSlider(0,500);
      line.addChangeListener(this);
      line.setMajorTickSpacing(50);
      line.setMinorTickSpacing(25);
      line.setPaintTicks(true);
      line.setPaintLabels(true);
      frame.add(line, BorderLayout.NORTH);


      @Override
      public void paintComponent(Graphics g)
      time.start();
      int x=0;
      while(x<lines)
      int x1=rand.nextInt(getWidth())+1;
      int y1=rand.nextInt(getHeight())+1;
      int x2=rand.nextInt(getWidth())+1;
      int y2=rand.nextInt(getHeight())+1;
      g.drawLine(x1, y1, x2, y2);
      x++;

      System.out.println(lines);


      @Override
      public void actionPerformed(ActionEvent e)
      repaint();

      @Override
      public void stateChanged(ChangeEvent e)
      lines=line.getValue();










      share|improve this question














      I was supposed to make a screen saver application that draws a certain amount of random lines every five seconds, and we were supposed to include a UI to allow the user to input the amount of lines they want drawn. I figured a slider would be easiest but I can't get the slider to change the value of the lines variable that is used in the loop that draws the lines. The value is stuck at 250 no matter where i put the slider



      public class Q4 extends JComponent implements ActionListener, ChangeListener{
      private Random rand=new Random();
      private Timer time=new Timer(5000,this);
      private int lines;
      JSlider line=new JSlider(0,500);;
      public Q4()
      JFrame frame=new JFrame();
      frame.setSize(1080,720);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.add(this);
      JSlider line=new JSlider(0,500);
      line.addChangeListener(this);
      line.setMajorTickSpacing(50);
      line.setMinorTickSpacing(25);
      line.setPaintTicks(true);
      line.setPaintLabels(true);
      frame.add(line, BorderLayout.NORTH);


      @Override
      public void paintComponent(Graphics g)
      time.start();
      int x=0;
      while(x<lines)
      int x1=rand.nextInt(getWidth())+1;
      int y1=rand.nextInt(getHeight())+1;
      int x2=rand.nextInt(getWidth())+1;
      int y2=rand.nextInt(getHeight())+1;
      g.drawLine(x1, y1, x2, y2);
      x++;

      System.out.println(lines);


      @Override
      public void actionPerformed(ActionEvent e)
      repaint();

      @Override
      public void stateChanged(ChangeEvent e)
      lines=line.getValue();







      java graphics slider changelistener






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 16:44









      Joe ButlerJoe Butler

      306




      306






















          1 Answer
          1






          active

          oldest

          votes


















          0














          private int lines;
          JSlider line=new JSlider(0,500);;
          public Q4(){
          ...
          JSlider line=new JSlider(0,500);


          The problem is you have two sliders, once instance variable and one local variable. You add the listener to the local variable, but your listener can only access the instance variable.



          Get rid of the local variable.



          Other problems:



          1. Don't start a Timer in a painting method. A painting method is for painting only. The Timer should be started in the constructor


          2. The setVisible( true ) should be invoked AFTER all the components have been added to the panel


          3. Your class should NOT be create the frame. Your custom component is a stand alone component and creating the frame in the constructor has nothing to do with your component. The frame should be create in the public static void main() method that you use to test the code.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324144%2fcant-get-change-listener-to-work-with-slider%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            private int lines;
            JSlider line=new JSlider(0,500);;
            public Q4(){
            ...
            JSlider line=new JSlider(0,500);


            The problem is you have two sliders, once instance variable and one local variable. You add the listener to the local variable, but your listener can only access the instance variable.



            Get rid of the local variable.



            Other problems:



            1. Don't start a Timer in a painting method. A painting method is for painting only. The Timer should be started in the constructor


            2. The setVisible( true ) should be invoked AFTER all the components have been added to the panel


            3. Your class should NOT be create the frame. Your custom component is a stand alone component and creating the frame in the constructor has nothing to do with your component. The frame should be create in the public static void main() method that you use to test the code.






            share|improve this answer



























              0














              private int lines;
              JSlider line=new JSlider(0,500);;
              public Q4(){
              ...
              JSlider line=new JSlider(0,500);


              The problem is you have two sliders, once instance variable and one local variable. You add the listener to the local variable, but your listener can only access the instance variable.



              Get rid of the local variable.



              Other problems:



              1. Don't start a Timer in a painting method. A painting method is for painting only. The Timer should be started in the constructor


              2. The setVisible( true ) should be invoked AFTER all the components have been added to the panel


              3. Your class should NOT be create the frame. Your custom component is a stand alone component and creating the frame in the constructor has nothing to do with your component. The frame should be create in the public static void main() method that you use to test the code.






              share|improve this answer

























                0












                0








                0







                private int lines;
                JSlider line=new JSlider(0,500);;
                public Q4(){
                ...
                JSlider line=new JSlider(0,500);


                The problem is you have two sliders, once instance variable and one local variable. You add the listener to the local variable, but your listener can only access the instance variable.



                Get rid of the local variable.



                Other problems:



                1. Don't start a Timer in a painting method. A painting method is for painting only. The Timer should be started in the constructor


                2. The setVisible( true ) should be invoked AFTER all the components have been added to the panel


                3. Your class should NOT be create the frame. Your custom component is a stand alone component and creating the frame in the constructor has nothing to do with your component. The frame should be create in the public static void main() method that you use to test the code.






                share|improve this answer













                private int lines;
                JSlider line=new JSlider(0,500);;
                public Q4(){
                ...
                JSlider line=new JSlider(0,500);


                The problem is you have two sliders, once instance variable and one local variable. You add the listener to the local variable, but your listener can only access the instance variable.



                Get rid of the local variable.



                Other problems:



                1. Don't start a Timer in a painting method. A painting method is for painting only. The Timer should be started in the constructor


                2. The setVisible( true ) should be invoked AFTER all the components have been added to the panel


                3. Your class should NOT be create the frame. Your custom component is a stand alone component and creating the frame in the constructor has nothing to do with your component. The frame should be create in the public static void main() method that you use to test the code.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 16:56









                camickrcamickr

                276k16127239




                276k16127239





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324144%2fcant-get-change-listener-to-work-with-slider%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線