Selasa, 14 Oktober 2014

Materi Grafika


Cara Membuat Bola Bergerak Pada NETBEANS


Disini kita menggunakan 3 Class, yaitu 
  1.  class Ball
  2.  class BallControl 
  3.  class BounceBallApp 
1. Kode pada class Ball yaitu:

package PR_Ball;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 *
 * @author DELFIKE
 */
public class Ball extends JPanel {
    private int delay = 50;
// Create a timer with delay 1000 ms
   protected Timer timer = new Timer(delay, new TimerListener());

   public int x = 0; private int y = 0; // Current ball position
   private int radius = 5; // Ball radius
   private int dx = 2; // Increment on ball's x-coordinate
   private int dy = 2; // Increment on ball's y-coordinate

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

   private class TimerListener implements ActionListener {
     /** Handle the action event */
     public void actionPerformed(ActionEvent e) {
       repaint();

     }
   }
    @Override
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);

     g.setColor(Color.red);

     // Check boundaries
     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);

     // Adjust ball position
        x += dx;
        y += dy;
        g.fillOval(x - radius, y - radius, radius * 2, radius * 2);

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


}

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

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

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

}


 2.  Kode pada class BallControl yaitu:

package PR_Ball;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 *
 * @author DELFIKE
 */
public class BallControl extends JPanel {
    private Ball ball = new Ball();
    private JButton jbtStop = new JButton("Stop");
    private JButton jbtResume = new JButton("Lanjut");
    private JScrollBar jsbDelay = new JScrollBar();

   public BallControl() {
     // Group buttons in a panel
     JPanel panel = new JPanel();
     panel.add(jbtStop);
     panel.add(jbtResume);
     // Add ball and buttons to the panel
     ball.setBorder(new javax.swing.border.LineBorder(Color.red));
     //raga.setBorder(new javax.swing.border.LineBorder(Color.yellow));
     jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
     ball.setDelay(jsbDelay.getMaximum());
     setLayout(new BorderLayout());
     add(jsbDelay, BorderLayout.NORTH);
     add(ball, BorderLayout.CENTER);
     add(panel, BorderLayout.SOUTH);

     // Register listeners
     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());
       }
     });
   }

}


 3.  Kode pada class BounceBallApp yaitu:

package PR_Ball;

import javax.swing.*;

/**
 *
 * @author DELFIKE
 */
public class BounceBallApp extends JApplet {
 public BounceBallApp() {
     add(new BallControl());
 }
}

Hasilnya seperti gambar dibawah ini



Sekian :) 

 

Tidak ada komentar:

Posting Komentar