الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

وش رايكم في هالبرنامج

مغلق
بدأه code2263 في 3 أبريل 2004 · 4 رد · 593 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

انشأت هذا البرنامج بنفسي بغرض التعلم..وأردت أن اشارككم ما تعلمت

أرجو أن يحوز البرنامج على أعجابكم

ملاحظه: البرنامج يتكون من 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;
}

Balls.jar

#2

it is funny code .. :)

it works ... but still it give me some error while it is working

see below

اقتباس
java.lang.NullPointerException

at Board.<init>(Board.java:24)

at Ball.<init>(Ball.java:20)

at Ball.main(Ball.java:25)

java.lang.NullPointerException

at Board.paint(Board.java:64)

at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4771)

at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4724)

at javax.swing.JComponent._paintImmediately(JComponent.java:4668)

at javax.swing.JComponent.paintImmediately(JComponent.java:4477)

at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)

at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

java.lang.NullPointerException

at Board.paint(Board.java:65)

at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4771)

at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4724)

at javax.swing.JComponent._paintImmediately(JComponent.java:4668)

at javax.swing.JComponent.paintImmediately(JComponent.java:4477)

at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)

at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

java.lang.NullPointerException

do you have an idea about it ..?

#3

I run the code myself and I got nothing. Are u sure You used Sun's version of java virtual machine??

By the way, do u hear sounds when the balls hit window boundries?

تم تعديل هذه المشاركة بواسطة code2263 في 5 أبريل 2004 في 08:13

#4

as i mention .. the code work so fine ..

but in the promt window it show me those massages..

..

and yes i head the sound when they hit the fram boundary

#5
I don't see any errors
The line where the error is generated is where the sound is played. If you hear the sound then you souldn't have any problems in that line. 
Ary you using my jar file?
or do you compile the code yourself? if so tell me about the folder address where the files are located.

هذا الموضوع مغلق.

مواضيع مشابهة