bounce.java

来自「一个用JAVA编写的多线程小球运动的东西,希望大家能对多线程运行的原理以及优先级」· Java 代码 · 共 176 行

JAVA
176
字号
/*
 * Created on 2005-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author pingping
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Bounce {
	
		public static void main(String[] args)
		{
			JFrame frame = new BounceFrame();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
		}
}
class BounceFrame extends JFrame
{
	public BounceFrame()
	{
		setTitle("Bounce...");
		setSize(400,450);
		canvas = new BallCanvas();
		Container contain = getContentPane();
		contain.add(canvas,BorderLayout.CENTER);
		JPanel buttonpanel = new JPanel();
		addButton(buttonpanel,"start",new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						addBall(Thread.NORM_PRIORITY,Color.black);
					}
				});
		addButton(buttonpanel,"express",new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						addBall(Thread.NORM_PRIORITY+2,Color.red);
					}
				});
		/*addButton(buttonpanel,"selfish",new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						addBall(Thread.NORM_PRIORITY+2,Color.red);
					}
				});*/
		addButton(buttonpanel,"stop",new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						System.exit(0);
					}
				});
		contain.add(buttonpanel,BorderLayout.SOUTH);
	}
	public void addButton(Container c,String name,ActionListener listener)
	{
		JButton button = new JButton(name);
		button.addActionListener(listener);
		c.add(button);
	}
	public void addBall(int priority,Color color)
	{
		Ball b = new Ball(canvas,color);
		canvas.add(b);
		BallThread thread = new BallThread(b);
		thread.setPriority(priority);
		thread.start();
	}
	private BallCanvas canvas;
}
/*A Thread make ball move...*/

class BallThread extends Thread
{
	public BallThread(Ball aball)
	{ ball = aball; }
	public void run()
	{
		try
		{
			for(int i=1;i<=10000;i++)
			{
				ball.move();
				sleep(5);
			}
		}
		catch (InterruptedException e)
		{
			Thread.currentThread().interrupt();
		}
	}
	
	private Ball ball;
}

/*draw the ball*/
class BallCanvas extends JPanel
{
	public void add(Ball b)
	{
		balls.add(b);
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		for(int i=0;i<balls.size();i++)
		{
			Ball b = (Ball)balls.get(i);
			b.drawBall(g2);
		}
	}
	private ArrayList balls = new ArrayList();
}
class Ball
{
	public Ball(Component c,Color color)
	{
		canvas = c;
		acolor = color;
	}
	
	public void drawBall(Graphics2D g2)
	{
		g2.setColor(acolor);
		g2.fill(new Ellipse2D.Double(x,y,15,15));
	}
	
	public void move()
	{
		x += dx;
		y += dy;
		if (x<0)
		{
			x = 0;
			dx = -dx;
		}
		if (x+15 >= canvas.getWidth())
		{
			x = canvas.getWidth()-15;
			dx = -dx;
		}
		if (y<0)
		{
			y = 0;
			dy = -dy;
		}
		if (y+15 >= canvas.getHeight())
		{
			y = canvas.getHeight()-15;
			dy = -dy;
		}
		
		canvas.repaint();
	}
	private Component canvas;
	private int x = 0;
	private int y = 0;
	private int dx = 2;
	private int dy = 2;
	private Color acolor;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?