Code Class BallControl :
package hello.BonceOut;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class BallControl extends JPanel {
private Ball ball = new Ball();
private JButton jbtSuspend = new JButton("Stop");
private JButton jbtResume = new JButton("Lanjutkan");
private JButton jbtAdd = new JButton("+1");
private JButton jbtDel = new JButton("-1");
private JScrollBar jsbDelay = new JScrollBar();
public BallControl() {
// Group buttons in a panel
JPanel panel = new JPanel();
panel.add(jbtSuspend);
panel.add(jbtResume);
panel.add(jbtAdd);
panel.add(jbtDel);
// Add ball and buttons to the panel
ball.setBorder(new javax.swing.border.LineBorder(Color.red));
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
jbtSuspend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ball.Stop();
}
});
jbtResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ball.Lanjutkan();
}
});
jsbDelay.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
}
});
}
}
Code Class Ball :
package hello.BonceOut;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ball extends JPanel {
private int delay = 10;
// Create a timer with delay 1000 ms
protected Timer timer = new Timer(delay, new TimerListener());
private int x = 0;
private int y = 0;
private int t = 0;
private int r = 0;
private int l = 0;
private int c = 0;
private int radius = 5;
private int dx = 10;
private int dy = 10;
private int dr = 15;
private int dl = 15;
private int dt = 20;
private int dc = 20;
public Ball() {
timer.start();
}
void addBall() {
throw new UnsupportedOperationException("Tambah Bola 1");
}
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);
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 * 2, radius * 2);
Polygon anu = new Polygon();
anu.addPoint(0, 0);
anu.addPoint(x - radius, y - radius);
g.drawPolygon(anu);
}
public void Stop() {
timer.stop(); // Suspend timer
}
public void Lanjutkan() {
timer.start(); // Resume timer
}
public void setDelay(int delay) {
this.delay = delay;
timer.setDelay(delay);
}
}
Code Class BounceBallApp :
package hello.BonceOut;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class BounceBallApp extends JApplet {
public BounceBallApp() {
add(new BallControl());
}
}
HASIL :
->Gabriel Daty
Terimakasih telah berkunjung ke blog saya semoga bermanfaat :D