java中怎么让图片随鼠标移动
2个回答
展开全部
按照你的要求画出来的图片随着鼠标的移动而移动的Java程序如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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();
}
}
运行结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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();
}
}
运行结果
展开全部
//给个实例
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageMove {
static int x,y;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图标
Icon icon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
// Icon icon = new ImageIcon("c:/logo-zhidao.gif");//本地图片文件
JLabel l = new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight()); l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l); f.setVisible(true);
l.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){ x=e.getX();y=e.getY(); } });
l.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {} }); } }
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageMove {
static int x,y;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图标
Icon icon = new ImageIcon(new URL("https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-zhidao.gif"));
// Icon icon = new ImageIcon("c:/logo-zhidao.gif");//本地图片文件
JLabel l = new JLabel(icon);
l.setSize(icon.getIconWidth(),icon.getIconHeight()); l.setBorder(BorderFactory.createLineBorder(Color.red));
f.getContentPane().add(l); f.setVisible(true);
l.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){ x=e.getX();y=e.getY(); } });
l.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {} }); } }
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询