package game;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class GameCanvas extends JPanel{
private static final long serialVersionUID = -1372862841511384090L;
private DestroyedPlanes game;
private boolean isMe;
private JLabel letter[] = new JLabel[10];
private JLabel num[] = new JLabel[10];
private Color chosed = new Color(153, 180, 209);
private Color unchosed = new Color(240, 240, 240);
public Plane[] planes = new Plane[4];
public int[][] planesCells ={
{ 0, 0, -1, 0, 0, 0, 0, -2, 0, 0},
{ 2, 2, 2, 2, 2, 7, 7, 7, 7, 7},
{ 0, 0, 2, 0, 0, 0, 0, 7, 0, 0},
{ 0, 2, 2, 2, 0, 0, 7, 7, 7, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, -3, 0, 0, 0, 0, -4, 0, 0},
{62, 62, 62, 62, 62, 67, 67, 67, 67, 67},
{ 0, 0, 62, 0, 0, 0, 0, 67, 0, 0},
{ 0, 62, 62, 62, 0, 0, 67, 67, 67, 0},
};
private Dimension vertical = new Dimension(200, 160);
private Dimension horizontal = new Dimension(160, 200);
public Image[][] cells = new Image[10][10];
private int xbefore = -1, ybefore = -1;
private Image
upPlane = new ImageIcon(getClass().getResource("image/UpPlane.png")).getImage(),
leftPlane = new ImageIcon(getClass().getResource("image/LeftPlane.png")).getImage(),
downPlane = new ImageIcon(getClass().getResource("image/DownPlane.png")).getImage(),
rightPlane = new ImageIcon(getClass().getResource("image/RightPlane.png")).getImage(),
head = new ImageIcon(getClass().getResource("image/head.png")).getImage(),
others = new ImageIcon(getClass().getResource("image/others.png")).getImage(),
empty = new ImageIcon(getClass().getResource("image/empty.png")).getImage(),
mark = new ImageIcon(getClass().getResource("image/mark.png")).getImage(),
markagain = new ImageIcon(getClass().getResource("image/markagain.png")).getImage();
private int ibefore, jbefore;
private Image
backImageMe = new ImageIcon(getClass().getResource("image/Me.jpg")).getImage(),
backImageYou = new ImageIcon(getClass().getResource("image/You.jpg")).getImage();
public GameCanvas(DestroyedPlanes dp, String YouOrMe){
this.game = dp;
if (YouOrMe.equals("Me"))
isMe = true;
else
isMe = false;
setLayout(null);
//加载标签
for (int i = 0; i < 10; i++){
letter[i] = new JLabel("" + (char)('A' + i));
letter[i].setBackground(unchosed);
letter[i].setOpaque(true);
letter[i].setBounds(360 - i * 40, 0, 40, 10);
letter[i].setVerticalAlignment(SwingConstants.CENTER);
letter[i].setHorizontalAlignment(SwingConstants.CENTER);
add(letter[i]);
}
for (int i = 0; i < 10; i++){
num[i] = new JLabel("" + i);
num[i].setBackground(unchosed);
num[i].setOpaque(true);
num[i].setBounds(400, 10 + i * 40, 10, 40);
num[i].setVerticalAlignment(SwingConstants.CENTER);
num[i].setHorizontalAlignment(SwingConstants.CENTER);
add(num[i]);
}
for (int i = 0; i < 4; i++){
planes[i] = new Plane();
}
//加载鼠标点击监听器
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if (isMe && game.isReadying() && !game.getMyReady() && !game.isPlaying()
&& e.getX() <= 400 && e.getY() >= 10){
int i= (e.getY() - 10) / 40;
int j= e.getX() / 40;
ibefore = i;
jbefore = j;
if (planesCells[i][j] != 0){
if (planesCells[i][j] < 0){
headnum = planesCells[i][j];
othersnum = i * 10 + j;
}
else{
othersnum = planesCells[i][j];
headnum = planesCells[othersnum / 10][othersnum % 10];
}
pressedChangePlaneLocation(i, j, planes[-1 - headnum].doPlane);
}
}
else if (game.isPlaying() && !game.isEnd()
&& e.getX() <= 400 && e.getY() >= 10){
int i= (e.getY() - 10) / 40;
int j= e.getX() / 40;
if (e.getButton() == MouseEvent.BUTTON3){
if (cells[i][j] == null){
cells[i][j] = mark;
repaint();
}
else if(cells[i][j] == mark){
cells[i][j] = markagain;
repaint();
}
else if(cells[i][j] == markagain){
cells[i][j] = null;
repaint();
}
}
else if (!isMe && game.isMyTurn() && cells[i][j] == null || cells[i][j] == mark || cells[i][j] == markagain){
choose(i, j);
game.getGameThread().iChoose(i, j);
game.setMyTurn(false);
}
}
}
});
//加载键盘监听器
setFocusable(true);
addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){
if (isMe && game.isReadying() && !game.getMyReady() && !game.isPlaying()){
if (e.getKeyCode() == KeyEvent.VK_A){
String NowDirection = planes[-1 - headnum].doPlane;
if (NowDirection.equals("UP")){
changePlaneLocation(ibefore, jbefore, "LEFT");
}
else if (NowDirection.equals("LEFT")){
changePlaneLocation(ibefore, jbefore, "DOWN");
}
else if (NowDirection.equals("DOWN")){
changePlaneLocation(ibefore, jbefore, "RIGHT");
}
else if (NowDirection.equals("RIGHT")){
changePlaneLocation(ibefore, jbefore, "UP");
}
}
else if (e.getKeyCode() == KeyEvent.VK_D){
String NowDirection = planes[-1 - headnum].doPlane;
if (NowDirection.equals("UP")){
changePlaneLocation(ibefore, jbefore, "RIGHT");
}
else if (NowDirection.equals("RIGHT")){
changePlaneLocation(ibefore, jbefore, "DOWN");
}
else if (NowDirection.equals("DOWN")){
changePlaneLocation(ibefore, jbefore, "LEFT");
}
else if (NowDirection.equals("LEFT")){
changePlaneLocation(ibefore, jbefore, "UP");
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
//加载鼠标移动监听器
addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (x >= 0 && x <=410 && y >= 0 && y <= 410){
if (x <= 400)
x = 9 - x / 40;
else
x = -1;
if (x != -1 && x != xbefore){
letter[x].setBackground(chosed);
if (xbefore != -1)
letter[xbefore].setBackground(unchosed);
xbefore = x;
}
if (y >= 10)
y = (y - 10) / 40;
else
y = -1;
if (y != -1 && y != ybefore){
num[y].setBackground(chosed);
if (ybefore != -1)
num[ybefore].setBackground(unchosed);
ybefore = y;
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if (isMe && game.isReadying() && !game.getMyReady() && !game.isPlaying()
&& e.getX() <= 400 && e.getY() >= 10){
int i= (e.getY() - 10) / 40;
int j= e.getX() / 40;
if (i != ibefore || j != jbefore){
ibefore = i;
jbefore = j;
draggedChangePlaneLocation(i, j, planes[-1 - headnum].doPlane);
}
}
}
});
}
private int headnum, othersnum;
private void presse
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java小游戏——“打飞机”源码 (153个子文件)
GameCanvas.class 21KB
SuggestTextField.class 8KB
DestroyedPlanes.class 6KB
GameThread.class 6KB
Menu.class 5KB
LabelPanel.class 4KB
ServerOrClientJFrame.class 3KB
GameJFrame.class 3KB
Help.class 3KB
PictruePanel.class 3KB
About.class 3KB
GameCanvas$1.class 2KB
GameCanvas$3.class 2KB
GameCanvas$2.class 2KB
About$4.class 2KB
About$3.class 2KB
About$6.class 2KB
About$1.class 2KB
About$2.class 2KB
About$5.class 2KB
ServerOrClientJFrame$1.class 1KB
ServerOrClientJFrame$3.class 1KB
ServerOrClientJFrame$2.class 1KB
PictruePanel$1.class 1KB
SuggestTextField$4.class 1KB
Menu$3.class 1KB
Menu$6.class 1004B
Menu$8.class 1000B
Menu$7.class 993B
Menu$1.class 961B
PictruePanel$2.class 934B
SuggestTextField$3.class 768B
SuggestTextField$2.class 768B
SuggestTextField$1.class 768B
Menu$5.class 739B
Menu$4.class 735B
Menu$10.class 715B
Menu$9.class 714B
GameJFrame$1.class 698B
Menu$2.class 671B
Plane.class 473B
.classpath 301B
stylesheet.css 2KB
inherit.gif 57B
SuggestTextField.html 29KB
GameJFrame.html 25KB
DestroyedPlanes.html 25KB
GameCanvas.html 22KB
ServerOrClientJFrame.html 21KB
Help.html 21KB
About.html 21KB
GameThread.html 16KB
serialized-form.html 15KB
index-5.html 11KB
Menu.html 10KB
DestroyedPlanes.html 10KB
index-14.html 10KB
Plane.html 8KB
index-10.html 8KB
index-7.html 8KB
help-doc.html 8KB
GameThread.html 8KB
index-12.html 7KB
package-summary.html 7KB
GameCanvas.html 7KB
overview-tree.html 7KB
package-tree.html 7KB
package-use.html 7KB
index-8.html 7KB
index-13.html 7KB
index-3.html 7KB
index-2.html 7KB
index-6.html 7KB
GameJFrame.html 7KB
index-4.html 7KB
Plane.html 7KB
Menu.html 7KB
index-1.html 6KB
index-9.html 6KB
index-11.html 6KB
index-15.html 6KB
index-16.html 6KB
constant-values.html 6KB
ServerOrClientJFrame.html 6KB
SuggestTextField.html 5KB
About.html 5KB
Help.html 5KB
deprecated-list.html 5KB
package-frame.html 2KB
allclasses-frame.html 2KB
allclasses-noframe.html 1KB
index.html 1KB
GameCanvas.java 45KB
SuggestTextField.java 7KB
Menu.java 6KB
GameThread.java 6KB
DestroyedPlanes.java 5KB
About.java 5KB
ServerOrClientJFrame.java 3KB
GameJFrame.java 2KB
共 153 条
- 1
- 2
在山
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页