Results 1 to 4 of 4
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default Use stop button to stop moving (stop timers) on JPanel

    Hello! I have a problem trying to add a stop button to my JAplet to make rectangle stop move by stoping it's timers. I have posted this on this forum: Use stop button to stop moving (stop timers) on JPanel (Swing / AWT / SWT / JFace forum at JavaRanch) but I didn't got a solution.

    Here is how I did:
    I have made many timers on my JPanel causing moving of rectangle in different directions. When one timer ends, the second starts and so on. I controlled that starting of timers by following the coordinates of the elements that draw rectangle.

    Now I have created a Stop button and I would like to stop the current timer when I click on it. I tried to write that in actionPerformed method but it only stops the first timer and ignores others; I wrote like this: first I wrote stopping of all timers, it didn't work, then I wrote, for example: if(SortPanel2.time1.isRunning()){SortPanel2.time1. stop();} to check is the timer running and to stop it then - I wrote this checking for all timers, but it doesn't work still.

    Here is an example:

    I initialized the variable: y2=80. Then I wrote:

    Java Code:
      if(y2<=230}{
    time2.start();
    g.drawRectangle(20, y2, 20, 20);}
    if(y2>230){time2.stop();}
    Code for Timer:
    Java Code:
                time2 = new Timer(50, new ActionListener() {
     public void actionPerformed(ActionEvent e) {
    y22 += delta;
    SortPanel2.this.repaint();
                }
            });
    I set delta=5.

    Variable y22 is one of the coordinates of rectangle and timer causes it move vertically from 80 to 230. After that, rectangle goes horizontally and for that I use Timer time3, which I start and stop again by making the conditions with x2 coordinate of the rectangle. Condition is: if(y2>230 and for x2...).

    I hope anybody can help. Greetings!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I wrote like this: first I wrote stopping of all timers, it didn't work, then I wrote, for example: if(SortPanel2.time1.isRunning()){SortPanel2.time1. stop();} to check is the timer running and to stop it then - I wrote this checking for all timers, but it doesn't work still.
    Sounds like a reasonable approach. If it doesn't work then you have a bug in your code and since we only have a few lines of code we have no idea where the problem might be.

    Post your Short, Self Contained, Correct Example (SSCCE) showing the problem.

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I tried to make it simplest I could. Here is the code for JApplet and its JPanel:
    Java Code:
    package org.me.sorting;
    
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.*;
    
    public class SortApplet extends JApplet implements ActionListener{
        static int s;
        int g;
       
        public void init() {
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        initComponents();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        @SuppressWarnings("unchecked")
        private void initComponents() {
    
            buttonGroup1 = new javax.swing.ButtonGroup();
            panel1 = new javax.swing.JPanel();
            jTabbedPane1 = new javax.swing.JTabbedPane();
            jPanel1 = new org.me.sorting.SortPanel();
            start = new javax.swing.JButton();
            stop = new javax.swing.JButton();
            restart = new javax.swing.JButton();
            label2 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jSeparator1 = new javax.swing.JSeparator();
            jSeparator2 = new javax.swing.JSeparator();
    
            panel1.setBackground(javax.swing.UIManager.getDefaults().getColor("nb_workplace_fill"));
    
            jTabbedPane1.setAlignmentX(1.0F);
            jTabbedPane1.setAlignmentY(1.0F);
    
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 483, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 241, Short.MAX_VALUE)
            );
    
            jTabbedPane1.addTab("Selection", null, jPanel1, "Odaberite metod sortiranja Selection sort");
            jPanel1.getAccessibleContext().setAccessibleName("");
            jPanel1.getAccessibleContext().setAccessibleDescription("");
            jPanel1.setBackground(new Color(176, 196, 222));
            jPanel1.setVerifyInputWhenFocusTarget(true);
    
            javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
            );
    
            jTabbedPane1.getAccessibleContext().setAccessibleName("none");
    
            start.setText("Start");
            start.setToolTipText("Pokrenite simulaciju klikom na dugme Start");
    
            stop.setText("Stop");
            stop.setToolTipText("Zaustavite simulaciju klikom na dugme Stop");
    
            restart.setText("Restart");
            restart.setToolTipText("Ponovo pokrenite simulaciju klikom na dugme Restert");
    
            label2.setText("Redosled sortiranja");
            label2.setToolTipText("Odabarite redosled sortiranja");
    
            buttonGroup1.add(jRadioButton1);
            jRadioButton1.setSelected(true);
            jRadioButton1.setText("Ascending (rastući)");
            jRadioButton1.setToolTipText("Rastući redosled sortiranja");
            jRadioButton1.setBorderPainted(true);
    
            buttonGroup1.add(jRadioButton2);
            jRadioButton2.setText("Descending (opadajući)");
            jRadioButton2.setToolTipText("Opadajući redosled sortiranja");
            jRadioButton2.setBorderPainted(true);
    
    
            jRadioButton1.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
            if(g==0){s=0;}
            }});
            jRadioButton2.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
            if(g==0){s=0;}
    			}});
    
            start.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
               
                if (jRadioButton1.isSelected()){
            if(g==0){
            s=1;
            SortPanel.time.start();
            }
    	}
            else if (jRadioButton2.isSelected()){
            if(g==0){s=2;
            SortPanel.time1d.start();}
    
            }}
    
            });
            stop.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e)
            {
            if(g==0){
            }
      }});
    
          jTabbedPane1.addChangeListener(new ChangeListener() {
          public void stateChanged(ChangeEvent e) {
            g = jTabbedPane1.getSelectedIndex();
    
            if (jRadioButton1.isSelected()){
            if(g==0){s=0;
            }
    		}
            else if (jRadioButton2.isSelected()){
            if(g==0){s=0;}
    		}
          }
        });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(label2)
                                )
                            .addGap(34, 34, 34)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jRadioButton2, javax.swing.GroupLayout.Alignment.LEADING))
                                .addComponent(jRadioButton1)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(start)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(stop)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(restart))))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(308, 308, 308)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(start)
                                .addComponent(stop)
                                .addComponent(restart))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                
    )))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(label2)
                        .addComponent(jRadioButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton2)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)))
                    .addGap(19, 19, 19))
            );
        }
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JSeparator jSeparator2;
        private javax.swing.JTabbedPane jTabbedPane1;
        public javax.swing.JLabel label2;
        public javax.swing.JPanel panel1;
        public javax.swing.JButton restart;
        public javax.swing.JButton start;
        public javax.swing.JButton stop;
    
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    Java Code:
    package org.me.sorting;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.Toolkit;
    
    public class SortPanel extends JPanel implements ActionListener{
      static Timer time, time1, time1d, time12d;
      int delta=5;
      static public int y1=230, x1=45, yx1=60, y12=230, x12=0, yx1d=40;
      Polygon poly11, poly12, poly13, poly21, poly22, poly23;
    
      public SortPanel() {
        try {
        } catch (Exception e) {
          e.printStackTrace();
        }
      time = new Timer(50, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
     y1 -= delta;
          SortPanel.this.repaint();
                }
            });
    
      time1d = new Timer(50, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
     y12 -= delta;
          SortPanel.this.repaint();
                }
            });
    
     time12d = new Timer(50, new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     x12 += delta;
          SortPanel.this.repaint();
                }
            });
     time1 = new Timer(50, new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     x1 += delta;
     SortPanel.this.repaint();
                }
            });
      }
        public void paintComponent(Graphics g) {
        super.paintComponent(g);
             Toolkit.getDefaultToolkit().sync();
        if(SortApplet.s==0){
    poly11 = new Polygon();
    poly11.addPoint(0, 230);
    poly11.addPoint(30, 230);
    poly11.addPoint(30, 200);
    poly11.addPoint(0, 200);
        g.setColor(Color.blue);
        g.fillPolygon(poly11);
        g.setColor(Color.black);
    g.drawPolygon(poly11);
    
    poly12 = new Polygon();
    poly12.addPoint(0, 200);
    poly12.addPoint(30, 200);
    poly12.addPoint(40, 190);
    poly12.addPoint(10, 190);
        g.setColor(Color.blue);
        g.fillPolygon(poly12);
        g.setColor(Color.black);
    g.drawPolygon(poly12);
    
    poly13 = new Polygon();
    poly13.addPoint(30, 230);
    poly13.addPoint(40, 220);
    poly13.addPoint(40, 190);
    poly13.addPoint(30, 200);
        g.setColor(Color.blue);
        g.fillPolygon(poly13);
        g.setColor(Color.black);
    g.drawPolygon(poly13);
    
    poly21 = new Polygon();
    poly21.addPoint(45, 230);
    poly21.addPoint(75, 230);
    poly21.addPoint(75, 180);
    poly21.addPoint(45, 180);
        g.setColor(Color.blue);
        g.fillPolygon(poly21);
        g.setColor(Color.black);
    g.drawPolygon(poly21);
    
    poly22 = new Polygon();
    poly22.addPoint(75, 230);
    poly22.addPoint(85, 220);
    poly22.addPoint(85, 170);
    poly22.addPoint(75, 180);
        g.setColor(Color.blue);
        g.fillPolygon(poly22);
        g.setColor(Color.black);
    g.drawPolygon(poly22);
    
    poly23 = new Polygon();
    poly23.addPoint(75, 180);
    poly23.addPoint(85, 170);
    poly23.addPoint(55, 170);
    poly23.addPoint(45, 180);
        g.setColor(Color.blue);
        g.fillPolygon(poly23);
        g.setColor(Color.black);
    g.drawPolygon(poly23);
    }
    
    if(SortApplet.s==1){
        g.setColor(Color.blue);
        g.fillPolygon(poly11);
        g.fillPolygon(poly12);
        g.fillPolygon(poly13);
        g.setColor(Color.black);
    
    poly11 = new Polygon();
    poly11.addPoint(0, 230);
    poly11.addPoint(30, 230);
    poly11.addPoint(30, 200);
    poly11.addPoint(0, 200);
    g.drawPolygon(poly11);
    
    poly12 = new Polygon();
    poly12.addPoint(0, 200);
    poly12.addPoint(30, 200);
    poly12.addPoint(40, 190);
    poly12.addPoint(10, 190);
    g.drawPolygon(poly12);
    
    poly13 = new Polygon();
    poly13.addPoint(30, 230);
    poly13.addPoint(40, 220);
    poly13.addPoint(40, 190);
    poly13.addPoint(30, 200);
    g.drawPolygon(poly13);
    
    if(y1>60){
    time.start();
    poly21 = new Polygon();
    poly21.addPoint(45, y1);
    poly21.addPoint(75, y1);
    poly21.addPoint(75, y1-50);
    poly21.addPoint(45, y1-50);
    g.drawPolygon(poly21);
    
    poly22 = new Polygon();
    poly22.addPoint(75, y1);
    poly22.addPoint(85, y1-10);
    poly22.addPoint(85, y1-60);
    poly22.addPoint(75, y1-50);
    g.drawPolygon(poly22);
    
    poly23 = new Polygon();
    poly23.addPoint(75, y1-50);
    poly23.addPoint(85, y1-60);
    poly23.addPoint(55, y1-60);
    poly23.addPoint(45, y1-50);
    g.drawPolygon(poly23);}
    
    if(y1<=60 && x1<=180){
    time1.start();
    poly21 = new Polygon();
    poly21.addPoint(x1, yx1);
    poly21.addPoint(x1+30, yx1);
    poly21.addPoint(x1+30, yx1-50);
    poly21.addPoint(x1, yx1-50);
    g.drawPolygon(poly21);
    
    poly22 = new Polygon();
    poly22.addPoint(x1+30, yx1);
    poly22.addPoint(x1+40, yx1-10);
    poly22.addPoint(x1+40, yx1-60);
    poly22.addPoint(x1+30, yx1-50);
    g.drawPolygon(poly22);
    
    poly23 = new Polygon();
    poly23.addPoint(x1+30, yx1-50);
    poly23.addPoint(x1+40, yx1-60);
    poly23.addPoint(x1+10, yx1-60);
    poly23.addPoint(x1, yx1-50);
    g.drawPolygon(poly23);}
    
        g.setColor(Color.blue);
        g.fillPolygon(poly21);
        g.fillPolygon(poly22);
        g.fillPolygon(poly23);
        g.setColor(Color.black);
    g.drawPolygon(poly21);
    g.drawPolygon(poly22);
    g.drawPolygon(poly23);
    }
             
    if(SortApplet.s==2){
    if(y12<=230 && y12>40){
        time1d.start();
    poly11 = new Polygon();
    poly11.addPoint(0, y12);
    poly11.addPoint(30, y12);
    poly11.addPoint(30, y12-30);
    poly11.addPoint(0, y12-30);
    g.drawPolygon(poly11);
    
    poly12 = new Polygon();
    poly12.addPoint(0, y12-30);
    poly12.addPoint(30, y12-30);
    poly12.addPoint(40, y12-40);
    poly12.addPoint(10, y12-40);
    g.drawPolygon(poly12);
    
    poly13 = new Polygon();
    poly13.addPoint(30, y12);
    poly13.addPoint(40, y12-10);
    poly13.addPoint(40, y12-40);
    poly13.addPoint(30, y12-30);
    g.drawPolygon(poly13);}
    
    if(y12<=40 && x12<=360){
        time12d.start();
     poly11 = new Polygon();
    poly11.addPoint(x12, yx1d);
    poly11.addPoint(x12+30, yx1d);
    poly11.addPoint(x12+30, yx1d-30);
    poly11.addPoint(x12, yx1d-30);
    g.drawPolygon(poly11);
    
    poly12 = new Polygon();
    poly12.addPoint(x12, yx1d-30);
    poly12.addPoint(x12+30, yx1d-30);
    poly12.addPoint(x12+40, yx1d-40);
    poly12.addPoint(x12+10, yx1d-40);
    g.drawPolygon(poly12);
    
    poly13 = new Polygon();
    poly13.addPoint(x12+30, yx1d);
    poly13.addPoint(x12+40, yx1d-10);
    poly13.addPoint(x12+40, yx1d-40);
    poly13.addPoint(x12+30, yx1d-30);
    g.drawPolygon(poly13);   }
    
        g.setColor(Color.blue);
        g.fillPolygon(poly11);
        g.fillPolygon(poly12);
        g.fillPolygon(poly13);
        g.setColor(Color.black);
        g.drawPolygon(poly11);
        g.drawPolygon(poly12);
        g.drawPolygon(poly13);
        g.setColor(Color.blue);
        g.fillPolygon(poly21);
        g.fillPolygon(poly22);
        g.fillPolygon(poly23);
        g.setColor(Color.black);
        g.drawPolygon(poly21);
        g.drawPolygon(poly22);
        g.drawPolygon(poly23);
    }
        }
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }}
    Try it, it works - by selecting radio button and Start button. Here is the explanation: I want to make simulation of sorting methods in programming. This is only part of my applet. As you can see, when the variable s is set to 0, two cuboids are only drawn. When we change s to 1 or 2 (clicking on radio buttons) elements move in different directions (simulating ascending and descending sorting). I want to make this elements stop when I press Stop button, start when I press Start and restart when I press Restart button. They are conditioned one by other: when I finish using timer time to draw vertical moving, I start using timer time1 to draw horizontal moving.

    About variable g: in my applet, I have more panels positioned as tabs and I control moving between them by using variable g. When the g is set to 0, the panel which I show you is selected.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Have you tried debugging your code by adding println() statements to show how variables change and how control flows?

Similar Threads

  1. sleep() and stop()
    By arefeh in forum New To Java
    Replies: 11
    Last Post: 01-01-2010, 02:10 PM
  2. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  3. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 08:44 PM
  4. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM
  5. stop button in the browser
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •