线程实现弹球

1.线程:

线程是彼此互相独立的、能独立运行的子任务,并且每个线程都有自己的调用栈。众所周知,每个java程序都至少有一个线程——主线程。当一个java程序启动时,JVM会创建主程序,并在该线程中调用程序的main()方法。线程,也可以理解为“程序内部一个独立的运行单位”。

2.线程的创建方法:

线程的创建有两种方式:

1.继承java.lang.Thread类

  public class TestThread extends Thread{

          //重写run方法

          public void run(){

 

          }

 }

 

2. 实现java.lang.Runnable接口。

   public class TestThreadimplements Runnable {

        public void run() {

        //重写

        }

   }

3.启动线程:

      线程的启动要调用start()方法

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class jframe extends JFrame {
	static jframe jf;

	public static void main(String[] args) {
		jf = new jframe();
		jf.initUI();
	}

	public void initUI() {
		this.setSize(600, 500);
		this.setDefaultCloseOperation(3);
		this.setLayout(new FlowLayout());
		this.setVisible(true);
		JButton btn = new JButton("开始");
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				thread td = new thread(jf);
				td.start();
			}
		});
		this.add(btn);
	}
}
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class thread extends Thread {
	JFrame jf;
	int X = 0; // X坐标
	int Y = 0; // Y坐标
	int moveX = 2; // X轴位移
	int moveY = 3; // Y轴位移
	int width;
	int height;
	Graphics g;

	public thread(JFrame jf) {
		this.jf = jf;
	}

	public void run() {
		init();
		while (true) {
			try {
				Thread.sleep(10);// 线程暂停10毫秒,减少CPU使用率
			} catch (Exception E) {
			}
			g.setColor(jf.getBackground());
			g.fillRect(X, Y, width, height);
			X = X + moveX;// 重新计算X、Y坐标
			Y = Y + moveY;
			// 碰到边界时改变递增值造成反弹效果
			if (X >= (width - 30)) {
				X = width - 30;
				moveX = -moveX;
			}
			if (X <= 0) {
				X = 0;
				moveX = -moveX;
			}
			if (Y >= (height - 30)) {
				Y = height - 30;
				moveY = -moveY;
			}
			if (Y <= 0) {
				Y = 0;
				moveY = -moveY;
			}
//			g.setColor(Color.WHITE);
//			g.fillRect(X, Y, 30, 30);
			g.setColor(Color.BLACK);
			g.fillOval(X, Y, 30, 30);
		}
	}

	public void init() {
		g = jf.getGraphics();
		width = jf.getWidth();
		height = jf.getHeight();
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; /** * 小球对象 * * @author yangenxiong [email protected] * @author Kelvin Mak [email protected] * @version 1.0 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br>Copyright (C), 2009-2010, yangenxiong * <br>This program is protected by copyright laws. */ public class Ball extends BallComponent { // 定义球的竖向速度 private int speedY = 10; // 定义弹球的横向速度 private int speedX = 8; // 定义是否在运动 private boolean started = false; // 定义是否结束运动 private boolean stop = false; /** * m 有参数构造器 * * @param panelWidth * int 画板宽度 * @param panelHeight * int 画板高度 * @param offset * int 位移 * @param path * String 图片路径 */ public Ball(int panelWidth, int panelHeight, int offset, String path) throws IOException { // 调用父构造器 super(panelWidth, panelHeight, path); // 设置y坐标 this.setY(panelHeight - super.getImage().getHeight(null) - offset); } /** * 设置横向速度 * * @param speed * int 速度 * @return void */ public void setSpeedX(int speed) { this.speedX = speed; } /** * 设置竖向速度 * * @param speed * int 速度 * @return void */ public void setSpeedY(int speed) { this.speedY = speed; } /** * 设置是否在运动 * * @param b * boolean * @return void */ public void setStarted(boolean b) { this.started = b; } /** * 设置是否结束运动 * * @param b * boolean * @return void */ public void setStop(boolean b) { this.stop = b; } /** * 返回横向速度 * * @return int 速度 */ public int getSpeedX() { return this.speedX; } /** * 返回竖向速度 * * @return int 速度 */ public int getSpeedY() { return this.speedY; } /** * 是否在运动 * * @return boolean 是否在运动 */ public boolean isStarted() { return this.started; } /** * 是否已经结束运动 * * @return boolean 是否已经结束运动 */ public boolean isStop() { return this.stop; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值