민들레를 흔들다

3912 단어
저녁에 기숙사에 돌아왔을 때 나는 어제 저녁의 코드를 수정해서 민들레가 펄럭이는 효과를 낼 수 있다는 것을 갑자기 생각했다. 그리고 코드를 엉망으로 수정했다. 코드는 다음과 같다. 그러나 펄럭이는 효과는 실행할 때 알 수 있다. 구체적인 코드는 다음과 같다.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *           。。。。(         ,         )
 * @author LONG
 *
 */
public class TheTree extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private Dimension di = null;
	private Graphics2D gr = null;
	private int x,y;		//         
	private JPanel jp = null;		//          
	private int count = 0;		//      
	
	/**
	 *      
	 * @param args
	 */
	public static void main(String[] args){
		TheTree tt = new TheTree();
		tt.showFrame();
	}
	
	/**
	 *     ,       
	 */
	public void showFrame(){
		this.setTitle("The Tree");
		Toolkit tl = Toolkit.getDefaultToolkit();
		di = tl.getScreenSize();
		this.setSize(di.width,di.height);
		this.setDefaultCloseOperation(3);
		jp = new JPanel();
		jp.setPreferredSize(new Dimension(di.width,di.height));
		x = di.width;			//      ,    
		y = di.height;			//      ,    
		this.add(jp);
		this.setResizable(false);
		jp.setBackground(Color.BLACK);
		this.setVisible(true);
		gr = (Graphics2D) jp.getGraphics();
		
		//        ,      
		jp.addMouseListener(new MouseAdapter(){
			public void mouseReleased(MouseEvent e){
				draw(e);
			}
		});	
	}
	
	/**
	 *       ,             
	 * @param e		      
	 */
	public void draw(MouseEvent e){
		while(y > 0){
			if(count == 0){
				x = e.getX();	//            
				y = e.getY();
			}else{
				y -= 5;		//     ,        ,    
				x += 6;
			}		
			init(x,y);		//       
		}
	}
	
	//           ,       
	public void init(int xx,int yy){
		BasicStroke bs = new BasicStroke(4);
		gr.setStroke(bs);
		gr.setColor(Color.GREEN);
		gr.drawLine(xx, yy, xx, yy - 400);	//           
		doSomething(xx,yy - 400,150);
	}
	
	/**
	 *         
	 * 
	 * @param x1	   x  
	 * @param y1	   y  
	 * @param h		  h
	 */
	public void doSomething(int x1, int y1, int h){
		//               
		for(int i = 0; i < 90; i++){
			double angle = i * 2 * Math.PI/90;		//         
			int x2 = x1 + (int)(Math.sin(angle) * h);
			int y2 = y1 - (int)(Math.cos(angle) * h);
			BasicStroke bs_next = new BasicStroke();
			gr.setStroke(bs_next);
			gr.setColor(Color.gray);
			gr.drawLine(x1, y1, x2, y2);
			gr.setColor(Color.WHITE);
			gr.fillOval(x2 - 1, y2 - 1, 5, 5);
		}
		try {
			Thread.sleep(120);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//        ,         ,                ,        
		BasicStroke bs = new BasicStroke(4);
		gr.setStroke(bs);
		gr.setColor(Color.BLACK);
		gr.drawLine(x1, y1 + 400, x1, y1);	
		for(int i = 0; i < 90; i++){
			double angle = i * 2 * Math.PI/90;		
			int x2 = x1 + (int)(Math.sin(angle) * h);
			int y2 = y1 - (int)(Math.cos(angle) * h);
			BasicStroke bs_next = new BasicStroke();
			gr.setStroke(bs_next);
			gr.setColor(Color.BLACK);
			gr.drawLine(x1, y1, x2, y2);
			gr.setColor(Color.BLACK);
			gr.fillOval(x2 - 1, y2 - 1, 5, 5);
		}
		count++;
	}
}

 

좋은 웹페이지 즐겨찾기