Java 如何在swing游戏中通知多线程事件
我应该做一个小游戏模拟。在这个游戏中有三个按钮。当用户点击“启动”按钮时,坦克和汽车将以90度角相互关闭;当用户点击“关闭”按钮时,坦克将向汽车投掷子弹 我做了一个模拟。坦克把子弹扔到车上,但当子弹撞到车上时,我不能这样做。我只需要增加坦克击中汽车多少次的分数 这是源代码Java 如何在swing游戏中通知多线程事件,java,multithreading,swing,Java,Multithreading,Swing,我应该做一个小游戏模拟。在这个游戏中有三个按钮。当用户点击“启动”按钮时,坦克和汽车将以90度角相互关闭;当用户点击“关闭”按钮时,坦克将向汽车投掷子弹 我做了一个模拟。坦克把子弹扔到车上,但当子弹撞到车上时,我不能这样做。我只需要增加坦克击中汽车多少次的分数 这是源代码 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public c
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Vehicle extends Thread {
private JPanel box;
private int XSIZE;
private int YSIZE;
private int time;
private int x;
private int y;
private int dx = 5;
private int dy = 5;
private int dim;
public Vehicle(JPanel b, int i) {
box = b;
this.dim = i;
if (i == 0) {
x = 0;
y = 100;
time = 1000;
XSIZE = 9;
YSIZE = 20;
} else {
time = 200;
y = box.getSize().height;
x = box.getSize().width / 2;
XSIZE = 6;
YSIZE = 10;
}
}
public void draw() {
Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void moveHorizontal() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setColor(Color.BLUE);
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
Dimension d = box.getSize();
if (x < 0) {
x = 0;
dx = -dx;
}
if (x + XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public JPanel getBox() {
return box;
}
public void setBox(JPanel box) {
this.box = box;
}
public void moveVertical() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
y += dy;
Dimension d = box.getSize();
if (y < 0) {
y = 0;
dy = -dy;
}
if (y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move(int i) {
if (i == 0) {
moveHorizontal();
} else {
moveVertical();
}
}
public int getYSIZE() {
return YSIZE;
}
public void setYSIZE(int ySIZE) {
YSIZE = ySIZE;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public int getXSIZE() {
return XSIZE;
}
public void setXSIZE(int xSIZE) {
XSIZE = xSIZE;
}
public void setY(int y) {
this.y = y;
}
public void run() {
try {
draw();
for (;;) {
move(dim);
sleep(time);
}
} catch (InterruptedException e) {
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Bullet extends Thread {
private JPanel box;
private int XSIZE = 3;
public int getXSIZE() {
return XSIZE;
}
public void setXSIZE(int xSIZE) {
XSIZE = xSIZE;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
private int YSIZE = 1;
private int x;
private int y;
private int dx = 3;
public Bullet(JPanel b, Vehicle tank, Vehicle car) {
box = b;
x = tank.getX() + tank.getXSIZE();
if (x >= tank.getBox().getSize().width / 2)
dx = -dx;
y = tank.getY() + tank.getYSIZE() / 2;
;
}
public void draw() {
Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void move() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setColor(Color.RED);
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
Dimension d = box.getSize();
if (x < 0) {
x = 0;
}
if (x + XSIZE >= d.width) {
x = d.width - XSIZE;
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}
public void run() {
try {
draw();
for (;;) {
move();
sleep(20);
}
} catch (InterruptedException e) {
}
}
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class Tank_Shut_Car_ThreadFrame extends JFrame {
private JPanel canvas;
private boolean isOn = false;
private Vehicle tank;
private Vehicle car;
private JLabel score;
public static int sc = 0;
public Tank_Shut_Car_ThreadFrame() {
setResizable(false);
setSize(600, 400);
setTitle("Tank Shut Car");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
canvas.setLayout(null);
score = new JLabel("0");
score.setBounds(527, 11, 36, 14);
canvas.add(score);
JLabel lblScore = new JLabel("score");
lblScore.setBounds(481, 11, 36, 14);
canvas.add(lblScore);
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!isOn) {
tank = new Vehicle(canvas, 0);
tank.start();
car = new Vehicle(canvas, 1);
car.start();
isOn = true;
}
}
});
addButton(p, "Shut", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (isOn) {
Bullet bullet = new Bullet(canvas, tank, car);
bullet.start();
score.setText("" + sc);
}
}
});
addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}
public void addButton(Container c, String title, ActionListener a) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(a);
}
}
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new Tank_Shut_Car_ThreadFrame();
frame.setVisible(true);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入javax.swing.JPanel;
公营车辆{
私人JPanel箱;
私有int-XSIZE;
私人住宅;
私人整数时间;
私人INTX;
私营企业;
私有整数dx=5;
私有整数dy=5;
私人智能交通系统;
公共车辆(JPanel b,int i){
box=b;
this.dim=i;
如果(i==0){
x=0;
y=100;
时间=1000;
XSIZE=9;
YSIZE=20;
}否则{
时间=200;
y=box.getSize()高度;
x=box.getSize().width/2;
XSIZE=6;
YSIZE=10;
}
}
公众抽签(){
Graphics g=box.getGraphics();
g、 圆角(x,y,x大小,y大小);
g、 处置();
}
公共空间(水平){
如果(!box.isVisible())
返回;
Graphics g=box.getGraphics();
g、 setColor(Color.BLUE);
g、 setXORMode(box.getBackground());
g、 圆角(x,y,x大小,y大小);
x+=dx;
维度d=box.getSize();
if(x<0){
x=0;
dx=-dx;
}
如果(x+XSIZE>=d.width){
x=d.width-XSIZE;
dx=-dx;
}
g、 圆角(x,y,x大小,y大小);
g、 处置();
}
公共JPanel getBox(){
返回框;
}
公共空立箱(JPanel箱){
this.box=box;
}
公共空间(垂直){
如果(!box.isVisible())
返回;
Graphics g=box.getGraphics();
g、 setXORMode(box.getBackground());
g、 圆角(x,y,x大小,y大小);
y+=dy;
维度d=box.getSize();
if(y<0){
y=0;
dy=-dy;
}
如果(y+y尺寸>=d高度){
y=d.高度-y尺寸;
dy=-dy;
}
g、 圆角(x,y,x大小,y大小);
g、 处置();
}
公共无效移动(int i){
如果(i==0){
水平移动();
}否则{
移动垂直();
}
}
public int getYSIZE(){
回归社会化;
}
公共无效设置大小(内部大小){
YSIZE=YSIZE;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
public int getXSIZE(){
返回XSIZE;
}
公共void setXSIZE(int-xSIZE){
XSIZE=XSIZE;
}
公共空间设置(整数y){
这个。y=y;
}
公开募捐{
试一试{
draw();
对于(;;){
移动(变暗);
睡眠(时间);
}
}捕捉(中断异常e){
}
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入javax.swing.JPanel;
公共类项目符号扩展线程{
私人JPanel箱;
私有int-XSIZE=3;
public int getXSIZE(){
返回XSIZE;
}
公共void setXSIZE(int-xSIZE){
XSIZE=XSIZE;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
私有内部规模=1;
私人INTX;
私营企业;
私有整数dx=3;
公共子弹(JPanel b、车辆油箱、车辆){
box=b;
x=tank.getX()+tank.getXSIZE();
如果(x>=tank.getBox().getSize().width/2)
dx=-dx;
y=tank.getY()+tank.getYSIZE()/2;
;
}
公众抽签(){
Graphics g=box.getGraphics();
g、 圆角(x,y,x大小,y大小);
g、 处置();
}
公开作废动议(){
如果(!box.isVisible())
返回;
Graphics g=box.getGraphics();
g、 setColor(Color.RED);
g、 setXORMode(box.getBackground());
g、 圆角(x,y,x大小,y大小);
x+=dx;
维度d=box.getSize();
if(x<0){
x=0;
}
如果(x+XSIZE>=d.width){
x=d.width-XSIZE;
}
g、 圆角(x,y,x大小,y大小);
g、 处置();
}
公开募捐{
试一试{
draw();
对于(;;){
move();
睡眠(20);
}
}捕捉(中断异常e){
}
}
}
导入java.awt.Container;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JLabel;
@抑制警告(“串行”)
公共级油箱\关闭\汽车\螺纹框架延伸JFrame{
私人JPanel帆布;
private=false;
私家车水箱;
私家车;
私人JLabel评分;
公共静态int sc=0;
公用油箱\关闭\汽车\螺纹框架(){
可设置大小(假);
设置大小(600400);
setTitle(“油罐车”);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
容器contentPane=getContentPane();
canvas=newjpanel();
添加(画布,“中心”);
canvas.setLayout(null);
分数=新的JLabel(“0”);
得分.挫折(527,11,36,14);
canvas.add(分数);
杰拉贝
public class GameEngine extends Thread {
public static final Object ASSET_LOCK = new Object();
private List<GameAsset> lstAssets;
private GameScreen screen;
public GameEngine(GameScreen screen) {
// Let the thread die when the JVM closes
setDaemon(true);
// Want to be below the UI thread (personal preference)
setPriority(NORM_PRIORITY - 1);
// A list of game assests
lstAssets = new ArrayList<GameAsset>(25);
// A reference to the screen
this.screen = screen;
// Add global key listener, this is simpler to the trying to attach a key listener
// to the screen.
Toolkit.getDefaultToolkit().addAWTEventListener(new EventHandler(), AWTEvent.KEY_EVENT_MASK);
}
public GameAsset[] getAssets() {
synchronized (ASSET_LOCK) {
return lstAssets.toArray(new GameAsset[lstAssets.size()]);
}
}
/*
* Allows for assets to be added
*/
public void addAsset(GameAsset asset) {
synchronized (ASSET_LOCK) {
lstAssets.add(asset);
}
}
@Override
public void run() {
while (true) {
try {
sleep(40);
} catch (InterruptedException ex) {
}
synchronized (ASSET_LOCK) {
GameAsset[] assets = lstAssets.toArray(new GameAsset[lstAssets.size()]);
for (GameAsset asset : assets) {
if (lstAssets.contains(asset)) {
asset.update(this, screen);
}
}
screen.repaint(new ArrayList<GameAsset>(lstAssets));
}
}
}
/**
* Allows the removal of an asset...
*/
public void removeAsset(GameAsset asset) {
synchronized (ASSET_LOCK) {
lstAssets.remove(asset);
}
}
/**
* Key event handling...
*/
protected class EventHandler implements AWTEventListener {
@Override
public void eventDispatched(AWTEvent event) {
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
synchronized (ASSET_LOCK) {
GameAsset[] assets = lstAssets.toArray(new GameAsset[lstAssets.size()]);
for (GameAsset asset : assets) {
if (lstAssets.contains(asset)) {
asset.processKeyPressed(GameEngine.this, screen, keyEvent);
}
}
}
} else if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
synchronized (ASSET_LOCK) {
GameAsset[] assets = lstAssets.toArray(new GameAsset[lstAssets.size()]);
for (GameAsset asset : lstAssets) {
if (lstAssets.contains(asset)) {
asset.processKeyReleased(GameEngine.this, screen, keyEvent);
}
}
}
}
}
}
}