うごうご

いろふさんクラスが動いてくれない

いろふさんクラスが動いてくれない

  • タグ:
  • タグはありません
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Animation5 extends Applet{
    MyThread thread[]={null,null,null,null,null,null,null};
    int length=thread.length;
    int width,height;
    int move=10;
    Circle circle;
    Square square;
    Triangle triangle;
    makeIrof irof;
    Button resumeButton,suspendButton;
    boolean threadSuspended=false;
    
    public void init(){
        width=getSize().width;
        height=getSize().height;
        
        int radius=25;
        circle=new Circle(radius);
        circle.setXY(radius,50+radius);
        circle.setSpeed(move);
        circle.setColor(Color.red);
        circle.setMovingWidth(width);
        
        square=new Square(50);
        square.setXY(0,110);
        square.setSpeed(move);
        square.setColor(Color.blue);
        square.setMovingWidth(width);
        
        triangle=new Triangle(25,50);
        triangle.setXY(25,170);
        triangle.setSpeed(move);
        triangle.setColor(Color.green);
        triangle.setMovingWidth(width);
        
        irof=new makeIrof(50);
        irof.setXY(50,50);
        irof.setSpeed(move);
        irof.setColor(Color.lightGray);
        irof.setMovingWidth(width);
        irof.setMovingHeight(height);
        
        resumeButton=new Button("Resume");
        suspendButton=new Button("Suspend");
        add(resumeButton);
        add(suspendButton);
        resumeButton.addActionListener(new ActionAdp());
        suspendButton.addActionListener(new ActionAdp());
    }
    
    public void start(){
        for(int i=0;i<length;i++){
            if(thread[i]==null){
                thread[i]=new MyThread(i,400-100*(i-3));
                thread[i].start();
            }
        }
    }
    
    public void stop(){
        for(int i=0;i<length;i++){
            if(thread[i]!=null){
                thread[i]=null;
            }
        }
    }
    
    public void paint(Graphics g){
        circle.paint(g);
        square.paint(g);
        triangle.paint(g);
    }
    
    class ActionAdp implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==resumeButton){
                threadSuspended=false;
                myResume();
            }else if(e.getSource()==suspendButton){
                threadSuspended=true;
            }
        }
    }
    
    void myResume(){
        for(int i=0;i<length;i++){
            thread[i].myResume();
        }        
    }
    
    class MyThread extends Thread{
        int index,stime;
        
        MyThread(int index,int stime){
            this.index=index;
            this.stime=stime;
        }
        
        public void run(){
            Thread thisThread=Thread.currentThread();
            while(thread[index]==thisThread){
                while(threadSuspended){
                    synchronized(this){
                        try{
                            wait();
                        }catch(InterruptedException e){
                        }
                    }
                }
                repaint();
                try{
                    this.sleep(stime);
                    if(index==0){
                        circle.nextPlace();
                    }else if(index==1){
                        square.nextPlace();
                    }else if(index==2){
                        triangle.nextPlace();
                    }
                }catch(InterruptedException e){
                }
            }
        }
        
        public synchronized void myResume(){
            notify();
        }
    }
    
    class Square{
        int x=0;
        int y=0;
        int length=0;
        int move=0;
        int width=0;
        int height=0;
        Color color=Color.black;
        
        Square(int length){
            this.length=length;
        }
        
        public void setXY(int x,int y){
            this.x=x;
            this.y=y;
        }
        
        public void setSpeed(int move){
            this.move=move;
        }
        
        public void setColor(Color color){
            this.color=color;
        }
        
        public void setMovingWidth(int width){
            this.width=width;
        }
        
        public void setMovingHeight(int height){
            this.height=height;
        }
        
        public void paint(Graphics g){
            g.setColor(color);
            g.fillRect(x,y,length,length);
        }
        
        public void nextPlace(){
            setXY((int)(x+3*move*(Math.random()-0.3)),(int)(y+3*move*(Math.random()-0.4)));
            if(x>=width){
                setXY(x-width,y);
            }else if(x<0){
                setXY(x+width,y);
            }
            if(y<0){
                setXY(x,y+height);
            }else if(y>=height){
                setXY(x,y-height);
            }
        }
    }
    
    class Triangle{
        int x[];
        int y[];
        int xShift=0;
        int yShift=0;
        int move=0;
        int width=0;
        int height=0;
        Color color=Color.black;
        
        Triangle(int xShift,int yShift){
            this.xShift=xShift;
            this.yShift=yShift;
            x=new int[3];
            x[0]=0;
            x[1]=-xShift;
            x[2]=xShift;
            y=new int[3];
            y[0]=0;
            y[1]=yShift;
            y[2]=yShift;
        }
        
        public void setXY(int x,int y){
            this.x[0]=x;
            this.x[1]=x-xShift;
            this.x[2]=x+xShift;
            this.y[0]=y;
            this.y[1]=y+yShift;
            this.y[2]=y+yShift;
        }
                
        public void setSpeed(int move){
            this.move=move;
        }
        
        public void setColor(Color color){
            this.color=color;
        }
        
        public void setMovingWidth(int width){
            this.width=width;
        }
        
        public void setMovingHeight(int height){
            this.height=height;
        }
        
        public void paint(Graphics g){
            g.setColor(color);
            g.fillPolygon(x,y,3);
        }
        
        public void nextPlace(){
            setXY((int)(x[0]+3*move*(Math.random()-0.3)),(int)(y[0]+3*move*(Math.random()-0.4)));
            if(x[0]>=width){
                setXY(x[0]-width,y[0]);
            }else if(x[0]<0){
                setXY(x[0]+width,y[0]);
            }
            if(y[0]<0){
                setXY(x[0],y[0]+height);
            }else if(y[0]>=height){
                setXY(x[0],y[0]-height);
            }
        }
    }
    
    class Circle{
        int radius=0;
        int x=0;
        int y=0;
        int move=0;
        int width=0;
        int height=0;
        Color color=Color.black;
        
        Circle(int radius){
            this.radius=radius;
        }
        
        public void setXY(int x,int y){
            this.x=x;
            this.y=y;
        }
        
        public void setSpeed(int move){
            this.move=move;
        }
        
        public void setColor(Color color){
            this.color=color;
        }
        
        public void setMovingWidth(int width){
            this.width=width;
        }
        
        public void setMovingHeight(int height){
            this.height=height;
        }
        
        public void paint(Graphics g){
            g.setColor(color);
            g.fillOval(x-radius,y-radius,2*radius,2*radius);
        }
        
        public void nextPlace(){
            setXY((int)(x+3*move*(Math.random()-0.3)),(int)(y+3*move*(Math.random()-0.4)));
            if(x>=width){
                setXY(x-width,y);
            }else if(x<0){
                setXY(x+width,y);
            }
            if(y<0){
                setXY(x,y+height);
            }else if(y>=height){
                setXY(x,y-height);
            }
        }
    }
    class makeIrof{
        int radius=0;
        int x=0;
        int y=0;
        int move=0;
        int width=0;
        int height=0;
        Color color=Color.black;
        
        makeIrof(int radius){
            this.radius=radius;
        }
        
        public void setXY(int x,int y){
            this.x=x;
            this.y=y;
        }
        
        public void setSpeed(int move){
            this.move=move;
        }
        
        public void setColor(Color color){
            this.color=color;
        }
        
        public void setMovingWidth(int width){
            this.width=width;
        }
        
        public void setMovingHeight(int height){
            this.height=height;
        }
        
        public void paint(Graphics g){
            g.setColor(color);
            g.fillArc(x-radius,y-radius,2*radius,2*radius,10,300);
        }
        
        public void nextPlace(){
             setXY((int)(x+3*move*(Math.random()-0.3)),(int)(y+3*move*(Math.random()-0.4)));
            if(x>=width){
                setXY(x-width,y);
            }else if(x<0){
                setXY(x+width,y);
            }
            if(y<0){
                setXY(x,y+height);
            }else if(y>=height){
                setXY(x,y-height);
            }
        }
    }
}