主题:[讨论]应用
buaacse3606
[专家分:0] 发布于 2007-11-25 00:02:00
本人有四个JAVA程序(见上传文件),希望做出一个界面,将Clock程序放在界面上,并设置三个按钮“计算”,“益智”,“游戏”,当鼠标点击按钮时,分别运行Caculator,blackvswhite,ufo_attack.
初学JAVA,希望各位大侠不令赐教!!!
http://upload.programfan.com/upfile/20071125142571.rar
最后更新于:2007-11-25 14:27:00
回复列表 (共23个回复)
11 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:07:00
(7)
public synchronized void paint(Graphics g)
{
if( !imageTracker.checkAll() )
{
g.setColor( getForeground() );
g.drawString( "Loading images", 50, 50 );
}
else
{
int num2;
int num3;
int num4;
int num5;
int num6;
buttonHeight = button.getSize().height + button.getLocation().y * 2;
getFontMetrics( f );
num2 = getSize().width - fontHeight * 3 - buttonHeight;
num3 = getSize().height - fontHeight * 3 - buttonHeight;
if( num2 != boardSize )
{
if( num2 > num3 )
boardSize = num3 / DIM * DIM;
else
boardSize = num2 / DIM * DIM;
}
if( boardSize > 400 )
boardSize = 400;
offScrGraphics.setColor(getBackground() );
offScrGraphics.fillRect( 0, 0, getSize().width, getSize().height );
offScrGraphics.setColor( Color.white );
offScrGraphics.fillRect( 0, 0, boardSize, boardSize );
num4 = boardSize / DIM;
offScrGraphics.setColor( Color.red );
for( num5 = 0; num5 < DIM; ++num5 )
{
offScrGraphics.drawLine( 0, num5 * num4, boardSize - 1, num5 * num4 );
for( num6 = 0; num6 < DIM; ++num6 )
{
offScrGraphics.drawLine( num6 * num4, 0, num6 * num4, boardSize - 1 );
if( grid[num5 * DIM + num6] == 2 )
offScrGraphics.drawImage( redImage, num6 * num4 + 1, num5 * num4 + 1, num4 - 2, num4 - 2, this );
else if( grid[num5 * DIM + num6] == 3 )
offScrGraphics.drawImage( blueImage, num6 * num4 + 1, num5 * num4 + 1, num4 - 2, num4 - 2, this );
}
}
offScrGraphics.drawLine( boardSize - 1, 0, boardSize - 1, boardSize - 1 );
offScrGraphics.drawLine( 0, boardSize - 1, boardSize - 1, boardSize - 1 );
offScrGraphics.setColor( getForeground() );
if( illegalMove )
offScrGraphics.drawString( "下子非法,请重新下子!", button.getLocation().x, boardSize + fontAscent );
else if( blueNotMoving )
offScrGraphics.drawString( "Computer Pass", button.getLocation().x, boardSize + fontAscent );
offScrGraphics.drawImage( redImage, button.getLocation().x, boardSize + fontHeight * 2 - fontAscent, fontAscent, fontAscent, this );
offScrGraphics.drawString( " 玩家: " + (String.valueOf( redScore )), button.getLocation().x + fontAscent, boardSize + fontHeight * 2 );
offScrGraphics.drawImage( blueImage, button.getLocation().x, boardSize + fontHeight * 3 - fontAscent, fontAscent, fontAscent, this );
offScrGraphics.drawString( " 电脑: " + (String.valueOf( blueScore )), button.getLocation().x + fontAscent, boardSize + fontHeight * 3 );
offScrGraphics.setColor( Color.white );
num6 = 0;
12 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:08:00
(8)
if( animating )
{
num6 = num4 - 2;
while( num6 > 0 )
{
int num7;
for( num7 = 0; num7 < DIM; ++num7 )
{
int num8;
for( num8 = 0; num8 < DIM; ++num8 )
{
if( grid[num7 * DIM + num8] == 5 )
{
offScrGraphics.fillRect( num8 * num4 + 1, num7 * num4 + 1, num4 - 1, num4 - 1 );
offScrGraphics.drawImage( blueImage, num8 * num4 + 1 + num4 / 2 - num6 / 2, num7 * num4 + 1 + num4 / 2 - num6 / 2, num6, num6, this );
if( num6 <= 2 )
grid[num7 * DIM + num8] = 2;
}
else if( grid[num7 * DIM + num8] == 4 )
{
offScrGraphics.fillRect( num8 * num4 + 1, num7 * num4 + 1, num4 - 1, num4 - 1 );
offScrGraphics.drawImage( redImage, num8 * num4 + 1 + num4 / 2 - num6 / 2, num7 * num4 + 1 + num4 / 2 - num6 / 2, num6, num6, this );
if( num6 <= 2 )
grid[num7 * DIM + num8] = 3;
}
}
}
g.drawImage( offScrImage, button.getLocation().x, buttonHeight, this );
num6 -= 3;
try
{
Thread.sleep( 50L );
continue;
}
catch( Exception unused9 )
{
continue;
}
}
animating = false;
paint( g );
}
if((redScore+blueScore)==DIM*DIM )
{
if(redScore!=blueScore)
offScrGraphics.drawString((redScore>blueScore)?"You Win!":"You Lose!",button.getLocation().x, boardSize + fontAscent);
else
offScrGraphics.drawString("No One Win!",button.getLocation().x, boardSize + fontAscent);
}
g.drawImage( offScrImage, button.getLocation().x, buttonHeight, this );
}
}
13 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:08:00
(9)
public synchronized void update(Graphics g)
{
paint( g );
}
public void actionPerformed(ActionEvent e)
{
newGame();
}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public synchronized void mouseReleased(MouseEvent e)
{
int x=e.getX();
int y=e.getY();
if( imageTracker.checkID( 0, true ) )
{
int num4 = (x - button.getLocation().x) * DIM / boardSize;
int num5 = (y - buttonHeight) * DIM / boardSize;
if( num5 < DIM && num4 < DIM && grid[num5 * DIM + num4] == 0 )
{
illegalMove = false;
if( isGood( num5, num4, 2, 3 ) <= 0 )
{
illegalMove = true;
paint(getGraphics() );
return;
}
else
{
grid[num5 * DIM + num4] = 2;
change( num5, num4, 2, 3, 5 );
checkScore();
paint( getGraphics() );
try
{
Thread.sleep( 1000L );
}
catch( Exception unused6 )
{
}
blueMove();
checkScore();
paint( getGraphics() );
}
}
else if( num5 < DIM && num4 < DIM && grid[num5 * DIM + num4] == 3 )
{
illegalMove = true;
paint( getGraphics() );
return;
}
}
return;
}
}
14 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:10:00
ufo(1)
import java.awt.*;
import java.applet.*;
import java.util.*;
//将类UFO_Attack转换为线程,并实现Runnable接口
public class UFO_Attack extends Applet implements Runnable {
Image buffer = null ; // 临时图像缓冲
Image backdrop = null ; // 背景幕
Image bgimg = null ; // 原背景幕
Image ufostrip = null ; // UFO 序列图
Image missile = null ; // 导弹序列图
Image missile_explosion = null ; // 导弹爆炸序列图
MediaTracker tracker = null ;//媒体跟踪器,用来监测图像的装载
Graphics buf_g = null ; // 缓冲中的图像对象
Graphics bkd_g = null ; // 背景幕的图像对象
Dimension window_size = null;//窗口尺寸
Font font ;
Font font_s ; //显示字的字体
AudioClip explosion = null ; //爆炸声
AudioClip newufo = null ; //新的UFO出现时发出的声音
AudioClip missile_launch = null ; //导弹发射的声音
Thread game = null ; //程序的主线程
boolean game_over = true ; //用来判断游戏结束与否
int mouse_x = 100 ; //鼠标的X坐标,用来控制导弹和发射架的移动
Rectangle paint_area = new Rectangle() ; //对象出现的区域
Rectangle new_area = new Rectangle() ; //对象即将出现的区域
Launcher L = null ; //定义一个导弹发射架
Missile M = null ; //定义一个导弹
Vector UV = new Vector() ; //定义UFO向量,即一个UFO集合
Vector EV = new Vector() ; //定义爆炸向量,即一个爆炸集合
int NU = 1 ; //UFO的数目
int score = 0 ; //玩家所得分数
// 相应对象的颜色设置
Color gunColor;
Color mColor;
Color ufoColor;
Color scoreColor;
Color bgColor;
//UFO_Attack类的初始化
public void init() {
System.out.println("UFO Attack: A game by Sergio Fanchiotti") ;
tracker = new MediaTracker(this) ; //媒体跟踪器监测图像装载的情况
//图片的装载
bgimg = getImage(this.getCodeBase(),"bgimg.gif") ;
tracker.addImage(bgimg,0) ;
ufostrip = getImage(this.getCodeBase(),"ufostrip.gif") ;
tracker.addImage(ufostrip,0) ;
missile = getImage(this.getCodeBase(),"missile.gif") ;
tracker.addImage(missile,0) ;
missile_explosion = getImage(this.getCodeBase(),"explosionstrip.gif") ;
tracker.addImage(missile_explosion,0) ;
font = new Font("Helvetica", Font.BOLD, 24) ;
font_s = new Font("Helvetica", Font.BOLD, 14) ; //显示字的字体设置
// 设置所需的颜色
bgColor = new Color(0,0,128);
gunColor = new Color(0,88,0);
mColor = new Color(255,255,255);
ufoColor = new Color(255,0,0);
scoreColor = new Color(0,0,255);
}
15 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:14:00
ufo(2)
public void start() {
// 使用十字型光标
getFrame(this).setCursor(Frame.CROSSHAIR_CURSOR) ;
//获取窗口的尺寸
window_size = size();
//生成缓冲区
buffer = null;
buffer = createImage(window_size.width, window_size.height);
backdrop = null;
backdrop = createImage(window_size.width, window_size.height);
//用背景色来填充缓冲区
buf_g = buffer.getGraphics();
buf_g.setColor(bgColor);
buf_g.fillRect(0, 0, window_size.width, window_size.height);
// 显示初始化信息
set_say_font(font) ;
set_say_mode(CENTER) ;
set_say_style(SHADOW) ;
say("UFO",10,80) ;
say("ATTACK") ;
set_say_font(font_s) ;
set_say_style(NORMAL) ;
say("") ;
say("Click to start") ;
say("a game") ;
//将缓冲绘制到屏幕上
Graphics g = getGraphics() ;
g.drawImage(buffer,0,0,this) ;
// 初始化导弹发射架
mouse_x = window_size.width/2 ;
L = new Launcher(this) ;
L.set_color(gunColor) ;
// 初始化导弹
M = new Missile(this) ;
M.set_color(mColor) ;
// 加载声音文件
if (explosion == null)
explosion = getAudioClip(getCodeBase(),"explosion.au") ;
if (newufo == null)
newufo = getAudioClip(getCodeBase(),"sonar.au") ;
if (missile_launch == null)
missile_launch = getAudioClip(getCodeBase(),"rocket.au") ;
game_over = true ;
//声音播放
newufo.play() ;
missile_launch.play() ;
explosion.play() ;
}
//停止运行函数
public void stop() {
// 如果线程正在运行,强行令其停止
if (game != null) {
game.stop() ;
game = null ; // and eliminate the thread
}
// 重新设置光标形状
getFrame(this).setCursor(Frame.DEFAULT_CURSOR) ;
}
// 游戏主线程的执行函数
public void run() {
// 定义本地变量和对象
UFO U ;
Explosion E ;
long count = 0 ;
long ti = 0 ;
// 等待图片装载完毕
Graphics g = getGraphics() ;
g.setColor(Color.red) ;
g.drawString("Starting Game...", 20,20) ;
16 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:20:00
ufo(3)
while (tracker.checkAll(true) == false) {
if ((ti++ % 2) == 0)
g.setColor(Color.red) ;
else
g.setColor(Color.green) ;
g.drawString("*", 10,22) ;
try {Thread.sleep(50);} catch (InterruptedException e) { } ;
//装载超时时强行退出
if (ti > 1000) break ;
}
//捕捉获取图片时的错误信息
if (tracker.isErrorAny()) {
showStatus("Error getting images") ;
return ;
}
showStatus("Loading completed") ;//装载成功
g.dispose() ;
//绘制背景幕的缓冲区
buf_g = backdrop.getGraphics();
buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;
// 将背景幕的缓冲绘制到屏幕上
buf_g = getGraphics();
buf_g.drawImage(backdrop,0,0,this) ;
// 绘制缓冲区
buf_g = buffer.getGraphics();
buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;
//重新绘制
repaint() ;
// 显示玩家得分数
display_score() ;
//绘制导弹发射架
L.draw() ;
showStatus("UFO ATTACK") ;
// 事件循环
for (;;) {
ti = System.currentTimeMillis() ;
// 如果有多余的UFO飞行空间的话可增加一架UFO
if ((UV.size() < NU) &&
(Math.random() > (UV.size() == 0 ? 0.90 : 0.98))) {
newufo.play() ; // 播放警报声
U = new UFO(this) ;
U.set_color(ufoColor) ;
// 在相应条件下提高UFO的下降速度
if (score > 10 && Math.random() > 0.7) U.vy -= 1 ;
// 在UFO向量中增加一名成员
UV.addElement(U) ;
}
// 在背景幕上绘制爆炸画面,结束后将其清除
for (int j=EV.size()-1; j>=0 ; --j) {
E = (Explosion) EV.elementAt(j) ;
if (E.active) {
// 如果爆炸出现就进行其画面的绘制
E.draw() ;
}
else {
// 结束后从背景幕上清除,并从爆炸向量中删除
E.erase() ;
EV.removeElementAt(j) ;
}
}
//移动导弹发射架
L.move() ;
//如果导弹存在,移动导弹
if (M.active() == true) M.move() ;
//移动每个UFO
for (int i=0; i < UV.size(); ++i) {
U = (UFO) UV.elementAt(i) ;
U.move() ;
}
//监察UFO与导弹之间的碰撞
for (int i=(UV.size()-1); i >=0 ; --i) {
U = (UFO) UV.elementAt(i) ;
if (U.active() && M.active() && U.collision(M)) {
++score ; //增加玩家的得分
explosion.stop() ;
display_score() ;
explosion.play() ;
17 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:21:00
ufo(4)
//每击落10架UFO后便增加UFO的最大出现数目,直到数目为5
if ((NU < 5) && (score % 10) == 1) ++NU ;
// 碰撞发生后,将导弹从背景幕上清除,并使其active属性为false
M.active(false) ;
M.erase() ;
//将被击中的UFO从背景幕上清除,并使其active属性为false
U.active(false) ;
U.erase() ;
//显示爆炸的场面
E = new Explosion(this,U.px,U.py) ;
//在爆炸向量中添加一员
EV.addElement(E) ;
}
// 如果UFO没有被击中,则显示出来,否则,将其从UFO向量中删除
if (U.active())
U.draw() ;
else
UV.removeElementAt(i) ;
//如果有一个UFO成功着陆则玩家失败
if ((U.py - U.h/2) <=0) {
game_over = true ;
display_game_over() ;
return ;
}
}
//如果导弹发射架移动了,重画发射架
if (L.has_moved() || ((M.py-M.h) < (L.py+L.h)) || (! M.active()) )
L.draw() ;
//如果导弹的active属性值为true,则重画导弹
if (M.active() == true) M.draw() ;
// 万一CPU速度太快,要使循环维持在20ms以上
ti = System.currentTimeMillis() - ti ;
ti = 20 - ti ;
ti = ti > 0 ? 10 + ti : 10 ;
Thread.yield() ;
//处理线程sleep函数的异常
try {Thread.sleep(ti);}
catch (InterruptedException e) { } ;
// 每100次循环重新绘制一次
if ((count = ++count % 500) == 0) {
repaint() ;
}
}
}
//显示玩家的得分
public void display_score() {
Graphics bkd_g = backdrop.getGraphics();
bkd_g.clipRect(window_size.width/2, 0, window_size.width/2, 40);
bkd_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;
bkd_g.setColor(Color.red) ;
bkd_g.setFont(font) ;
String aux = score > 9 ? "" : "0" ;
bkd_g.drawString(aux+score, window_size.width - 60,30) ;
bkd_g.dispose() ;
Graphics bg = buffer.getGraphics() ;
bg.clipRect(0, 0, window_size.width, 40);
bg.drawImage(backdrop,0,0,this) ;
bg.dispose() ;
Graphics g = getGraphics() ;
g.clipRect(0, 0, window_size.width, 40);
g.drawImage(buffer,0,0,this) ;
g.dispose() ;
}
//游戏结束时进行提示
public void display_game_over() {
set_say_font(font) ;
set_say_mode(CENTER) ;
set_say_style(SHADOW) ;
set_say_pos(10,80) ;
say("GAME OVER") ;
set_say_font(font_s) ;
set_say_style(NORMAL) ;
say("(click to start)") ;
repaint() ;
try {Thread.sleep(500);} catch (InterruptedException e) { } ;
}
18 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:22:00
ufo(5)
//处理鼠标移动事件
public boolean mouseMove(Event e, int x, int y) {
// 返回鼠标所在位置的X坐标
mouse_x = x ;
return true;
}
// 处理鼠标的按下事件
public boolean mouseDown(Event e, int x, int y) {
//游戏结束时所做的相应处理
if (game_over) {
game_over = false ;
if (game != null) {
game.stop() ;
game = null ;
}
NU = 1 ;
score = 0 ;
M.active(false) ;
UV.removeAllElements() ;
EV.removeAllElements() ;
//新建一个线程
game = new Thread(this) ;
game.setPriority(Thread.MIN_PRIORITY) ;
//线程启动
game.start() ;
buf_g.dispose() ;
return true ;
}
//如果游戏没有结束并且导弹没有被发射,则发射导弹
if (M != null && ! M.active()) {
missile_launch.stop() ;
missile_launch.play() ;
M.set_pos(L.px,L.py) ;
M.active(true) ;
}
return true;
}
// 将缓冲绘制到屏幕上
public void paint(Graphics g) {
if (buffer != null) g.drawImage(buffer, 0, 0, this);
}
// 使动画更加流畅
public void update(Graphics g) {
paint(g) ;
}
//获取这个applet的Frame对象
public Frame getFrame(Component c) {
while( c != null && !(c instanceof java.awt.Frame) )
c = c.getParent();
return (Frame) c;
}
// 文本显示的常量
public static final int CENTER = 1 ; // 模式: 居中
public static final int LEFT = 2 ; // 居左
public static final int RIGHT = 3 ; // 居右
public static final int FREE = 0 ; // 居于所给的(x,y)位置上
public static final int NORMAL = 0 ; // 类型:正常
public static final int SHADOW = 1 ; // 带有阴影
// 文本显示变量
private int say_pos_y = 0 ;
private int say_pos_x = 0 ;
private int say_mode = -1 ;
private int say_style = -1 ;
private int say_margin = 10 ;
private Font say_font = null ;
// 文本显示方法
public void say(String s, int x, int y) {
set_say_pos(x, y) ;
say(s) ;
}
//文本显示
public void say(String s) {
//获取字体的信息
FontMetrics fm = getFontMetrics(say_font) ;
// 计算x坐标
switch(say_mode) {
case CENTER:
say_pos_x = (window_size.width - fm.stringWidth(s))/2 ;
break ;
case RIGHT:
say_pos_x = window_size.width - fm.stringWidth(s) - say_margin ;
break ;
case LEFT:
default :
say_pos_x = say_margin ;
break ;
}
19 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:23:00
ufo(6)
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) ;
//提升相应的 y坐标值
say_pos_y += (int) (1.2 * fm.getHeight()) ;
// 释放一些资源
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 {
UFO_Attack 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.reshape(ox,oy,w,h) ;
n.reshape(x,y,w,h) ;
}
//获取active属性值
public boolean active() {
return active ;
}
//设置active属性值
public void active(boolean s) {
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 ;
}
20 楼
buaacse3606 [专家分:0] 发布于 2007-11-25 14:24:00
ufo(7)
//对象的绘制函数
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() ;
}
}
//定义导弹发射架类,继承于Piece类
class Launcher extends Piece {
//Launcher类的构造函数
public Launcher (UFO_Attack 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() ;
//根据导弹的active属性值进行相应的绘制
if (a.M.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() ;
// bg = null ;
}
//使用新的区域
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() ;
}
}
我来回复