多线程Java小程序弹跳球和按钮

时间:2014-10-17 21:56:33

标签: java multithreading button applet border

我试图创建一个Java小程序,它在applet窗口中弹出几个球,每个小程序都有自己的线程和自己的按钮。如何用球添加到每个按钮线程?

这是我的BiliardBalls.java类的代码

import java.awt.*;
    import java.applet.*;

    class CollideBall 
    {
        int width, height;
        public static final int diameter=30;
        double x, y, xinc, yinc, coll_x, coll_y;
        boolean collide;
        Color color;
        Graphics g;

        public CollideBall(int w, int h, int x, int y, double xinc, double yinc, Color c)
        {
            width=w;
            height=h;
            this.x=x;
            this.y=y;
            this.xinc=xinc;
            this.yinc=yinc;
            color=c;
        }

        public double getCenterX() {return x+diameter/2;}
        public double getCenterY() {return y+diameter/2;}
        public void move()
        {

            if (collide)
            {

                double xvect=coll_x-getCenterX();
                double yvect=coll_y-getCenterY();

                if((xinc>0 && xvect>0) || (xinc<0 && xvect<0))
                    xinc=-xinc;

                if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
                    yinc=-yinc;
                collide=false;
            }

            x+=xinc;
            y+=yinc;

            //border
            if(x<0 || x>width-diameter)
            {xinc=-xinc; x+=xinc; }

            if(y<0 || y>height-diameter)
            {yinc=-yinc; y+=yinc;}
        }

        public void hit(CollideBall b){
            if(!collide)
            {coll_x=b.getCenterX(); coll_y=b.getCenterY(); collide=true;}}

        public void paint(Graphics gr){
            g=gr; g.setColor(color);
            g.fillOval((int)x,(int)y,diameter,diameter); }
    }

    @SuppressWarnings("serial")
    public class BiliardBalls extends Applet implements Runnable
    {
        Thread runner1; 
        Thread runner2;
        Thread runner3;
        Thread runner4;
        Image Buffer;
        Graphics gBuffer;
        CollideBall ball[];
        AudioClip Collision;
        boolean collision;
        static final int MAX=4;

        Button a = new Button("A");
        Button b = new Button("B");
        Button c = new Button("C");
        Button d = new Button("D");
        boolean stop_a = true;
        boolean stop_b = true;
        boolean stop_c = true;
        boolean stop_d = true;
        boolean isClicked = false;
        @SuppressWarnings("deprecation")
        public void init()
        {
                setSize(500,500);  
            Buffer=createImage(size().width,size().height);
            gBuffer=Buffer.getGraphics();

            ball=new CollideBall[MAX];

            final int w=size().width;
            final int h=size().height;

            try{Collision=getAudioClip(getCodeBase());}
            catch (Exception e){}
            ball[0]=new CollideBall(w,h,50,20,1.5,2.0,Color.orange);
            ball[1]=new CollideBall(w,h,60,100,2.0,-3.0,Color.red);
            ball[2]=new CollideBall(w,h,15,70,-2.0,-2.5,Color.pink);
            ball[3]=new CollideBall(w,h,150,60,-2.7,-2.0,Color.cyan);

            a = new Button("Run first ball");
            add(a);
            b = new Button("Run second ball");
            add(b); 
            c = new Button("Run third ball");
            add(c);
            d = new Button("Run fourth ball");
            add(d);     
        } 

        public boolean action(Event evt, Object arg ) {

             if (evt.target==a)
            {
                 if(runner1 != null)
                    {   runner1.stop();
                        runner1 = null;
                        a.setLabel("run first ball"); 
                    }

                else if (runner1 == null)
                    {   runner1 = new Thread(this); 
                        runner1.start();

                        a.setLabel("stop first ball");

                    }
                stop_a = !stop_a;

            return true;}

             if (evt.target==b)
            {
                   if(runner2 != null)
                    {   runner2.stop();
                        runner2 = null;
                        b.setLabel("run second ball"); 
                    }

                else if (runner2 == null)
                    {   runner2 = new Thread( this); 
                        runner2.start();
                        b.setLabel("stop second ball");
                    }   
                stop_b = !stop_b;

            return true;}

             if (evt.target==c)
            {
                   if(runner3 != null)
                    {   runner3.stop();
                        runner3 = null;
                        c.setLabel("run third ball"); 
                    }

                else if (runner3 == null)
                    {   runner3 = new Thread( this); 
                        runner3.start();
                        c.setLabel("stop third ball");
                    }   
                stop_c = !stop_c;

            return true;}

             if (evt.target==d)
            {
                   if(runner4 != null)
                    {   runner4.stop();
                        runner4 = null;
                        d.setLabel("run fourth ball"); 
                    }

                else if (runner4 == null)
                    {   runner4 = new Thread( this); 
                        runner4.start();
                        d.setLabel("stop fourth ball");
                    }   
                stop_d = !stop_d;

            return true;}

            else 
              return super.action(evt,arg);
         }


        @SuppressWarnings("static-access")
        public void run()
        {
            while(true)
            {
                try {runner1.sleep(13);
    }
                catch (Exception e) { }

                for(int i=0;i<MAX;i++){
                       ball[i].move();} 

                handleCollision();
                repaint();
            }
        }

        @SuppressWarnings("static-access")
        boolean collide(CollideBall b1, CollideBall b2)
        {
            double wx=b1.getCenterX()-b2.getCenterX();
            double wy=b1.getCenterY()-b2.getCenterY();
            double distance=Math.sqrt(wx*wx+wy*wy);
            if(distance<b1.diameter)
            {
                if(collision)Collision.play();
                return true;}
            return false;
        }
        private void handleCollision()
        { for(int i=0;i<MAX;i++)
                for(int j=0;j<MAX;j++) {
                    if(i!=j) {
                        if(collide(ball[i], ball[j])){
                            ball[i].hit(ball[j]);
                            ball[j].hit(ball[i]);}}}
        }
        public void update(Graphics g) { paint(g);}
        @SuppressWarnings("deprecation")
        public void paint(Graphics g)
        {
            gBuffer.setColor(Color.black);
            gBuffer.fillRect(0,0,size().width,size().height);
            gBuffer.setColor(Color.green);
            for(int x=0;x<size().width;x+=50)
                gBuffer.drawLine(x,0,x,size().height);
            for(int y=0;y<size().height;y+=50)
                gBuffer.drawLine(0,y,size().width,y);
            for(int i=0;i<MAX;i++)
                ball[i].paint(gBuffer);
            g.drawImage (Buffer,0,0, this);
        }
    }

0 个答案:

没有答案