انشأت هذا البرنامج بنفسي بغرض التعلم..وأردت أن اشارككم ما تعلمت
أرجو أن يحوز البرنامج على أعجابكم
ملاحظه: البرنامج يتكون من 2 classes و ملف صوت Ding.wav كلها مضافه إلى ملف Jar
هذا هو الكود للبرنامج
/*
* Ball.java
*
* Created on April 1, 2004, 6:49 AM
*/
package animation;
import javax.swing.*;
import java.awt.*;
/**
*
* @author Abdul
*/
public class Ball extends JFrame{
/** Creates a new instance of Ball */
public Ball() {
setTitle("Ball");
setBounds(100, 100, 400,300);
getContentPane().add(new Board());
setVisible(true);
}
public static void main(String[] args){
Ball me=new Ball();
me.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}/*
* graphics.java
*
* Created on April 1, 2004, 6:43 AM
*/
package animation;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
/**
*
* @author Abdul
*/
public class Board extends JPanel{
/** Creates a new instance of graphics */
public Board() {
//loading the sound file
try{
File af= new File(getClass().getResource("Ding.wav").getFile());
ac=Applet.newAudioClip(getClass().getResource("Ding.wav"));
}catch(Exception e){e.printStackTrace();}
//creating redrowing Thread
Thread t=new Thread(new Runnable(){
public void run(){
while(key){
repaint();
try{
Thread.sleep(sleeptime);
}catch(Exception e){}
}
}
});
t.start();
}
public void makeBalles(){
n=4; //number of balls
x=new int[n];
y=new int[n];
xi=new int[n];
yi=new int[n];
width=new int[n];
for(int i=0; i<n;i++){
xi=(int)(Math.pow(-1, (int)(Math.random()*4.)));
yi=(int)(Math.pow(-1, (int)(Math.random()*4.)));
width=5+(int)(Math.random()*50.0);
x=(int)(Math.random()*(getWidth()-width));
y=(int)(Math.random()*(getHeight()-width));
}
}
public void paint(Graphics g){
if(start){
makeBalles();
start=false;
}
g.clearRect(0,0, getWidth(), getHeight());
for(int z=0; z<x.length;z++){
if (x[z]+xi[z]+width[z]>getWidth()|| x[z]+xi[z]<0) {xi[z]=-xi[z];ac.play();}
if(y[z]+yi[z]+width[z]>getHeight() || y[z]+yi[z]<0) {yi[z]=-yi[z];ac.play();}
x[z]+=xi[z];
y[z]+=yi[z];
//g.setColor(Color.magenta);
g.fillOval(x[z],y[z], width[z],width[z]);
}
}
boolean start=true;
AudioClip ac;
int n;
int[] width;
int[] x, y,xi,yi;
boolean key=true;
int sleeptime=5;
}