Hi all,

following is the code for a simple but pleasant animation (3000 colourful circles with random sizes etc. bouncing off edges of the app window)

What I have problem with is getting the "coef" to be automatically updated on "jSlider1" change in order to control the animation speed

Also struggling to get the "GO!" button to restart the whole animation...

Both problematic areas marked in the following code with "******" column

import java.awt.Color;
import java.awt.FlowLayout;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Kolos003 extends javax.swing.JFrame
{

    public Kolos003()
    {
        initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {

        jPanel1 = new MyPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jSlider1 = new javax.swing.JSlider();
        jProgressBar1 = new javax.swing.JProgressBar();
        jToggleButton1_GO = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(new javax.swing.border.MatteBorder(null));

        jToggleButton1.setText("jToggleButton1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToggleButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToggleButton1)
                .addContainerGap(384, Short.MAX_VALUE))
        );

        jSlider1.setMajorTickSpacing(10);
        jSlider1.setMinorTickSpacing(5);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setToolTipText("");
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener()
        {
            public void stateChanged(javax.swing.event.ChangeEvent evt)
            {
                jSlider1StateChanged(evt);
            }
        });

        jProgressBar1.setToolTipText("");
        jProgressBar1.setValue(50);
        jProgressBar1.setStringPainted(true);

        jToggleButton1_GO.setText("GO!");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jToggleButton1_GO)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 323, Short.MAX_VALUE)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButton1_GO))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt)                                      
    {                                          
        // TODO add your handling code here:

        System.out.println("jSlider1: " + jSlider1.getValue());
        jProgressBar1.setValue(jSlider1.getValue());

        /*
         * 
         * 
         * This is the jSlider that I am trying to pass value from...
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         *  
         */

    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(Kolos003.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(Kolos003.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(Kolos003.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(Kolos003.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Kolos003().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    public javax.swing.JSlider jSlider1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton1_GO;
    // End of variables declaration
}



class CircleFull
{

    float pos_x = 100, pos_y = 100;
    int siz_x = 20, siz_y = 20;
    float vel_x, vel_y;
    int sig_x = 1, sig_y = 1;
    float MyRandColor_r = 1, MyRandColor_g = 0, MyRandColor_b = 1;
    Color MyRandColor;
    int coef = 1 * 1;

    /*
     * 
     * 
     * "int coef" is the thing i am trying take value from "jSlider1"
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     *  
     */
    public CircleFull()
    {
        Random MyRandNum = new Random();;


        pos_x = MyRandNum.nextInt(300);
        pos_y = MyRandNum.nextInt(300);

        if (MyRandNum.nextInt(3) == 0)
        {
            siz_x = MyRandNum.nextInt(30) + 2;
            siz_y = siz_x;
        } else
        {
            siz_x = MyRandNum.nextInt(20) + 2;
            siz_y = siz_x;
        }
        vel_x = (float) 0.01 * (50 + MyRandNum.nextInt(100)); ///(float)0.1 ==== 0.1f
        vel_y = (float) 0.01 * (50 + MyRandNum.nextInt(100));
        sig_x = MyRandNum.nextInt(200) - 100;
        sig_y = MyRandNum.nextInt(200) - 100;

        if (MyRandNum.nextInt(5) == 0)
        {
            MyRandColor_r = (float) 0.01 * (0 + MyRandNum.nextInt(20));
            MyRandColor_g = (float) 0.01 * (50 + MyRandNum.nextInt(50));
            MyRandColor_b = (float) 0.01 * (50 + MyRandNum.nextInt(10));
        } else
        {
            MyRandColor_r = (float) 0.01 * (50 + MyRandNum.nextInt(50));
            MyRandColor_g = (float) 0.01 * (0 + MyRandNum.nextInt(20));
            MyRandColor_b = (float) 0.01 * (50 + MyRandNum.nextInt(10));
        }

        MyRandColor = new Color(MyRandColor_r, MyRandColor_g, MyRandColor_b);

        System.out.println("pos_x: " + pos_x);
        System.out.println("pos_y: " + pos_y);
        System.out.println("vel_x: " + vel_x);
        System.out.println("vel_y: " + vel_y);

    }

    public void moveCircle(int border_x, int border_y)
    {


        if (sig_x > 0)
        {
            pos_x += vel_x * coef;
        } else
        {
            pos_x -= vel_x * coef;
        }
        if (pos_x > border_x - siz_x)
        {
            sig_x = -1;
        }
        if (pos_x < 0)
        {
            sig_x = 1;
        }

        ///--------------------------------///

        if (sig_y > 0)
        {
            pos_y += vel_y * coef;
        } else
        {
            pos_y -= vel_y * coef;
        }
        if (pos_y > border_y - siz_y)
        {
            sig_y = -1;
        }

        if (pos_y < 0)
        {
            sig_y = 1;
        }
    }
}


class CircleEmpty extends CircleFull
{
}

class MyPanel extends javax.swing.JPanel
{

    CircleFull[] cf;
    CircleEmpty[] ce;

    public MyPanel()
    {
        cf = new CircleFull[1500];
        ce = new CircleEmpty[1500];
        for (int i = 0; i < cf.length; i++)
        {
            cf[i] = new CircleFull();
        }
        for (int i = 0; i < ce.length; i++)
        {
            ce[i] = new CircleEmpty();
        }
    }

    @Override ///można bez tego ale wtedy jest na żółto 
    public void paintComponent(java.awt.Graphics g) /// funkcja publiczna istnieje jeuż wcześniej
    {

        g.clearRect(0, 0, this.getWidth(), this.getHeight());

        for (int i = 0; i < cf.length; i++)
        {
            g.setColor(cf[i].MyRandColor);
            g.fillOval((int) cf[i].pos_x, (int) cf[i].pos_y, cf[i].siz_x, cf[i].siz_y); /// (x, y, w, h)
            cf[i].moveCircle(this.getWidth(), this.getHeight());
        }
        for (int i = 0; i < ce.length; i++)
        {
            g.setColor(ce[i].MyRandColor);
            g.drawOval((int) ce[i].pos_x, (int) ce[i].pos_y, ce[i].siz_x, ce[i].siz_y); /// (x, y, w, h)
            ce[i].moveCircle(this.getWidth(), this.getHeight());
        }

        g.setColor(Color.black);

        for (int i = 0; i < 21; i++)
        {
            g.drawLine(0, 20 * i, 400, 20 * i);
            g.drawLine(20 * i, 0, 20 * i, 400);
        }


        repaint();


    }
}

have to testing with if (jSlider1.getValueIsAdjusting()) { inside ChangeListener

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.