Aplikasi Bouncing Ball Menggunakan Bahas

Pembuatan Aplikasi Bouncing Ball
Menggunakan Bahasa Pemrograman
JAVA

Oleh :
NIM

: 120030027

NAMA

: Christian Iswahyudi

KELAS

: AI133

MATA KULIAH

: Pemrograman Berorientasi Obyek II


PROGRAM STUDI

: Sistem Informasi

SEKOLAH TINGGI
MANAJEMEN INFORMATIKA DAN TEKNIK KOMPUTER
(STMIK) STIKOM BALI
2015

Penjelasan
Aplikasi bouncing ball adalah aplikasi java yang bersifat sederhana yang menggunakan GUI
dimana pada aplikasi yang dibangun ini, terdapat beberapa bola dengan warna yang
berbeda dapat memantul di dalam windows form. Jika windows form diperlebar, maka
pantulan bola akan mengikuti luas dari windows form tersebut. Aplikasi sederhana ini dibuat
menggunakan bahasa pemrograman JAVA.

Tampilan

Source Code
import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class J2dObjekBola extends JPanel implements ActionListener {
static class Ball {
Color color;
float radius;
float x, y;
int dirx, diry;
JPanel container;
Ball(String ballcolors, float x, float y, float radius, JPanel cont) {
this.x = x;

this.y = y;
this.radius = radius;
this.container = cont;
this.dirx = (int) (Math.random() * 3);
this.diry = (int) (Math.random() * 3);
if(ballcolors == "pink") {
color = Color.pink;
}
if(ballcolors == "blue") {
color = Color.blue;
}
if(ballcolors == "yellow") {
color = Color.yellow;

}
if(ballcolors == "green") {
color = Color.green;
}
if(ballcolors == "white") {
color = Color.white;

}
}
public void clear(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D e = new Ellipse2D.Double(this.x, this.y,
this.radius, this.radius);
g2.setColor(this.container.getBackground());
g2.fill(e);
}
public void paint(Graphics gr) {
Graphics2D g2 = (Graphics2D) gr;
Ellipse2D e = new Ellipse2D.Double(this.x, this.y,
this.radius, this.radius);
g2.setColor(color);
g2.fill(e);
}
public void paintBlink(Graphics gr) {
Graphics2D g2 = (Graphics2D) gr;
Ellipse2D e = new Ellipse2D.Double(this.x, this.y,
this.radius, this.radius);

Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color ranColor = new Color(r, g, b);
g2.setColor(ranColor);
g2.fill(e);
}

public void tick() {
if (this.x + dirx >= this.container.getWidth()) {
dirx *= -1;
}
if (this.x + dirx = this.container.getHeight()) {
diry *= -1;
}
if (this.y + diry = this.container.getWidth()) {
dirx *= -1;
}
if (this.x + dirx = this.container.getWidth()) {

dirx *= -1;
}
this.x += dirx;
this.y += diry;
}
}
private List balls;
private Timer animation;
public J2dObjekBola() {
balls = new LinkedList();
balls.add(new Ball ("yellow",5,10,20,this));
balls.add(new Ball ("green",10,20,30,this));
balls.add(new Ball ("pink",15,30,40,this));
balls.add(new Ball ("blue",20,40,50,this));
balls.add(new Ball ("white",17,30,20,this));
this.animation = new Timer(8, this);
this.animation.start();

}
@Override

public void paint(Graphics g) {
for (Ball b : this.balls) {
b.clear(g);
b.tick();
b.paint(g);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
}
public static void main(String[] args) {
JFrame f = new JFrame("120030027_CHRISTIAN ISWAHYUDI");
f.setSize(300,300);
J2dObjekBola b = new J2dObjekBola();
f.add(b);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}