首页>代码>一个好玩的java swing小游戏——桌球>/桌球Billiards/src/Billiards.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jvnet.substance.SubstanceLookAndFeel;

import com.swtdesigner.SwingResourceManager;

public class Billiards extends JPanel implements Runnable {

	// //常量声明////////////////////////////////////

	public final double WIDTH_OF_DESK = 803d;// 球台宽度
	public final double HEIGHT_OF_DESK = 526d;// 球台高度

	public final double WIDTH_OF_FLAT = 748d;// 击球区宽度
	public final double HEIGHT_OF_FLAT = 388d;// 击球区高度

	public final int COUNT_OF_ALL_BALL = 16;// 球的总数
	public final double RADIUS = 16d;// 球的半径

	// //属性///////////////////////////////////////

	private Ball ball[] = new Ball[COUNT_OF_ALL_BALL];
	private double pocketX[] = { 0d, WIDTH_OF_FLAT / 2d, WIDTH_OF_FLAT };
	private double pocketY[] = { 0d, HEIGHT_OF_FLAT };
	private int score;// 得分
	private int ball_left;// 剩余球数
	private boolean time_to_hit;// 是否该击球
	private double stepXforHit, stepYforHit;
	private int mouseX, mouseY;
	private boolean clicked;
	private JLabel label;// 显示分数的标签
	
	private Power power;
	private boolean isfirst_flag=true;

	private JLabel ball_label[] = new JLabel[COUNT_OF_ALL_BALL];

	// //构造函数////////////////////////////////////

	public Billiards() 
	{
		
		score = 0;
		time_to_hit = true;
		ball_left = 16;
		stepXforHit = 0d;
		stepYforHit = 0d;
		clicked = false;
		
		power=new Power();

		// 初始化界面
		try {
            UIManager.setLookAndFeel(new SubstanceLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
		viewInitialize();

		// 初始化16个球(1白、15红)的属性
		ballInitialize();

		// 开局,并开始检测、控制、重绘
		new Thread(this).start();
	}
	
	public void run() 
	{
		long timeStart;
		long timeEnd;
		long timeLast;
		timeStart = System.currentTimeMillis(); // 返回以毫秒为单位的当前时间,赋给timeStart,作为循环前的开始时间

		while (true)
		{
			timeEnd = System.currentTimeMillis(); // 设置每个单位时间段的结尾时刻
			timeLast = timeEnd - timeStart; // timeEnd - timeStart即为单位时间
			//hitHandle(); // 击球处理
			moveHandle((double) timeLast); // 球的移动处理
			crashHandle(); // 球的碰撞处理
			reboundHandle(); // 球的反弹处理
			pocketHandle(); // 球的入袋处理
			stayHandle(); // 球的静止处理(判断所有球是否都已停下)
			goNext(); // 将下一帧的坐标作为当前坐标
			draw(); // 绘制

			timeStart = timeEnd; // 重置时间起点

			try {
				Thread.sleep(10L);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}

	}

	public void ballInitialize() {
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++)// !!!!!!!!!!!!!!!!!!
		{
			ball[i] = new Ball();
		}

		ball[0].setID(0);
		ball[0].setColor(Color.white);
		ball[0].setCurrentLocation(ball_label[0].getX() + RADIUS, ball_label[0].getY()+ RADIUS);

		for (int i = 1; i < COUNT_OF_ALL_BALL; i++) {
			ball[i].setID(i);
			ball[i].setColor(Color.red);
			ball[i].setValue(i);
			ball[i].setCurrentLocation(ball_label[i].getX() + RADIUS,
					ball_label[i].getY() + RADIUS);
		}
	}

	public void viewInitialize() 
	{
		this.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(final MouseEvent arg0) {
				mouseX = arg0.getX();
				mouseY = arg0.getY();
			}
		});

		this.addMouseListener(new MouseAdapter() {
			//开始计算力量
			public void mousePressed(final MouseEvent arg0) {
				if(arg0.getModifiers()==InputEvent.BUTTON1_MASK){
//				System.out.println(arg0.toString());
				if(isfirst_flag)
				{
				power.mythread.start();
				isfirst_flag=false;
				}
				else
					power.mythread.resume();
				}

			}
			// 开始击球
			public void mouseReleased(final MouseEvent arg0) {
				if (arg0.getModifiers() == InputEvent.BUTTON1_MASK) {
					power.mythread.suspend();
					clicked = true;
//					System.out.println(arg0.toString());
					stepXforHit=power.progressBar.getValue()*6;
					stepYforHit=power.progressBar.getValue()*6;
//					System.out.println("stepXforHit:"+stepXforHit);
				}
			}

		});

		JFrame frame = new JFrame();
		frame.getContentPane().addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent arg0) {
			}

			public void mouseEntered(final MouseEvent arg0) {
			}

			public void mouseExited(final MouseEvent arg0) {
			}
		});
		frame.setTitle("桌球-2.0版");
		frame.setResizable(false);
		frame.getContentPane().setLayout(null);
		frame.setIconImage(SwingResourceManager.getImage(Billiards.class,
				"icon.png"));
		frame.setBounds(100, 100, 803, 526);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// button.setFont(new Font("", Font.PLAIN, 11));

		final JButton btnCancel = new JButton();
		btnCancel.setToolTipText("关闭程序退出");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				System.exit(1);
			}
		});
		btnCancel.setText("退出");
		btnCancel.setBounds(723, 452, 66, 26);
		frame.getContentPane().add(btnCancel);

		final JButton btnHelp = new JButton();
		btnHelp.setToolTipText("打开帮助");
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				btnHelpactionPerformed();
			}
		});
		btnHelp.setText("帮助");
		btnHelp.setBounds(25, 452, 71, 26);
		frame.getContentPane().add(btnHelp);

		final JButton btnHighScore = new JButton();
		btnHighScore.setToolTipText("最高分");
		btnHighScore.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {
				btnHighScoreactionPerformed();
			}
		});
		btnHighScore.setText("最高分");
		btnHighScore.setBounds(102, 452, 80, 26);
		frame.getContentPane().add(btnHighScore);

		final JLabel label_1 = new JLabel();
		label_1.setFont(new Font("", Font.BOLD, 16));
		label_1.setForeground(new Color(255, 0, 0));
		label_1.setText("力量:");
		label_1.setBounds(391, 460, 59, 18);
		frame.getContentPane().add(label_1);

		power.progressBar.setBounds(456, 460, 174, 18);
		frame.getContentPane().add(power.progressBar);
		
		this.setLayout(null);
		this.setOpaque(false);
		this.setBounds(25, 20, (int) WIDTH_OF_FLAT, (int) HEIGHT_OF_FLAT);
		frame.getContentPane().add(this);

		ball_label[0] = new JLabel();
		ball_label[0].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/0.png"));
		ball_label[0].setBounds(145, 176, 32, 32);
		this.add(ball_label[0]);

		ball_label[1] = new JLabel();
		ball_label[1].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/1.png"));
		ball_label[1].setBounds(488, 179, 32, 32);
		this.add(ball_label[1]);

		ball_label[2] = new JLabel();
		ball_label[2].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/2.png"));
		ball_label[2].setBounds(628, 110, 32, 32);
		this.add(ball_label[2]);

		ball_label[3] = new JLabel();
		ball_label[3].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/3.png"));
		ball_label[3].setBounds(523, 160, 32, 32);
		this.add(ball_label[3]);

		ball_label[4] = new JLabel();
		ball_label[4].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/4.png"));
		ball_label[4].setBounds(593, 229, 32, 32);
		this.add(ball_label[4]);

		ball_label[5] = new JLabel();
		ball_label[5].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/5.png"));
		ball_label[5].setBounds(593, 160, 32, 32);
		this.add(ball_label[5]);

		ball_label[6] = new JLabel();
		ball_label[6].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/6.png"));
		ball_label[6].setBounds(628, 179, 32, 32);
		this.add(ball_label[6]);

		ball_label[7] = new JLabel();
		ball_label[7].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/7.png"));
		ball_label[7].setBounds(628, 248, 32, 32);
		this.add(ball_label[7]);

		ball_label[8] = new JLabel();
		ball_label[8].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/8.png"));
		ball_label[8].setBounds(558, 179, 32, 32);
		this.add(ball_label[8]);

		ball_label[9] = new JLabel();
		ball_label[9].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/9.png"));
		ball_label[9].setBounds(628, 213, 32, 32);
		this.add(ball_label[9]);

		ball_label[10] = new JLabel();
		ball_label[10].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/10.png"));
		ball_label[10].setBounds(628, 144, 32, 32);
		this.add(ball_label[10]);

		ball_label[11] = new JLabel();
		ball_label[11].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/11.png"));
		ball_label[11].setBounds(593, 125, 32, 32);
		this.add(ball_label[11]);

		ball_label[12] = new JLabel();
		ball_label[12].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/12.png"));
		ball_label[12].setBounds(558, 213, 32, 32);
		this.add(ball_label[12]);

		ball_label[13] = new JLabel();
		ball_label[13].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/13.png"));
		ball_label[13].setBounds(558, 144, 32, 32);
		this.add(ball_label[13]);

		ball_label[14] = new JLabel();
		ball_label[14].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/14.png"));
		ball_label[14].setBounds(523, 195, 32, 32);
		this.add(ball_label[14]);

		ball_label[15] = new JLabel();
		ball_label[15].setIcon(SwingResourceManager.getIcon(Billiards.class,
				"/GameImage/15.png"));
		ball_label[15].setBounds(593, 194, 32, 32);
		this.add(ball_label[15]);

		label = new JLabel();
		label.setFont(new Font("", Font.BOLD, 16));
		label.setForeground(new Color(255, 0, 0));
		label.setText("分数:0");
		label.setBounds(226, 460, 113, 18);
		frame.getContentPane().add(label);

		final JLabel image1 = new JLabel();
		image1.setIcon(SwingResourceManager.getIcon(Billiards.class, "1.jpg"));// 为方便编辑
		// 先隐藏图片
		// 下同
		image1.setBounds(0, 0, 513, 505);
		frame.getContentPane().add(image1);

		final JLabel image2 = new JLabel();
		image2.setIcon(SwingResourceManager.getIcon(Billiards.class, "2.jpg"));
		image2.setBounds(510, 0, 287, 505);
		frame.getContentPane().add(image2);

		frame.setVisible(true);
	}

	protected void btnHelpactionPerformed() {
		// TODO Auto-generated method stub
		String msg ="<html>" +
				"<table width=558 border=0 cellspacing=0 cellpadding=0>" +
				"<br>" +
				"<table width=558 border=0 cellspacing=0 cellpadding=0>" +
				"<tr>" +
				"<br>"+
				"<br>"+
				"<td><p align=left><strong>【 常见问题 】<br>" +
				"</strong> <br>" +
				"1. 如果您的机器没有安装显卡驱动,或是只是用的系统默认的驱动,会影响您正常游戏,请安装正确的显卡驱动。 <br>" +
				"2. 如何开始游戏:打开游戏界面,游戏即开始。 <br>" +
				"3. 如何击球:瞄准目标球后,按住鼠标左键不放,屏幕右下角的力度槽会由大至小的变化,力度槽满时代表最大力量,在您觉得合适的力度时候松开鼠标左键,将按照当前力度击球。 <br>" +
				"4. 怎么查看玩家信息:右键点击用户所属的信息栏即可。 <br>" +
				"5. 如何设置我喜欢的桌布颜色和场景:您可以通过右上角的游戏设置面板,进行操作。 </p>" +
				"<p align=left><strong>        <br>" +
				"</strong><strong>【 游戏介绍 】</strong></p>" +
				"<br>"+
//				"<img width=796 height=518 src = /GameImage/1.png><br>"+
				"<p>          桌 球 是一款模拟真实的桌球游戏,游戏提供了2D 视角,极为简单而流行的操作方式,绚丽的动画特效,配以真实的物理参数,让各位玩家有如身临其境,仿佛自己就是叱刹风云的球坛新贵,置身于万众瞩目的赛场一切乐趣受尽在其中,如果您是桌球高手,桌 球 可以让您们尽情挥洒技艺,您会发现在这里,可以把您平日的技巧发挥的淋漓尽致,让对手崇拜的五体投地。如果您是新手,也不用担心被人狂切,桌 球 提供了完美的练习模式,您可以不受干扰的在这里练习您的技术,直到您横空出世,称霸一方。</p>" +
				"<p align=left><strong>一.系统要求配置</strong></p>" +
				"<p>操作系统: Windows 98/ Me/2000 /xp/vista ,系统要求安装 Direct X 8.1 版本以上驱动。 " +
				"CPU : PIII 800 Mhz 以上 " +
				"显存: 32M 以上 " +
				"内存: 128M 以上 " +
				"</p>" +
				"<strong>二.游戏操作方法</strong>" +
				"<br>"+
				"<p>功能菜单:鼠标左键点击即可。" +
				"玩家个人信息:在玩家信息栏上鼠标右键点击即可。" +
				"摆球:当需要玩家摆球时,鼠标会变成一个手部形状,玩家只需要移动鼠标把球发到自己理想的位置上,然后点击鼠标左键即摆好球了。" +
				"击球:瞄准目标球后,按住鼠标左键不放,屏幕右下角的力度槽会由大至小的变化,力度槽满时代表最大力量,在您觉得合适的力度时候松开鼠标左键,将按照当前力度击球。" +
				"选择击球点 " +
				"在轮到自己击球的时候前,使用左键在游戏界面的右下角选择您需要的击球点,然后再进行击球,这样您可以打出退杆,旋转球等多种厉害的打法。" +
				"</p>" +
				"<p><strong>三.游戏规则 </strong></p>" +
				"<br>"+
				"<p>按所进球号计算相应得分</p>" +
				"<p>母球进袋扣四分</p>" +
				"<p>&nbsp; </p></td>" +
				"</tr>" +
				"</table>"+
				"</html>";
            ;

		JOptionPane.showMessageDialog(null,msg,"帮助",JOptionPane.QUESTION_MESSAGE);
		
	}

	protected void btnHighScoreactionPerformed() {
		// TODO Auto-generated method stub
		
	}

//	public void hitHandle() 
//	{
//		if (time_to_hit) 
//		{
//			ball[0].setStep(stepXforHit, stepYforHit);
//			time_to_hit = false;
//		}
//	}

	public void moveHandle(double timeLast) 
	{
		double tmpX, tmpY;
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++) {
			tmpX = ball[i].getCurrentX() + ball[i].getStepX() * timeLast / 100d;
			tmpY = ball[i].getCurrentY() + ball[i].getStepY() * timeLast / 100d;
			if (!ball[i].isPocketed()) {
				ball[i].setNextLocation(tmpX, tmpY);
				ball[i].setStep(ball[i].getStepX() * 0.99d,
						ball[i].getStepY() * 0.99d);


				if (Math.hypot(ball[i].getStepX(), ball[i].getStepY()) < 1d) // 当位移量很小时,将其置零,从而使球停止,hypot返回 sqrt(x^2+y^2)
				{
					ball[i].setStep(0, 0);
				}
			}
		}
	}

	public void crashHandle() 
	{
		int i1, j1;// 判断碰撞临时序号
		boolean flag = false;
		for (i1 = 0; i1 < COUNT_OF_ALL_BALL; i1++) {// 对每个求进行碰撞判断
			if (!ball[i1].isPocketed()) {// 如果球在击球区,继续判断与其他球相撞的可能性
				for (j1 = i1 + 1; j1 < COUNT_OF_ALL_BALL; j1++) {
					if (!ball[j1].isPocketed() && isCrash(i1, j1)) {
						for (int temp = 0; temp <= 10; temp++) {// 考虑可能重合,要疏远两球的下一步中心
							double xi, yi, xj, yj;
							xi = (ball[i1].getCurrentX() + ball[i1].getNextX()) / 2d;
							yi = (ball[i1].getCurrentY() + ball[i1].getNextY()) / 2d;
							xj = (ball[j1].getCurrentX() + ball[j1].getNextX()) / 2d;
							yj = (ball[j1].getCurrentY() + ball[j1].getNextY()) / 2d;
							ball[i1].setNextLocation(xi, yi);
							ball[j1].setNextLocation(xj, yj);
							flag = isCrash(i1, j1);
						}
						if (flag) {// 疏远无效
							ball[i1].setNextLocation(ball[i1].getCurrentX(),
									ball[i1].getCurrentY());
							ball[j1].setNextLocation(ball[j1].getCurrentX(),
									ball[j1].getCurrentY());
						}
						double l1 = ball[i1].getNextX() - ball[j1].getNextX();
						double l2 = ball[i1].getNextY() - ball[j1].getNextY();
						double l3 = Math.hypot(l1, l2);
						double l4 = l1 / l3;// 球心连线与横纵坐标的夹角的正,余弦
						double l5 = l2 / l3;
						double ki, li, kj, lj;
						ki = ball[i1].getStepX() - ball[i1].getStepX() * l4
								* l4;
						ki -= ball[i1].getStepY() * l4 * l5;
						ki += ball[j1].getStepX() * l4 * l4;
						ki += ball[j1].getStepY() * l4 * l5;// i1球的速度横分量
						li = ball[i1].getStepY() - ball[i1].getStepY() * l5
								* l5;
						li -= ball[i1].getStepX() * l4 * l5;
						li += ball[j1].getStepY() * l5 * l5;
						li += ball[j1].getStepX() * l4 * l5;// i1球的速度纵分量
						kj = ball[j1].getStepX() - ball[j1].getStepX() * l4
								* l4;
						kj -= ball[j1].getStepY() * l4 * l5;
						kj += ball[i1].getStepX() * l4 * l4;
						kj += ball[i1].getStepY() * l4 * l5;// j1球的速度横分量
						lj = ball[j1].getStepY() - ball[j1].getStepY() * l5
								* l5;
						lj -= ball[j1].getStepX() * l4 * l5;
						lj += ball[i1].getStepY() * l5 * l5;
						lj += ball[i1].getStepX() * l4 * l5;// j1球的速度纵分量
						ball[i1].setStep(ki, li);
						ball[j1].setStep(kj, lj);

					}
				}

			}
		}
	}

	public boolean isCrash(int i, int j) // 判断两球是否相撞(坐标之和小于直径)
	{
		return Math.hypot(ball[i].getNextX() - ball[j].getNextX(), ball[i]
				.getNextY()
				- ball[j].getNextY()) <= 2D * RADIUS;
	}

	public void pocketHandle() 
	{
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++) {
			if (!ball[i].isPocketed()) {
				for (int j1 = 0; j1 < 2; j1++) {
					for (int j2 = 0; j2 < 3; j2++) {
						if (Math.hypot(pocketX[j2] - ball[i].getCurrentX(),
								pocketY[j1] - ball[i].getCurrentY()) < 1.5d * RADIUS) {
							ball[i].setPocketed(true);
							if (ball[i].getID() != 0) {
								ball_left--;
								score += ball[i].getValue();
							} else {
								score -= 4;
								ball[0].setCurrentLocation(145 + RADIUS,
										176 + RADIUS);
								ball_label[0].setLocation((int) (145),
										(int) (176));
								// 主球进袋情况的处理要写在draw方法中,若ball[0].getPocketed()=true,则重新摆主球
							}
							label.setText("分数:" + score);
							ball[i].setStep(0, 0);
						}
					}
				}
			}

		}
	}

	public void reboundHandle()// 球与球台边缘碰撞时反弹
	{
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++) {
			if (ball[i].isPocketed()) // 进袋则跳出,继续循环
				continue;
			else {
				double Vx = ball[i].getStepX(); 	// 取横向速度
				double Vy = ball[i].getStepY(); 	// 取纵向速度
				double X = ball[i].getNextX(); 		// 取下一刻横坐标
				double Y = ball[i].getNextY(); 		// 取下一刻的纵坐标
				if (X <= (RADIUS ) && Vx < 0) // 判断是否与左边发生碰撞
				{
					Vx = -Vx;
					Vy = Vy;
					ball[i].setStep(Vx, Vy); // 反弹后的速度
				} 
				else if ((WIDTH_OF_FLAT-X)<RADIUS && Vx > 0) // 判断是否与右边发生碰撞
				{
					Vx = -Vx;
					Vy = Vy;
					ball[i].setStep(Vx, Vy); // 反弹后的速度
				}
				else if (Y <  RADIUS && Vy < 0) // 判断是否与上边发生碰撞
				{
					Vx = Vx;
					Vy = -Vy;
					ball[i].setStep(Vx, Vy); // 反弹后的速度
				} else if (HEIGHT_OF_FLAT - Y <= RADIUS && Vy >0) // 判断是否与下边发生碰撞
				{
					Vx = Vx;
					Vy = -Vy;
					ball[i].setStep(Vx, Vy); // 反弹后的速度
				} 
				else
					continue;
			}
		}
	}

	public void stayHandle() 
	{
		boolean tmp_allStopped = true;
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++) {
			if (ball[i].getStepX() != 0 || ball[i].getStepY() != 0) {
				tmp_allStopped = false;
				break;
			}
		}
		if (tmp_allStopped)// 所有球全停
		{
			time_to_hit = true;// 可以击球
		} else {
			time_to_hit = false;// 否则,不可击球
		}
	}

	public void goNext()
	{
		for (int i = 0; i < COUNT_OF_ALL_BALL; i++) {
			if (!ball[i].isPocketed()) {
				ball[i].setCurrentLocation(ball[i].getNextX(), ball[i]
						.getNextY());
			}
		}
	}

	public void draw() 
	{
		this.repaint();
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if (time_to_hit)
		{
			ball_label[0].setVisible(true);
//			if (ball_left <= 1) {
//				JOptionPane.showMessageDialog(this,"游戏结束","提示",JOptionPane.CANCEL_OPTION);
//				System.exit(1);
//			}
			double tmp_length = Math.hypot(mouseX - ball[0].getCurrentX(),mouseY - ball[0].getCurrentY());
			double tmp_cos = (ball[0].getCurrentX() - mouseX) / tmp_length;
			double tmp_sin = (ball[0].getCurrentY() - mouseY) / tmp_length;
			double tmp_stepX = 20d * tmp_cos;
			double tmp_stepY = 20d * tmp_sin;
			double tmp_drawLineX = ball[0].getCurrentX(), tmp_drawLineY = ball[0].getCurrentY();
			double tmp_lineX1, tmp_lineY1, tmp_lineX2, tmp_lineY2;

			if (!clicked) 
			{
				g.setColor(Color.red);
				for (int i = 0; i < 30; i++) {
					if ((ball[0].getCurrentX() + tmp_stepX * i) < 0) {
						tmp_drawLineX = -(ball[0].getCurrentX() + i * tmp_stepX);
					} else if ((ball[0].getCurrentX() + tmp_stepX * i) > WIDTH_OF_FLAT) {
						tmp_drawLineX = 2d * WIDTH_OF_FLAT
								- (ball[0].getCurrentX() + i * tmp_stepX);
					} else {
						tmp_drawLineX += tmp_stepX;
					}
					if ((ball[0].getCurrentY() + tmp_stepY * i) < 0) {
						tmp_drawLineY = -(ball[0].getCurrentY() + i * tmp_stepY);
					} else if ((ball[0].getCurrentY() + tmp_stepY * i) > HEIGHT_OF_FLAT) {
						tmp_drawLineY = 2d * HEIGHT_OF_FLAT
								- (ball[0].getCurrentY() + i * tmp_stepY);
					} else {
						tmp_drawLineY += tmp_stepY;
					}
					g.fillOval((int) (tmp_drawLineX - 2),
							(int) (tmp_drawLineY - 2), 4, 4);
				}
				g.setColor(Color.black);
				tmp_lineX1 = ball[0].getCurrentX() - tmp_stepX;
				tmp_lineY1 = ball[0].getCurrentY() - tmp_stepY;
				tmp_lineX2 = ball[0].getCurrentX() - tmp_stepX - 300d * tmp_cos;
				tmp_lineY2 = ball[0].getCurrentY() - tmp_stepY - 300d * tmp_sin;
				g.drawLine((int) tmp_lineX1, (int) tmp_lineY1,
						(int) tmp_lineX2, (int) tmp_lineY2);
				g.drawLine((int) tmp_lineX1 + 1, (int) tmp_lineY1 + 1,
						(int) tmp_lineX2, (int) tmp_lineY2);
				g.drawLine((int) tmp_lineX1 + 2, (int) tmp_lineY1 + 1,
						(int) tmp_lineX2, (int) tmp_lineY2);
			}
			else
			{
				System.out.println("clicked!!!!!!");
				ball[0].setPocketed(false);
				ball[0].setStep(stepXforHit * tmp_cos, stepYforHit * tmp_sin);
				time_to_hit = false;
				clicked = false;
			}

		} 
		else 
		{
			for (int i = 0; i < COUNT_OF_ALL_BALL; i++) 
			{
				if (ball[i].isPocketed()) 
				{
					ball_label[i].setVisible(false);
				}
				else
				{
					ball_label[i].setLocation(
							(int) (ball[i].getCurrentX() - RADIUS),
							(int) (ball[i].getCurrentY() - RADIUS));
				}
			}
		}
	}

	public static void main(String[] args) {
		new Billiards();
	}

}
最近下载更多
ffffwwrgch  LV1 4月28日
奈墨樱  LV1 2023年9月7日
wangjialiang1  LV17 2023年8月24日
huangzy  LV12 2023年4月6日
xiaoweitianshi001  LV1 2022年11月8日
迷迭香  LV10 2022年9月21日
hhhhedd  LV1 2022年6月14日
四季夏目  LV7 2022年3月22日
Darkic  LV2 2022年1月23日
王圆姣  LV1 2022年1月7日
最近浏览更多
ffffwwrgch  LV1 4月28日
奈墨樱  LV1 2023年9月7日
wangjialiang1  LV17 2023年8月24日
ann000  LV2 2023年7月2日
hougui  LV1 2023年6月20日
微信网友_6469256976748544  LV1 2023年5月10日
huangzy  LV12 2023年4月6日
leon96  LV1 2023年3月18日
xiaoweitianshi001  LV1 2022年11月8日
迷迭香  LV10 2022年9月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友