看看这个程序,40分

肖尧19 2001-07-19 09:22:41
我想实现象即时战略游戏中用鼠标框选作战单位那样的效果,但是写了一个,有点问题,望各位能帮忙改一下
/*
<Applet code=TMouseMotion width=400 height=400>
</Applet>
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TMouseMotion extends JApplet implements MouseListener
{
int x,y;
int o_x,o_y;
int flag;
int s_x;
int s_y;
int width;
int height;
public void init()
{
CustomListener ct=new CustomListener(this);
this.addMouseMotionListener(ct);
}
public void update(Graphics g)
{
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
paint(g);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString("drag/move mouse……",5,20);
g.setColor(Color.red);
if(flag==1)
{
g.drawString("don't move!drag the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
else if(flag==2)
{
g.drawString("don't drag!move the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
g.setColor(Color.blue);
g.drawRect(s_x,s_y,width,height);
}
public void mousePressed(MouseEvent e)
{
o_x=e.getX();
o_y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
o_x=0;
o_y=0;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
class CustomListener implements MouseMotionListener
{
TMouseMotion tm;
public CustomListener(TMouseMotion tm)
{
this.tm=tm;
}
public void mouseMoved(MouseEvent e)
{
tm.flag=2;
tm.x=e.getX();
tm.y=e.getY();
tm.repaint();
}
public void mouseDragged(MouseEvent e)
{
tm.flag=1;
tm.x=e.getX();
tm.y=e.getY();
if(tm.x>=tm.o_x && tm.y<=tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.y;
tm.width=tm.x-tm.o_x;
tm.height=tm.o_y-tm.y;
}
if(tm.x>=tm.o_x && tm.y>=tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.o_y;
tm.width=tm.x-tm.o_x;
tm.height=tm.y-tm.o_y;
}
if(tm.x<=tm.o_x && tm.y>=tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.o_y;
tm.width=tm.o_x-tm.x;
tm.height=tm.y-tm.o_y;
}
if(tm.x<=tm.o_x && tm.y<=tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.y;
tm.width=tm.o_x-tm.x;
tm.height=tm.o_y-tm.y;
}

tm.repaint();

}
}


...全文
131 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
darcy07 2001-07-19
  • 打赏
  • 举报
回复
在init中少了
addMouseListener(this);
masterz 2001-07-19
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
public class TMouseMotion extends JApplet implements MouseListener
{
int x,y;
int o_x,o_y;
int flag;
int s_x;
int s_y;
int width;
int height;
public void init()
{
CustomListener ct=new CustomListener(this);
this.addMouseMotionListener(ct);
addMouseListener(this);//You missed this line
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics gdraw)
{
BufferedImage bufimg=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics g=bufimg.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.blue);
g.drawString("drag/move mouse......",5,20);
g.setColor(Color.red);
if(flag==1)
{
g.drawString("don't move!drag the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
else if(flag==2)
{
g.drawString("don't drag!move the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
g.setColor(Color.blue);
g.drawRect(s_x,s_y,width,height);
gdraw.drawImage(bufimg,0,0,null);
}

public void mousePressed(MouseEvent e)
{
o_x=e.getX();
o_y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
o_x=0;
o_y=0;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
class CustomListener implements MouseMotionListener
{
TMouseMotion tm;
public CustomListener(TMouseMotion tm)
{
this.tm=tm;
}
public void mouseMoved(MouseEvent e)
{
tm.flag=2;
tm.x=e.getX();
tm.y=e.getY();
tm.repaint();
}
public void mouseDragged(MouseEvent e)
{
tm.flag=1;
tm.x=e.getX();
tm.y=e.getY();
if(tm.x>=tm.o_x && tm.y<=tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.y;
tm.width=tm.x-tm.o_x;
tm.height=tm.o_y-tm.y;
}
if(tm.x>=tm.o_x && tm.y>=tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.o_y;
tm.width=tm.x-tm.o_x;
tm.height=tm.y-tm.o_y;
}
if(tm.x<=tm.o_x && tm.y>=tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.o_y;
tm.width=tm.o_x-tm.x;
tm.height=tm.y-tm.o_y;
}
if(tm.x<=tm.o_x && tm.y<=tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.y;
tm.width=tm.o_x-tm.x;
tm.height=tm.o_y-tm.y;
}

tm.repaint();

}

}
knight_qmh 2001-07-19
  • 打赏
  • 举报
回复
在init()方法里加上
addMouseListener(this);
肖尧19 2001-07-19
  • 打赏
  • 举报
回复
what is the problem?
当然是画的框不对!

高手快来!
masterz 2001-07-19
  • 打赏
  • 举报
回复
What is the problem?

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧