JPanel
ベースにすればダブルバッファリングで描画するので、ちらつかないようになります。
Java
1import java.awt.Graphics;
2import java.awt.Image;
3import java.awt.event.MouseAdapter;
4import java.awt.event.MouseEvent;
5import javax.swing.ImageIcon;
6import javax.swing.JFrame;
7import javax.swing.JPanel;
8
9
10public class Mouse extends JPanel { // JPanelベースに
11 public static void main(String[] args) {
12 var frame = new JFrame();
13 frame.setSize(500, 500);
14 frame.setLocationRelativeTo(null);
15 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16 frame.add(new Mouse());
17 frame.setVisible(true);
18 }
19
20 private Image image = new ImageIcon("./image/imsge1.png").getImage();
21 private int x1 = 100;
22 private int y1 = 100;
23 private int x2 = 100;
24 private int y2 = 300;
25 private int offsetX; // 画像のつまんだ位置X
26 private int offsetY; // 同上Y
27 private int command;
28
29 public Mouse() {
30 addMouseListener(new MouseAdapter() { // 不要なメソッドが邪魔なのでアダプタで
31 @Override public void mousePressed(MouseEvent e) { // ClickedからPressedに変更
32 var clickX = e.getPoint().x;
33 var clickY = e.getPoint().y;
34
35 if (x1 < clickX && clickX < x1 + 50 && y1 < clickY && clickY < y1 + 50) {
36 command = 1;
37 offsetX = clickX - x1; // 画像のつまんだ位置を保存
38 offsetY = clickY - y1;
39 }
40 if (x2 < clickX && clickX < x2 + 50 && y2 < clickY && clickY < y2 + 50) {
41 command = 2;
42 offsetX = clickX - x2;
43 offsetY = clickY - y2;
44 }
45 }
46
47 @Override public void mouseReleased(MouseEvent e) {
48 command = 0;
49 }
50 });
51
52 addMouseMotionListener(new MouseAdapter() {
53 @Override public void mouseDragged(MouseEvent e) {
54 switch (command) {
55 case 1:
56 x1 = e.getPoint().x - offsetX; // 画像のつまんだ位置を考慮し更新
57 y1 = e.getPoint().y - offsetY;
58 repaint();
59 break;
60 case 2:
61 x2 = e.getPoint().x - offsetX;
62 y2 = e.getPoint().y - offsetY;
63 repaint();
64 break;
65 }
66 }
67 });
68 }
69
70 @Override
71 protected void paintComponent(Graphics g) { // paintからpaintComponentに
72 super.paintComponent(g);
73
74 g.drawImage(image, x1, y1, 50, 50, this);
75 g.drawImage(image, x2, y2, 50, 50, this);
76 }
77}
気になった点をいくつか修正させていただきました。
画像をJLabel
で表示すれば、当たり判定や描画等をSwing
に任せられてかなり手を抜けます。
Java
1import java.awt.Point;
2import java.awt.event.MouseAdapter;
3import java.awt.event.MouseEvent;
4import javax.swing.ImageIcon;
5import javax.swing.JComponent;
6import javax.swing.JFrame;
7import javax.swing.JLabel;
8import javax.swing.JPanel;
9
10
11public class Mouse extends JPanel {
12 public static void main(String[] args) {
13 var frame = new JFrame();
14 frame.setSize(500, 500);
15 frame.setLocationRelativeTo(null);
16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17 frame.add(new Mouse());
18 frame.setVisible(true);
19 }
20
21 public Mouse() {
22 setLayout(null);
23
24 var imageIcon = new ImageIcon("./image/imsge1.png");
25 var adapter = new MouseAdapter() {
26 private Point offset;
27
28 @Override public void mousePressed(MouseEvent e) {
29 offset = e.getPoint();
30 }
31
32 @Override public void mouseDragged(MouseEvent e) {
33 var component = (JComponent) e.getSource();
34 var x = component.getX() + e.getX() - offset.x;
35 var y = component.getY() + e.getY() - offset.y;
36 component.setLocation(x, y);
37 }
38 };
39
40 var label1 = new JLabel(imageIcon);
41 label1.setBounds(100, 100, 50, 50);
42 label1.addMouseListener(adapter);
43 label1.addMouseMotionListener(adapter);
44 add(label1);
45
46 var label2 = new JLabel(imageIcon);
47 label2.setBounds(100, 300, 50, 50);
48 label2.addMouseListener(adapter);
49 label2.addMouseMotionListener(adapter);
50 add(label2);
51 }
52}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/04 16:27