衝突円コード

46145 ワード

package zhang.start;

import javax.swing.*;
import java.awt.*;

//   
public class AlgoFrame extends JFrame {
    private final int canvasWidth;
    private final int canvasHeight;

    public AlgoFrame(String title) {
        this(title, 1024, 768);
    }

    public AlgoFrame(String title, int canvasWidth, int canvasHeight) { //canvas      
        super(title);

        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;

        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);     // canvas          
        pack();                     //      ,        

        setResizable(false);                                //            
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     //  X     
        setVisible(true);                                   //     
    }

    public int getCanvasWidth() {
        return canvasWidth;
    }

    public int getCanvasHeight() {
        return canvasHeight;
    }

    private Circle[] circles;
    //    
    public void render(Circle[] circles) {
        this.circles = circles;    //         
        this.repaint();     // JFrame            
        //AlgoCanvas        paintComponent    
    }

    //         JFrame       Content Pane 
    private class AlgoCanvas extends JPanel {       //              

        //                         
        public AlgoCanvas() {
            super(true);
        }

        @Override
        protected void paintComponent(Graphics g) {     //    
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;    //      

            //   :              
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(renderingHints);

            //    
            AlgoVisHelper.setStrokeWidth(g2d, 1);
            AlgoVisHelper.setColor(g2d, Color.magenta);
            for (Circle circle : circles) {
                if (!circle.isFilled)
                    AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
                else
                    AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR());

            }

            //    
//            AlgoVisHelper.setStrokeWidth(g2d,5);
//
//            AlgoVisHelper.setColor(g2d,Color.magenta);
//            AlgoVisHelper.fillCircle(g2d,canvasWidth/2,canvasHeight/2,200);   //     
//
//            AlgoVisHelper.setColor(g2d,Color.green);
//            AlgoVisHelper.strokeCircle(g2d,canvasWidth/2,canvasHeight/2,200);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(canvasWidth, canvasHeight);    //     
        }
    }


}

package zhang.start;

import java.awt.*;
import java.awt.geom.Ellipse2D;

public class AlgoVisHelper {

    private AlgoVisHelper() {
    }   //          

    public static void setStrokeWidth(Graphics2D g2d,int w){
        g2d.setStroke(new BasicStroke(w,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); //    、    
    }

    public static void strokeCircle(Graphics2D g2d,int x,int y ,int r){
        Ellipse2D circle = new Ellipse2D.Float(x-r,y-r,2*r,2*r);
        g2d.draw(circle);
    }

    public static void fillCircle(Graphics2D g2d,int x,int y ,int r){
        Ellipse2D circle = new Ellipse2D.Float(x-r,y-r,2*r,2*r);
        g2d.fill(circle);
    }

    public static void setColor(Graphics2D g2d,Color color){
        g2d.setColor(color);
    }


    public static void pause(int t){
        try {
            Thread.sleep(t);
        } catch (InterruptedException e) {
            System.out.println("Error is sleeping");
        }

    }

}

package zhang.start;
import java.awt.*;
import java.awt.event.*;

//                  
public class AlgoVisializer {

    private final Circle[] circles;
    private AlgoFrame frame ;
    private boolean isAnimated = true ;

    public AlgoVisializer(int sceneWidth,int sceneHeight , int N) {
        //     
        circles = new Circle[N];
        int R = 50;    //    
        for (int i = 0; i < circles.length; i++) {
            int x = (int) (Math.random() * (sceneWidth - 2 * R)) + R;
            int y = (int) (Math.random() * (sceneHeight - 2 * R)) + R;
            int vx = (int) (Math.random() * 11) - 5;  //  -5~5    
            int vy = (int) (Math.random() * 11) - 5;  //  -5~5    
            circles[i] = new Circle(x, y, R, vx, vy);
        }

        //     
        //          Gui                                、           
        EventQueue.invokeLater(
                () -> {
                    frame = new AlgoFrame("Wlcome", sceneWidth, sceneHeight);
                    frame.addKeyListener(new AlgoKeyListener());    //         
                    frame.addMouseListener(new AlgoMouseListener());
                    //The Event Dispatch Thred            
                    //                   ,                    
                    // Tasks on the event dispatch thread must finish quickly
                    //               
                    new Thread(
                            () -> {
                                run();
                            }
                    ).start();
                }
        );

    }

    //    
    public void run(){
        while (true) {
            //    
            frame.render(circles);
            AlgoVisHelper.pause(20);    //    
            //    
            if (isAnimated){
                for (Circle circle : circles) {
                    circle.move(0, 0, frame.getCanvasWidth(), frame.getCanvasHeight());
                }
            }
        }
    }

    //            implements KeyListener  |   extends KeyAdapter
    private class AlgoKeyListener extends KeyAdapter {
        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyChar() == ' '){    //       
                isAnimated = !isAnimated ;
            }
        }
    }

    //         
    private class AlgoMouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            e.translatePoint(0,-(frame.getBounds().height - frame.getCanvasHeight()) );
            for(Circle circle : circles) {
                if (circle.contain(e.getPoint()))
                    circle.isFilled = !circle.isFilled ;
            }
        }
    }


    public static void main(String[] args) {
        int sceneWidth = 800;
        int sceneHeight = 800;
        int N = 10;    //    
        AlgoVisializer algoVisializer = new AlgoVisializer(sceneWidth, sceneHeight, N);
    }

}

package zhang.start;

import java.awt.*;

//   
public class Circle {
    public int x , y ;
    public Boolean isFilled = false ;
    private int r ;
    private int vx , vy ;

    public Circle(int x, int y, int r, int vx, int vy) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.vx = vx;
        this.vy = vy;
    }

    public int getR() {
        return r;
    }

    public void move(int minx,int miny,int maxx,int maxy){
        x += vx ;
        y += vy ;
        checkCollision(minx, miny, maxx, maxy);
    }

    private void checkCollision(int minx,int miny,int maxx,int maxy){
        if (x - r < minx) {
            x = r;
            vx = -vx;
        }
        if (x + r >= maxx) {
            x = maxx - r;
            vx = -vx;
        }
        if (y - r < miny) {
            y = r;
            vy = -vy;
        }
        if (y + r >= maxy) {
            y = maxy - r;
            vy = -vy;
        }
    }


    public boolean contain(Point point) {
        return (x - point.x) * (x - point.x) + (y - point.y) * (y - point.y) <= r * r ;
    }
}