1. 程式人生 > >動態彈球的實現 加入了多執行緒技術--javaSE遊戲準備工作

動態彈球的實現 加入了多執行緒技術--javaSE遊戲準備工作

任務描述:實現了動態彈球的功能,對於有彈球功能的SE遊戲奠定了基礎。

package 運用執行緒技術的小球;


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;//不清楚這個有什麼用
import java.util.*;
import javax.swing.*;

public class Bounces {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		JFrame j = new BounceFrame();
		
		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		j.setVisible(true);
		
	}

}

class BallRunnable implements Runnable//執行緒是Thread(Runnable target) 要使用執行緒 你必須要實現Runnable介面

{
	
	private Ball ball;
	
	private Component component ;
	
	private static final int step = 300000;
	
	private static final int delay = 1;

	public BallRunnable(Ball aball,Component acomponent)
	{
		ball = aball;
		
		component = acomponent;
		
	}
	
	public void run()
	
	{
		
		
		try{
			
			for(int i = 0 ; i <= step ; i++)
					
					{
				ball.move(component.getBounds());
				
				component.repaint();//面板不斷重新整理
				
				Thread.sleep(delay);
				
					}
		}catch(InterruptedException e){}
				
}
}

class Ball
{//實現一個小球類 這個小球包含的方法 包括move()
	
	private double x = 0;
	
	private double y = 0 ;
	
	private double dx = 1;
	
	private double dy = 1;
	
    private static final int XSIZE =15;
    
    private static final int YSIZE =15;
	
	public void move(Rectangle2D bounds)
	
	{
		
		
		x = x + dx;
		
		y = y + dy;
		
		if(x < bounds.getMinX())
			
		{
			x = bounds.getMinX();
			
			dx = -dx;
			
		}
		
		if(x+XSIZE>=bounds.getMaxX())
		{
			x = bounds.getMaxX()-XSIZE;
			
			dx = - dx;
			
		}
		
        if(y < bounds.getMinY())
			
		{
			y = bounds.getMinY();
			
			dy = -dy;
			
		}
		
		if(y+YSIZE>=bounds.getMaxY())
		{
			y = bounds.getMaxY()-YSIZE;
			
			dy = - dy;
			
		}
		
	}//關於小球如何移動
	
	
	public Ellipse2D getShape()
	
	{
		return new Ellipse2D.Double(x,y,XSIZE,YSIZE);
	}//返回此時的小球的繪畫位置
}


class BallPanel extends JPanel
{
	private ArrayList<Ball> balls = new ArrayList<Ball>();
	
	//定義了一個集合 這個集合是Ball型別的儲存 這個知識點很關鍵 管儲存的作用

	public void add(Ball b)
	
	{
		balls.add(b);//將Ball的物件載入進去
		
	}//這就是重寫JPanel中的add方法 實現集合加入要更新的小球的重要一步
	
	public void  paint(Graphics g)
	{
		
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D) g;//轉換成2D的繪圖模式了
		
		for(Ball b :balls)
			
		{
			
			g2.fill(b.getShape());//這時的g2重新繪製小球的全部資訊 fill是 專門繪製圖形的方法
		}
		
		
	}
	
	
}


class BounceFrame extends JFrame
{

	private BallPanel panel;
	

	
	public BounceFrame()
	
	
	{
		
		setTitle("小球");
		
		panel = new BallPanel();
		
		panel.setBackground(Color.BLUE);
		
		add(panel,BorderLayout.CENTER);
		
		JPanel buttonPane = new JPanel();
		
		setBounds(200,200,700,500);
		
		addButton(buttonPane,"start",new ActionListener(){
			
			public void actionPerformed(ActionEvent event)
			
			{
				
				
				addBall();
			}
		});
		
		addButton(buttonPane,"Close",new ActionListener(){
			
			public void actionPerformed(ActionEvent event)
			{
				
				System.exit(0);
			}
		});
		
		add(buttonPane,BorderLayout.SOUTH);
		}
	
	public void addButton(Container c,String title,ActionListener listener)
	{
		JButton b = new JButton(title);
		
		c.add(b);
		
		b.addActionListener(listener);
		
	}//這個算是變形吧   學習思想
	
	public void addBall()
	{
			
			
			Ball ball = new Ball();
			
			panel.add(ball);
		
			Runnable r = new BallRunnable(ball,panel);
			
			Thread t = new Thread(r);//Thread(Runnable target)
			
			t.start();//啟動執行緒 實質上是啟動的run()方法
	}
}