관리 메뉴

취미개발 블로그와 마음수양

눈 내리는 자바 소스 본문

Language

눈 내리는 자바 소스

아라한사 2014. 8. 13. 17:35

알록달록한 공임;;





import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;


/**

 * Created by USER on 2014-08-13.

 */

public class SnowRect extends JFrame {

    Rect canvas;

    int y=30;

    final static int x = 900;

    boolean flag=true;



    ArrayList<Ball> ballList = new ArrayList<Ball>();


    public SnowRect() throws HeadlessException {


        setTitle("hi");

        canvas = new Rect();



        JButton button = new JButton("click");

        button.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {


                System.out.println("버튼 출력");

                for (int i = 0; i < 5; i++) {

                    ballList.add(new Ball());

                }


                if(flag){

                    Thread myThread = new Thread(canvas);

                    myThread.start();

                    flag=false;

                }else{


                }


            }

        });



        add(canvas, BorderLayout.CENTER);

        add(button, BorderLayout.SOUTH);


        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(x, 500);

    }


    public static void main(String[] args) {

        new SnowRect();

    }



    class Rect extends Canvas implements  Runnable{


        Rect() {

            ballList.add(new Ball());

        }


        @Override

        public void paint(Graphics g) {

            super.paint(g);


                for (int i = 0; i < ballList.size(); i++) {

                    g.fillOval(ballList.get(i).x, ballList.get(i).y, 25, 25);

                    int[] color = new int[3];


                    color[0] = (int)(Math.random()*255);

                    color[1] = (int)(Math.random()*255);

                    color[2] = (int)(Math.random()*255);

                    Color c = new Color(color[0], color[1], color[2]);

                    g.setColor(c);

                }


        }




        @Override

        public void run() {

            while(true){

                try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }


                for(int i=0;i<ballList.size();i++){

                    ballList.get(i).plusY();

                    repaint();

                }

            }


        }

    }

    class Ball{

        int x;

        int y;


        Ball() {

            this.x= (int)(Math.random()*900);

            y=20+(int)(Math.random()*10);

        }


        public void plusY(){

            if(y<=490){

                y+=10;

            }


            if((int)Math.random()*2 ==0){

                x+=5;

            }else{

                x-=5;

            }

        }

    }

}



'Language' 카테고리의 다른 글

javax.annotation.meta.When not found  (0) 2019.04.08
Java beans, VO, DTO, POJO  (0) 2016.10.08
자바 enum  (0) 2014.08.04
날짜 오늘 날짜 변환 공식  (0) 2014.06.06
ip주소 중간중간에 하트로표시해주기.  (0) 2014.05.31