あとちょっと

四角、丸、三角のどれか1つでもぱくぱくに被ったらぱくぱくが円になるようにしたいところ

四角、丸、三角のどれか1つでもぱくぱくに被ったらぱくぱくが円になるようにしたいところ

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

public class Problem5 extends Applet{
    
    MyThread thread[]={null,null,null,null,null};
    int length=thread.length;
    int width,height;
    int move=10;
    int circlex=25;
    int squarex=0;
    int trianglex=25;
    int circley=75;
    int squarey=110;
    int triangley=170;
    Circle circle;
    Square square;
    Triangle triangle;
    makePack pack;
    Button resumeButton,suspendButton;
    boolean threadSuspended=false;
    
    public void init(){
        width=getSize().width;
        height=getSize().height;
        
        int radius=25;
        circle=new Circle(radius);
        circle.setXY(circlex,circley);
        circle.setSpeed(move);
        circle.setColor(Color.red);
        circle.setMovingWidth(width);
        circle.setMovingHeight(height);
        
        square=new Square(50);
        square.setXY(squarex,squarey);
        square.setSpeed(move);
        square.setColor(Color.blue);
        square.setMovingWidth(width);
        square.setMovingHeight(height);
        
        triangle=new Triangle(25,50);
        triangle.setXY(trianglex,triangley);
        triangle.setSpeed(move);
        triangle.setColor(Color.green);
        triangle.setMovingWidth(width);
        triangle.setMovingHeight(height);
        
        pack=new makePack(50);
        pack.setXY(450,50);
        pack.setSpeed(move);
        pack.setColor(Color.lightGray);
        pack.setMovingWidth(width);
        pack.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);
        pack.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();
                    }else if(index==3){
                        pack.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+5*move*(Math.random()-0.3)),(int)(y+5*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]+5*move*(Math.random()-0.3)),(int)(y[0]+5*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+5*move*(Math.random()-0.3)),(int)(y+5*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 makePack{
        int radius=0;
        int x=0;
        int y=0;
        int move=0;
        int width=0;
        int height=0;
        Color color=Color.black;
        
        makePack(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);
            if((((450<=circlex) && (circlex<=500)) && ((y<=circley) && (circley<=y+50))) || (((450<=squarex) && (squarex<=500)) && ((y<=squarey) && (squarey<=y+50))) || (((450<=trianglex) && (trianglex<=500)) && ((y<=triangley) && (triangley<=y+50)))){
                g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
            }else{
                g.fillArc(x-radius,y-radius,2*radius,2*radius,190,300);
            }
        }
        
        public void nextPlace(){
            setXY((int)x,(int)(y+3*move*(Math.random()-0.4)));
            if(y<0){
                setXY(x,y+height);
            }else if(y>=height){
                setXY(x,y-height);
            }
        }
    }
}