package start;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class StartFrame extends JFrame{
/**
*
*/
public int x;
public int y;
public static StartFrame startFrame;
private static final long serialVersionUID = 1L;
public StartFrame(){
//設置窗體大小
this.setSize(400, 654);
//設置窗口居中顯示
//this.setLocationRelativeTo(null);
this.setLocation(150, 50);
//設置任務欄圖標
this.setIconImage(new ImageIcon("image/icon.jpg").getImage());
//去掉窗體的自帶邊框
this.setUndecorated(true);
StartPanel sp = new StartPanel();
this.add(sp);
this.setVisible(true); //默認窗體可見 放在 this.add(sp);後面
//啟動線程
Thread th = new Thread(sp);
th.start();
}
public static void main(String[] args) {
startFrame = new StartFrame();
}
}
package start;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import enemy.Enemy;
import enemy.EnemyMoveThread;
public class StartPanel extends JPanel implements Runnable {
/**
* 通過鼠標來控制MouseMontionListener
* 重寫mouseMovie() ——————測試語句
* 給面板添加監聽器
* panel.addMouseMontionListener(面板);
* 點關閉圖標關閉
* 點擊縮小圖標
* 實現MouseListener接口
* 重寫
*/
private static final long serialVersionUID = 1L;
int x;
StartPanel sp;
Enemy e;
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(new ImageIcon("image/startback.png").getImage(), 0, 0, this);
g.drawImage(new ImageIcon("image/start_aircraft.png").getImage(), x,450, this);
}
public void run() {
// TODO Auto-generated method stub
while (true) {
x++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (x > 400) {
// 隱藏StartFrame的窗體
StartFrame.startFrame.dispose();
/*
* 獲得左上角的坐標 int x = StartFrame.startFrame.getLocation().x;int y
* = StartFrame.startFrame.getLocation().y;
*/
// 創建新的PlaneFrame窗口
new PlaneFrame();
break;
}
repaint();
}
}
}
package start;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import enemy.Enemy;
import enemy.EnemyMoveThread;
/**
創建新的面板
封裝低級Enemy xy score image speed isLive panel;
*/
public class PlaneFrame extends JFrame implements Runnable{
/*
public PlaneFrame(){
this.setSize(400, 654);
//x y?
this.setLocation(150,50);
this.setIconImage(new ImageIcon("image/icon.jpd").getImage());
this.setUndecorated(true);
pp = new PlanePanel(this);
this.add(pp);
pp.addMouseMotionListener(pp); //第一個pp代表面,,第二個pp代表監聽器對象
//給面板添加監聽器對象
pp.addMouseListener(pp);
EnemyMoveThread emt = new EnemyMoveThread(e);
emt.start();
this.setVisible(true);
}
public static void main(String[] args) {
planeFrame = new PlaneFrame();
}
public void run() {
//如果英雄機存活,創建敵機
while(pp.hero_isLive){
//x, y, speed, icon, panel
/*
* Random r = new Random();
/
ImageIcon icon = new ImageIcon("image/shoot0_0.png");
//大類型的數據不能直接賦值給小類型的數據,需要強制性轉換
int x = (int) (Math.random()(pp.getWidth()-icon.getIconWidth()));
int y = -icon.getIconHeight();
int speed = 10;
//創建敵機 添加集合之中
Enemy e = new Enemy(x,y,speed,icon,pp);
//將敵機添加到集合中
pp.vs.add(e);
//給敵機綁定運動的線程
EnemyMoveThread th = new EnemyMoveThread(e);
th.start();
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
package start;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import enemy.Enemy;
public class PlanePanel extends JPanel implements MouseListener,
MouseMotionListener {
// 定義布爾類型isLive(存活)
private boolean isLive = true;
int x;
int hero_x = 200;
int hero_y = 500;
boolean hero_isLive = true;
PlaneFrame f;
ImageIcon icon = new ImageIcon("Image/hero.png");
ImageIcon min = new ImageIcon("image/min.png");
// 定義一個集合存放敵機
// Vector集合避免線程異步的問題 線程安全的一個線程
public Vector<Enemy> vs = new Vector<Enemy>();
// 通 過 構造方法 傳值
public PlanePanel(PlaneFrame planeFrame) {
this.f = planeFrame;
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(new ImageIcon("image/backmain.png").getImage(), 0, 0, this);
g.drawImage(new ImageIcon("image/min.png").getImage(), 325, 3, this);
g.drawImage(new ImageIcon("image/close.png").getImage(), 360, 3, this);
g.drawImage(new ImageIcon("image/hero.png").getImage(), hero_x, hero_y,
this);
for(int i = 0;i<vs.size();i++){
Enemy e = vs.get(i);
e.drawEnemy(g);
}
}
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if (hero_isLive) {
int x = e.getX(); // 得到鼠標的x坐標
int y = e.getY(); // 得到鼠標的y坐標
// 根據鼠標的坐標與英雄機的寬和高計算英雄機的坐標
hero_x = x - icon.getIconWidth() / 2;
hero_y = y - icon.getIconHeight() / 2;
if (hero_x <= 400 - 55 && hero_y >= 31 && hero_y <= 650 - 90
&& hero_x >= -40) {
this.repaint();
}
}
//
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int x = e.getX();
int y = e.getY();
if (x > 325 && x < 325 + min.getIconWidth() && y > 3
&& y < min.getIconHeight()) {
// 縮小窗體
f.setState(JFrame.ICONIFIED);
} else if (x > 360 && x < 360 + 31 && y > 3 && y < 3 + 31) {
System.exit(0); //
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed1(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
package enemy;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import start.PlanePanel;
public class Enemy {
int x;
int y;
int speed;
int score = 100;
ImageIcon icon;
boolean isLive = true;
PlanePanel panel;
public Enemy(){
}
public Enemy(int x, int y, int speed, ImageIcon icon, PlanePanel panel) {
super();
this.x = x;
this.y = y;
this.speed = speed;
this.icon = icon;
this.panel = panel;
}
//畫敵機的方法
public void drawEnemy(Graphics g){
if(isLive){
g.drawImage(icon.getImage(),x,y,panel);
}
}
public void move(){
y += speed;
//如果敵機飛出面板,刪除該敵機,並且isLive設置成false
if(y>panel.getHeight()){
isLive = false;
panel.vs.remove(this);
}
panel.repaint();
}
}
package enemy;
//封裝敵機運動的線程類
public class EnemyMoveThread extends Thread {
// 創建成員變量e
Enemy e;
public EnemyMoveThread(Enemy e) {
this.e = e;
}
@Override
public void run() {
super.run();
while (e.isLive) {
//
e.move();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
小型DBMS(c實現)
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!