Senin, 13 Oktober 2014

Membuat Animasi Bola Menggunakan Netbeans

Untuk membuat animasi bola menggunakan Netbeans kita membutuhkan tiga Class yaitu : Class Ball, Class BallControl, dan Class BounceBallApp ...
Berikut adalah coding untuk masing-masing Class...

Coding untuk Class Ball :
 package Bola;

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.Timer;
import javax.swing.JPanel;

/**
 *
 * @author Natan
 */

public class Ball extends JPanel {
    private int delay = 50;

   protected Timer timer = new Timer(delay, new TimerListener());

   public int x = 0;
   public int y = 0;
   private int radius = 5;
   private int dx = 7;
   private int dy = 7;

   public int a = 0;
   public int b = 0;
   private int ca = 5;
   private int cb = 5;

   public Ball() {
     timer.start();
   }

   private class TimerListener implements ActionListener {
     public void actionPerformed(ActionEvent e) {
       repaint();

     }
   }
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);
//bola warna merah
     g.setColor(Color.red);

     if (x < radius) dx = Math.abs(dx);
     if (x > getWidth() - radius) dx = -Math.abs(dx);
     if (y < radius) dy = Math.abs(dy);
     if (y > getHeight() - radius) dy = -Math.abs(dy);

        x += dx;
        y += dy;
        g.fillOval(x - radius, y - radius, radius * 3, radius * 3);

       Polygon anu=new Polygon();
       anu.addPoint(0, 0);
       anu.addPoint(x - radius, y - radius);

       g.drawPolygon(anu);

       //bola warna biru
          g.setColor(Color.BLUE);

     if (a < radius) ca = Math.abs(ca);
     if (a > getWidth() - radius) ca = -Math.abs(ca);
     if (b < radius) cb = Math.abs(cb);
     if (b > getHeight() - radius) cb = -Math.abs(cb);

        a += ca;
        b += cb;
        g.fillOval(a - radius, b - radius, radius * 3, radius * 3);

       Polygon raga=new Polygon();
       raga.addPoint(0, 0);
       raga.addPoint(a - radius, b - radius);

       g.drawPolygon(raga);

}

public void suspend() {
timer.stop();
}

public void resume() {
timer.start();
}

public void setDelay(int delay) {
this.delay = delay;
timer.setDelay(delay);
   }
}

***********
Coding untuk Class BallControl :
 package Bola;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;

/**
 *
 * @author Natan
 */

public class BallControl extends JPanel {
    private Ball ball = new Ball();
    private JButton jbtStop = new JButton("Berhenti");
    private JButton jbtResume = new JButton("Lanjutkan");
    private JScrollBar jsbDelay = new JScrollBar();

   public BallControl() {

     JPanel panel = new JPanel();
     panel.add(jbtStop);
     panel.add(jbtResume);
     ball.setBorder(new javax.swing.border.LineBorder(Color.GREEN));
     jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
     ball.setDelay(jsbDelay.getMaximum());
     setLayout(new BorderLayout());
     add(jsbDelay, BorderLayout.NORTH);
     add(ball, BorderLayout.CENTER);
     add(panel, BorderLayout.SOUTH);

     jbtStop.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         ball.suspend();
       }
     });
     jbtResume.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         ball.resume();
       }
     });
     jsbDelay.addAdjustmentListener(new AdjustmentListener() {
       public void adjustmentValueChanged(AdjustmentEvent e) {
         ball.setDelay(jsbDelay.getMaximum() - e.getValue());
       }
     });
   }
}

****************
Coding untuk Class BounceBallApp :
 package Bola;

import javax.swing.JApplet;

/**
 *
 * @author Natan
 */

public class BounceBallApp extends JApplet {
    public BounceBallApp(){
        add(new BallControl());
    }
}

*****************

Setelah di jalankan hasilnya akan tampak seperti gambar berikut :



Selamat mencoba.... :)

0 komentar:

Posting Komentar