如何使用MouseListener来移动不同形状

huangapple go评论83阅读模式
英文:

How to move different shapes with MouseListener

问题

我有一个程序可以绘制我放在ArrayList中的不同形状用来迭代形状以绘制它们的部分工作正常但是我的移动方法却不起作用是我move()方法有问题吗

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyShapes extends JPanel {
    private Point2D.Float position;

    private final DifferentShapes[] shapes = new DifferentShapes[]{new Circle(), new Triangle(), new Square()};

    MovingAdapter ma = new MovingAdapter();

    public MyShapes() {
        addMouseMotionListener(ma);
        addMouseListener(ma);
    }

    interface DifferentShapes {
        void paint(Graphics2D graphics);
        boolean contains(int x, int y);
        void move(int dx, int dy);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D graphics = (Graphics2D) g;
        for (DifferentShapes shape : this.shapes) {
            shape.paint(graphics);
        }
    }

    class MovingAdapter extends MouseAdapter {
        private int x;
        private int y;

        public void mousePressed(MouseEvent e) {
            x = e.getX();
            y = e.getY();
        }

        public void mouseDragged(MouseEvent e) {
            final int dx = e.getX() - x;
            final int dy = e.getY() - y;

            for (DifferentShapes shape : shapes) {
                if (shape.contains(x, y)) {
                    shape.move(dx, dy);
                }
            }
            x += dx;
            y += dy;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Shapes World");
        MyShapes m = new MyShapes();
        m.setDoubleBuffered(true);
        frame.add(m);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Circle implements MyShapes.DifferentShapes {
    public Circle() {
    }
    public static Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);

    public void paint(Graphics2D graphics) {
        Graphics2D circle = (Graphics2D) graphics;

        circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        circle.setColor(new Color(0, 0, 117));
        circle.fill(myCr);
    }
    @Override
    public boolean contains(int x, int y) {
        if (myCr.contains(x, y)) {
        }
        return true;
    }
    public void move(int dx, int dy) {
        myCr.x += dx;
        myCr.y += dy;
    }
}
英文:

I have a program that paints different shapes that I put in the ArrayList, it works fine to iterate with shapes to paint them but my methods for moving them doesn't work. Is it something wrong with my move() method?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyShapes extends JPanel {
private Point2D.Float position;
private final DifferentShapes[] shapes = new DifferentShapes[]{new Circle(), new Triangle(), new Square()};
MovingAdapter ma = new MovingAdapter();
public MyShapes() {
addMouseMotionListener(ma);
addMouseListener(ma);
}
interface DifferentShapes {
void paint(Graphics2D graphics);
boolean contains(int x, int y);
void move(int dx, int dy);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D graphics = (Graphics2D) g;
for (DifferentShapes shape : this.shapes) {
shape.paint(graphics);
}
}

class MovingAdapter

  public  class MovingAdapter extends MouseAdapter {
private int x;
private int y;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
final int dx = e.getX() - x;
final int dy = e.getY() - y;
for (DifferentShapes shape : shapes) {
if (shape.contains(x, y)) {
shape.move(dx, dy);
}
}
x += dx;
y += dy;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shapes World");
MyShapes m = new MyShapes();
m.setDoubleBuffered(true);
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class Circle

 class Circle implements MyShapes.DifferentShapes {
public Circle() {
}
public static Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
public void paint(Graphics2D graphics) {
Graphics2D circle = (Graphics2D) graphics;
circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
circle.setColor(new Color(0, 0, 117));
circle.fill(myCr);
}
@Override
public boolean contains(int x, int y) {
if (myCr.contains(x, y)) {
}
return true;
}
public void move(int dx, int dy) {
myCr.x += dx;
myCr.y += dy;
}
}

When I use regular if statement it works fine but not in the for loop and my move() method

答案1

得分: 0

逻辑是正确的,你正在正确地改变坐标。你只需要调用repaint()来使这些变化显示出来。

if (shape.contains(x, y)) {
   shape.move(dx, dy);
   repaint();
}
英文:

The logic is right and you are correctly changing the coordinates. You just need to call repaint() in order to make the the changes display.

if (shape.contains(x, y)) {
   shape.move(dx, dy);
   repaint();
}

huangapple
  • 本文由 发表于 2020年4月7日 00:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61063992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定