鼠标拖动画图的问题????///

funpig 2002-01-25 06:31:09
下面的程序中,我想实现在鼠标的拖动中,圆随着鼠标的拖动而移动,但我怎么也实现
不了,希望各位大哥给点看法,谢谢!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyGame
{
public static void main(String[] args)
{
JFrame mygame=new MyGameFrame();
mygame.show();
}
}

class MyGameFrame extends JFrame
{
public MyGameFrame()
{
setTitle("游戏");
setSize(350,350);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new MyPanel());
}
}

class MyPanel extends JPanel implements MouseMotionListener
{

private int curx=10,cury=10,newx,newy;
private Graphics g;

public MyPanel()
{

addMouseMotionListener(this);

}


public boolean isFocusTraversable()
{
return(true);
}

public void update(Graphics g)
{

movepaint(g,curx,cury);
movepaint(g,newx,newy);
curx=newx;
cury=newy;
}

public void paint(Graphics g)
{
//g.setPaintMode();
//g.setColor(Color.black);
movepaint(g,curx,cury);
}

private void movepaint(Graphics g,int x,int y)
{
g.setXORMode(Color.white);
g.setColor(Color.black);
g.fillOval(x,y,25,25);

}



public void mouseDragged(MouseEvent e)
{
System.out.println("Dragged");
newx = e.getX();
newy = e.getY();
//System.out.println(newx+" "+newy);
repaint();


}

public void mouseMoved(MouseEvent e) {}


}


...全文
48 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
skyyoung 2002-01-26
/*
A basic extension of the java.applet.Applet class
*/
import java.awt.*;
import java.lang.*;
import java.applet.*;

public class DoubleClickApplet extends Applet
{
String mouseClickedString = "Mouse event : none";
String mouseClickedString2 = "Mouse double click : no";
long lastDownTime = 0;
final static long DOUBLE_CLICK_TIME = 500;
public boolean mouseDown(Event evt, int x, int y)
{
if((evt.when - lastDownTime) < DOUBLE_CLICK_TIME)
mouseDoubleClick(evt, x, y);
else
{
mouseClickedString2 = "Mouse double click : no";
repaint();
}
lastDownTime = evt.when;
mouseClickedString = "Mouse event : mouseDown";
getAppletContext().showStatus("Mouse Click");
repaint();
return false;
}
public boolean mouseDoubleClick(Event evt, int x, int y)
{
mouseClickedString2 = "Mouse double click : yes";
getAppletContext().showStatus("Mouse Double Click");
repaint();
return false;
}
public boolean mouseDrag(Event evt, int x, int y)
{
mouseClickedString = "Mouse event : mouseDrag";
getAppletContext().showStatus("Mouse Drag");
repaint();
return false;
}
public boolean mouseEnter(Event evt, int x, int y)
{
mouseClickedString = "Mouse event : mouseEnter";
getAppletContext().showStatus("Mouse Enter");
repaint();
return false;
}
public boolean mouseExit(Event evt, int x, int y)
{
mouseClickedString = "Mouse event : mouseExit";
getAppletContext().showStatus("Mouse Exit");
repaint();
return false;
}
public boolean mouseUp(Event evt, int x, int y)
{
mouseClickedString = "Mouse event : mouseUp";
getAppletContext().showStatus("Mouse Up");
repaint();
return false;
}
// public boolean mouseMove(Event evt,int x,int y)
// {
// mouseClickedString="Mouse event : mouseMove";
// mouseClickedString2="Mouse double click : no";
// repaint();
// return false;
// }
public void paint(Graphics g)
{
g.drawString(mouseClickedString, 20, 20);
g.drawString(mouseClickedString2, 20, 50);
}
}

回复
masterz 2002-01-26
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class MyGame
{
public static void main(String[] args)
{
JFrame mygame=new MyGameFrame();
mygame.show();
}
}

class MyGameFrame extends JFrame
{
public MyGameFrame()
{
setTitle("ÓÎÏ·");
setSize(350,350);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new MyPanel());
}
}

class MyPanel extends JPanel implements MouseMotionListener, MouseListener
{

private int curx=10,cury=10,newx,newy;
// private Graphics g;
private Point lastPoint;
public MyPanel()
{
addMouseMotionListener(this);
addMouseListener(this);
}


public boolean isFocusTraversable()
{
return(true);
}

public void update(Graphics g)
{
paint(g);
}

public void paint(Graphics g)
{
movepaint(g,curx,cury);
g.fillOval(newx,newy,25,25);
curx=newx;
cury=newy;
}

private void movepaint(Graphics g,int x,int y)
{
if(lastPoint==null)
return;
///g.setXORMode(Color.white);
g.setXORMode(this.getBackground());
g.setColor(Color.black);
g.fillOval(x,y,25,25);

}
public void mouseDragged(MouseEvent e)
{
System.out.println("Dragged");
if(binside)
{
newx = newx + e.getX()-lastPoint.x;
newy = newy + e.getY()-lastPoint.y;
lastPoint=e.getPoint();
repaint();
}

}

public void mouseMoved(MouseEvent e) {}

public void mouseClicked(MouseEvent parm1) {
// TODO: Add your code here
}
boolean binside;
public void mousePressed(MouseEvent parm1)
{
if(isInsideObject(parm1.getPoint()))
lastPoint=parm1.getPoint();
}

public void mouseReleased(MouseEvent parm1)
{
// TODO: Add your code here
}

public void mouseEntered(MouseEvent parm1) {
// TODO: Add your code here
}

public void mouseExited(MouseEvent parm1)
{
// TODO: Add your code here
}
private boolean isInsideObject(Point pt)
{
int cx=newx+25/2;
int cy = newy+25/2;
int xdif=pt.x-cx;
int ydif=pt.y-cy;
double fdis=xdif*xdif+ydif*ydif;
fdis=Math.sqrt(fdis);
if(fdis>25/2)
binside= false;
else
binside=true;
return binside;
}

}
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2002-01-25 06:31
社区公告
暂无公告