เป็นการเขียนแบบ application
คำสั่งสำคัญที่ใช้คือ

1. ส่วนของการกำหนดเงื่อนไขเริ่มต้นเหตุการณ์
ค่า 20 หมายถึงค่าในหน่วย millisecond ที่จะสั่งให้โปรแกรมเข้าไปคำนวณการเด้งของลูกบอลใน actionlistenner กล่าวอีกอย่างคือเป็นการระบุความเร็วในการเด้งของลูกบอลนั้นเอง
[src]
Timer t;
t = new Timer(20, this); //call
[/src]

2. หลายคนที่เจอปัญหาเกี่ยวกับการวาดแล้วภาพซ้อนๆกัน ก็เป็นเพราะไม่ได้ระบุคำสั่งนี้ ซึ่งจะเป็นการระบุว่า ให้ทำการเคลียร์ JPanel ทุกครั้งที่วาดใหม่
[src]super.paintComponent(g);[/src]

[src]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class BouncingBall extends JPanel implements ActionListener{
//============================================== fields
private int radius = 10; // radius of the ball
private int dx = 3; // some initial horizontal velocity
private int dy = 5; // some initial vertical velocity
private int x = radius; // initial position of ball
private int y = radius;
private Timer t; // to control the animation timing
private Color ballColor; // Color of the ball


public BouncingBall() {
this.setBackground(Color.white);
this.setForeground(Color.black);
this.setPreferredSize(new Dimension(400,200));
ballColor = Color.black;
t = new Timer(20, this); //call actionPerformed every 50 ms.
}//end constructor


public void setAnimation(boolean turnOn) {
if (turnOn) {
t.start(); // start animation by starting the timer.
}else{
t.stop(); // stop timer
}
}//end setAnimation

//========================================================= setBallColor
public void setBallColor(Color bc) {
ballColor = bc;
this.repaint(); // make it show
}//end setBallColor

//====================================================== actionPerformed
// Anything that is an ActionListener (this class) requires
// actionPerformed. Called by timer to compute next ball position
public void actionPerformed(ActionEvent e) {
//--- Get the the current size of the Panel.
int w = getWidth(); // get width and height of panel
int h = getHeight();

//--- Compute new x, y coordinates of the ball
x = x + dx;
if (x < radius) { // if we're at left side
x = radius;
dx = -dx;
}else if (x > w-radius) { // if we're at right side
x = w-radius;
dx = -dx;
}

y = y + dy;
if (y < radius) { // if we're at top
y = radius;
dy = -dy;
}else if (y > h-radius) { // if we're at bottom
y = h-radius;
dy = -dy;
}
// Calling repaint will call paintComponent. This is typically
// how animation is done - change something then call repaint.
this.repaint();
}//end actionPerformed


public void paintComponent(Graphics g) {
super.paintComponent(g); // paint background, border
g.setColor(ballColor);
g.fillOval(x-radius, y-radius, 2*radius, 2*radius); // Draw ball
}



public static void main(String args[])
{
JFrame frame=new JFrame("test");
BouncingBall Bball=new BouncingBall();
Bball.setAnimation(true);
frame.getContentPane().add(Bball);
frame.setSize(500,500);
frame.show();
}
}[/src]