java怎么画出来的图片随着鼠标的移动而移动,有代码更好。新手求大腿
1个回答
展开全部
按照你的要求画出来的图片随着鼠标的移动而移动的Java程序如下
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class FFF extends JFrame implements MouseMotionListener{
ImageIcon ii=new ImageIcon("female.png");
int x,y;
FFF(){
setTitle("Mouse Move");
addMouseMotionListener(this);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(ii.getImage(), x, y, null);
}
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
x=e.getX();
y=e.getY();
repaint();
}
public static void main(String[] args) {
new FFF();
}
}
运行结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询