One Page Revision – cfad

package cfad;

//lowercase projectname
//import j2_c4d.myBall2.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.Timer;

class myBall2 extends JComponent {

    private final int mradius;
    private int mxpos;
    private int mypos;
    private int mspeed = 1;
    private int mxdir = 1;
    private int mydir = 1;
    private Color mcolor;
    private int mcvwidth;
    private int mcvheight;
    private int mrise;
    private int mrun;

    private Timer bTimer;

    public myBall2(int radius, int xpos, int ypos,
        int speed, int rise, int run, int cvwidth, int cvheight, Color xColor) {

        this.mradius = radius;
        this.mxpos = xpos;
        this.mypos = ypos;
        this.mspeed = speed;
        this.mcolor = xColor;
        this.mcvwidth = cvwidth / 2;
        this.mcvheight = cvheight / 2;
        this.mrise = rise;
        this.mrun = run;
        this.setSize(mcvwidth, mcvheight);

        btime();
        bTimer.start();
    }

    public void paintComponent(Graphics g) {
        //super.paintComponent(g); 
        g.setColor(mcolor);
        g.fillOval(mxpos, mypos, mradius, mradius);
    }

    private void btime() {
        bTimer = new Timer(mspeed,
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {

                        if (mxpos > mcvwidth) {
                            mxdir = mxdir * -1;
                            mxpos = mcvwidth - 1;
                        } else if (mxpos < -1) {
                            mxdir = mxdir * -1;
                            mxpos = 0;
                        }

                        if (mypos > mcvheight) {
                            mydir = mydir * -1;
                            mypos = mcvheight;

                        } else if (mypos < -1) {
                            mydir = mydir * -1;
                            mypos = 0;
                        }

                        mxpos = mxpos + (mxdir * mrun);
                        mypos = mypos + (mydir * mrise);
                        setLocation(mxpos, mypos);
                    }
                }
        );
    }
}

class BallFrame extends javax.swing.JFrame {

    public BallFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 661, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 438, Short.MAX_VALUE)
        );

        jButton1.setText("Add Ball");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(91, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(101, 101, 101))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addContainerGap(98, Short.MAX_VALUE))
        );

        pack();
    }//                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        myBall2 aball3 = new myBall2(15, 0, 25, 15, 2, 1,
                jPanel1.getWidth() - 15,
                jPanel1.getHeight() - 15,
                Color.green);

        aball3.setSize(jPanel1.getWidth() - 15, jPanel1.getHeight() - 15);
        aball3.setLocation(0, 0);
        jPanel1.add(aball3);

    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* 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(BallFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BallFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BallFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BallFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BallFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}

public class Cfad {

    public static void main(String[] args) {
        // TODO code application logic here

        BallFrame xxxx = new BallFrame();
        xxxx.setVisible(true);
    }

}