线程基础

package 游戏背景基础;


在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口

在Java中,类仅支持单继承,也就是说,当定义一个新的类的时候,它只能扩展一个外部类。这样,如果创建自定义线程类的时候是通过扩展 Thread类的方法来实现的,那么这个自定义类就不能再去扩展其他的类,也就无法实现更加复杂的功能。因此,如果自定义类必须扩展其他的类,那么就可以使用实现Runnable接口的方法来定义该类为线程类,这样就可以避免Java单继承所带来的局限性
下面是利用线程的方法编写的 小球游戏:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

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

public class Game {
private JFrame jf;
//定义数组
private ArrayList<Ball> list = new ArrayList<Ball>();

public void initUI(){
jf = new JFrame();
jf.setSize(600, 600);
jf.setDefaultCloseOperation(3);

jf.setLayout(new FlowLayout());
//添加按钮
JButton btn = new JButton("start");
jf.add(btn);
btn.addActionListener(l);

JButton btn2 = new JButton("pause");
jf.add(btn2);
btn2.addActionListener(l);

JButton btn3 = new JButton("resume");
jf.add(btn3);
btn3.addActionListener(l);

JButton btn4 = new JButton("stop");
jf.add(btn4);
btn4.addActionListener(l);

jf.setVisible(true);

while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
draw(jf.getGraphics());
}
}

//定义图片添加图片
ImageIcon bjIcon = new ImageIcon("img/backg.png");
ImageIcon personIcon = new ImageIcon("img/person.gif");
int x=0,y=0;

//画图
public void draw(Graphics g){
//利用缓冲区画图
BufferedImage buffer = new BufferedImage(
jf.getWidth(),
jf.getHeight(),
BufferedImage.TYPE_INT_RGB);
//在缓冲区画
Graphics gg = buffer.getGraphics();
//画布颜色
gg.setColor(Color.GREEN);
gg.fillRect(0, 0, jf.getWidth(), jf.getHeight());
//
gg.drawImage(bjIcon.getImage(), x, y, null);
gg.drawImage(bjIcon.getImage(), x+bjIcon.getIconWidth(), y, null);
x-=5;
if(x+bjIcon.getIconWidth() <= 0){
x = 0;
}

//
gg.drawImage(personIcon.getImage(), 200, 360, null);

//遍历每个球 画在缓冲区上
for(int i=0; i<list.size(); i++){
Ball ball = list.get(i);
ball.draw(gg);
}

//画
g.drawImage(buffer, 0, 0, null);
}

ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if("start".equals(cmd)){
Ball t = new Ball(jf, list);
t.start();

list.add(t);
}
if("pause".equals(cmd)){
for(int i=0; i<list.size(); i++){
Ball ball = list.get(i);
// ball.suspend();
ball.setPause(true);
}
}
if("resume".equals(cmd)){
for(int i=0; i<list.size(); i++){
Ball ball = list.get(i);
ball.setPause(false);
}
}
if("stop".equals(cmd)){
while(!list.isEmpty()){
Ball ball = list.remove(0);
ball.setStop(true);
}
}
}
};


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

}


package 游戏背景基础;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class Ball extends Thread{
private JFrame jf;
//定义属性
private int radius = 20;
private int x,y;
private int vx,vy;
private Color color;
//
private boolean pauseFlag = false;
private boolean stopFlag = false;
private Graphics g;
private ArrayList<Ball> list;
//球属性
public Ball(JFrame jf, ArrayList<Ball> list){
this.list = list;
this.jf = jf;
Random r = new Random();
vx = r.nextInt(5)+1;
vy = r.nextInt(5)+1;
radius = r.nextInt(20)+20;
color = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
}

public void setPause(boolean flag){
pauseFlag = flag;
}

public void setStop(boolean flag){
stopFlag = flag;
}

public void run(){
g = jf.getGraphics();
while(true){
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(pauseFlag == true)
continue;

// clear();

if(stopFlag)
return;

move();

// draw();
}
}

private void clear(){
g.setColor(jf.getBackground());
g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
}

public void draw2(Graphics g){
g.setColor(color);
g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
}

//填充方法
private void fillCircle(Graphics g, int x0, int y0, int r, Color color) {
g.setColor(color);
g.fillOval(x0 - r, y0 - r, 2*r, 2*r);
}

public void draw(Graphics g0){
Graphics2D g = (Graphics2D)g0;
// 设置
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// float alpha = 1.0f;


int r = this.radius;
int c = 10;//(255 - minColor)*2/r;
int x2 = x;
int y2 = y;
//
for(int i = 0; r > 1 && x-x2 < this.radius; i++){
// alpha -= 0.05;
// if(alpha < 0)
// alpha = 0;
// g.setComposite(AlphaComposite.SrcOver.derive(alpha));
fillCircle(g, x2, y2 , r, getColor(c*i));
r -= 2;
x2 -= 1;
}
// lastX = x;
// lastY = y;
}
//重新定义颜色
private Color getColor(int increase) {
int red2 = color.getRed()+increase;
int green2 = color.getGreen()+increase;
int blue2 = color.getBlue()+increase;
if(red2 >= 256)
red2 = 255;
if(green2 >= 256)
green2 = 255;
if(blue2 >= 256)
blue2 = 255;
return new Color(red2,green2,blue2);
}

int oldX,oldY;
private void move(){
//
oldX = x;
oldY = y;

//
x+=vx;
y+=vy;

if(x<0 || x > jf.getWidth()-2*radius){
vx = -vx;
}
if(y<0 || y > jf.getHeight()-2*radius){
vy = -vy;
}

for(int i=0; i<list.size(); i++){
Ball ball = list.get(i);
if(ball == this){
continue;
}
int xx = Math.abs(x-ball.x);
int yy = Math.abs(y-ball.y);
int len = (int)Math.sqrt(xx*xx+yy*yy);
if(len <= this.radius+ball.radius){
System.out.println("peng");
int temp = this.vx;
this.vx = ball.vx;
ball.vx = temp;

temp = this.vy;
this.vy = ball.vy;
ball.vy = temp;

x = oldX;
y = oldY;
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值