当前位置: 技术问答>java相关
看看这个程序,40分
来源: 互联网 发布时间:2015-01-16
本文导语: 我想实现象即时战略游戏中用鼠标框选作战单位那样的效果,但是写了一个,有点问题,望各位能帮忙改一下 /* */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TMouseMotion extends JApplet implements Mous...
我想实现象即时战略游戏中用鼠标框选作战单位那样的效果,但是写了一个,有点问题,望各位能帮忙改一下
/*
*/
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_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_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_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_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
/*
*/
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_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_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_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_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