⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 launch_missiles.java

📁 java程序中关于多媒体编程 既有文件说明 更有例子 希望大家可以一起学习交流
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      default :        say_pos_x = say_margin ;        break ;    }    Graphics bg = buffer.getGraphics() ;    bg.setFont(say_font) ;    if (say_style == SHADOW) {      bg.setColor(new Color(150,150,150)) ;      bg.drawString(s, say_pos_x+2,say_pos_y+1) ;    }    bg.setColor(Color.white) ;            // 在缓冲区内写字符串    bg.drawString(s, say_pos_x,say_pos_y) ;    say_pos_y += (int) (1.2 * fm.getHeight()) ;         // 提升相应的 y坐标值    bg.dispose() ;                              // 释放资源  }  public void set_say_mode(int m) {       // 设置显示模式    say_mode = m ;  }  public void set_say_style(int s) {        // 设置显示类型    say_style = s ;  }  public void set_say_font(Font f) {        // 设置显示所用的字体    say_font = f ;  }  public void set_say_margin(int margin) {        // 设置显示的空白边缘    say_margin = margin ;  }  public void set_say_pos(int x, int y) {     // 设置显示位置的坐标    say_pos_x = x ;    say_pos_y = y ;  }}/* --------------------  定义Piece基类 -------------------------- */class Piece {  Launch_Missiles a ;  int px,py ; // 定义新位置的x、y坐标  int opx,opy ; // 定义旧位置的x、y坐标  int w,h ; // 定义宽度,高度  int vx,vy ; // 定义x和y方向的速度  Color c ; // 定义颜色  boolean active = false ;  Image img = null ;  public void set_pos(int x, int y) {    // 设定位置    px = opx = x ;    py = opy = y ;  }  public void set_vel(int x,int y) {  // 设置速度    vx = x ;    vy = y ;  }  public void set_size(int x,int y) {    // 设置高度和宽度    w = x ;    h = y ;  }  public void set_color(Color c) {    // 设置颜色    this.c = c ;  }  public void set_draw_rectangles(Rectangle o, Rectangle n) {    // 设置绘制区域    int sh = a.window_size.height ;    int x  = px - w/2 ;    int y  = (sh - py) - h/2 ;    int ox = opx - w/2 ;    int oy = (sh - opy) - h/2 ;    o.setBounds(ox,oy,w,h);    n.setBounds(x,y,w,h) ;  }  public boolean active() {    // 获取active属性值    return active ;  }  public void active(boolean s) {    // 设置active属性值    active = s ;  }  public boolean collision(Piece p) {    // 物体间的碰撞的监测    int dpx = Math.abs(px - p.px) ;    int dpy = Math.abs(py - p.py) ;    if ((dpx < (Math.max(w/2,p.w/2))+1) && (dpy < (Math.max(h/2,p.h/2)+1)))      return true ;    return false ;  }  public void draw() {    // 绘制对象    set_draw_rectangles(a.paint_area, a.new_area) ;      // 设置绘制的区域    Graphics bg = a.buffer.getGraphics() ;      // 绘制缓冲区    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    a.buf_g.setColor(c);      // 填充缓冲区    a.buf_g.fillRect(a.new_area.x, a.new_area.y, w, h);    a.paint_area.add(a.new_area) ;      // 使用新的区域    Graphics g = a.getGraphics() ;      // 将缓冲绘制到屏幕上    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }  public void erase() {    // 清除    set_draw_rectangles(a.paint_area, a.new_area) ;      // 设置绘制的区域    a.paint_area.add(a.new_area) ;      // 使用新的区域    Graphics bg = a.buffer.getGraphics() ;      // 将背景幕拷到缓冲中    bg.clipRect(a.paint_area.x, a.paint_area.y, a.paint_area.width, a.paint_area.height);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    Graphics g = a.getGraphics() ;      // 将缓冲绘制到屏幕上    g.clipRect(a.paint_area.x,a.paint_area.y,a.paint_area.width,a.paint_area.height);    g.drawImage(a.buffer,0,0, a);    g.dispose() ;  }}/* --------------------  定义导弹发射架类 -------------------------- */class Launcher extends Piece {  public Launcher (Launch_Missiles a) {    this.a = a ;      // 初始化导弹发射架属性值    w  = 12 ;    h  = 22 ;    px = opx = a.window_size.width/2 ;    py = opy = w/2+1 ;    active = true ;    img = a.missile ;  }  public void move() {    // 移动导弹发射架    opx = px ;    opy = py ;    int dx  = a.mouse_x - px ;    int abs_dx = Math.abs(dx) ;    int step = 1 ;    if (abs_dx > 10)      step = 5 ;    else if (abs_dx > 1)      step = abs_dx/2 ;    if (dx != 0) {      px += step*(dx/abs_dx) ;      if (px < w/2)        px = w/2 ;    else if (px > (a.window_size.width - w/2))      px = a.window_size.width - w/2 ;    }  }  public boolean has_moved() {    // 判断导弹发射架是否移动    if ((px - opx) != 0)      return true ;    return false ;  }  public void draw() {    // 绘制导弹发射架    set_draw_rectangles(a.paint_area, a.new_area) ;    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    if (a.M.active()) {      // 根据导弹的active属性值进行相应的绘制      a.buf_g.setColor(c);        // 导弹飞行时,发射架外观为一实心矩形      a.buf_g.fillRect(a.new_area.x, a.new_area.y, w, h);    }    else {        // 否则,发射架外观为一竖立的导弹      bg = a.buffer.getGraphics() ;      bg.clipRect(a.new_area.x, a.new_area.y, w, h);      bg.drawImage(img,a.new_area.x,a.new_area.y,a) ;      bg.dispose() ;    }    a.paint_area.add(a.new_area) ;      // 使用新的区域    Graphics g = a.getGraphics() ;      // 将缓冲绘制到屏幕上    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }}/* --------------------  定义导弹Missile类 -------------------------- */class Missile extends Piece {  public Missile (Launch_Missiles a) {    this.a = a ;      // 初始化导弹属性值    px = opx = 0 ;    py = opy = 0 ;    vx = 0 ;    vy = 7 ;    w  = 12 ;    h  = 22 ;    active = false ;    img = a.missile ;  }  public void move() {    // 移动对象    opx = px ;    opy = py ;    px = a.L.px ;    int dx = px - opx ;    int nvy = vy*vy - dx*dx ;    if (nvy > 0) nvy = (int) Math.sqrt(nvy) ;    if (nvy < 1) nvy = 1 ;    py += nvy ;    if (py > a.window_size.height + 2*h) active = false ;  }  int seq  = 0 ;  public void draw() {    // 绘制导弹    set_draw_rectangles(a.paint_area, a.new_area) ;      // 设置绘制区域    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    seq = ++seq % 1 ;    int dx = px - opx ;    seq = 0 ;    if (dx > 0)      seq = 1 ;    else if (dx < 0)      seq = 2 ;    bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x, a.new_area.y, w, h);    bg.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bg.dispose() ;    a.paint_area.add(a.new_area) ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }}/* --------------------  定义飞行物Flyer类 -------------------------- */class Flyer extends Piece {  public Flyer (Launch_Missiles a) {    this.a = a ;      // 初始化飞行物属性值    vx = (Math.random() > 0.5 ? 1 : -1) ;    vy = -2 ;    w  = 20 ;    h  = 8 ;    int aw = a.window_size.width ;    px = opx =  (int) (w/2+1 + (aw-w-2)* Math.random()) ;    py = opy = a.window_size.height + h/2 + 1  ;    active = true ;    img = a.ufostrip ;  }  public void move() {    // 移动飞行物    opx = px ;    opy = py ;    px += vx ;    py += vy ;    if (py < -h/2) active = false ;    if ((px <= w/2) || (px >= (a.window_size.width - w/2)) ||        (Math.random() > 0.96)) {      vx = -vx ;    }  }  int seq  = 0 ;  int seq2 = 0 ;  public void draw() {    // 绘制飞行物    set_draw_rectangles(a.paint_area, a.new_area) ;    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    if ((++seq2 % 4) == 0) seq = ++seq % 4 ;    bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x, a.new_area.y, w, h);    bg.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bg.dispose() ;    a.paint_area.add(a.new_area) ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }}/* --------------------  定义爆炸Explosion类 -------------------------- */class Explosion extends Piece {  public Explosion (Launch_Missiles a, int x, int y) {    this.a = a ;      // 初始化爆炸对象属性值    w  = 30 ;    h  = 30 ;    px = opx = x ;    py = opy = y ;    active = true ;    img = a.missile_explosion ;  }  int seq  = 0 ;  int seq2 = 0 ;  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    Graphics bkd_g = a.backdrop.getGraphics();    bkd_g.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bkd_g.drawImage(a.bgimg,0,0,a.window_size.width,a.window_size.height,a) ;    if ((++seq2 % 4) == 0) seq = ++seq % 5 ;    if (seq == 4) active = false ;    bkd_g.clipRect(a.new_area.x, a.new_area.y, w, h);    bkd_g.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bkd_g.dispose() ;    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x,a.new_area.y,w,h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }  public void erase() {    // 清除爆炸    set_draw_rectangles(a.paint_area, a.new_area) ;    Graphics bkd_g = a.backdrop.getGraphics();    bkd_g.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bkd_g.drawImage(a.bgimg,0,0,a.window_size.width,a.window_size.height,a) ;    bkd_g.dispose() ;    super.erase() ;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -