Detect overlapping figures after dragging with the mouse

1

I drew four pictures with Graphics 2D (two large triangles and two medium triangles), two fixed (1 medium triangle and 1 large triangle) and 2 for movement (1 medium triangle and 1 large triangle).

When you move the picture, you need to detect if it has been dragged and dropped into its corresponding shape. For example, if the Big triangle was dragged and dropped in the Big triangle and the middle one the same thing.

Follow my code:

public class Teste2 extends javax.swing.JPanel {

    private Polygon[] polygons = {};
    private Polygon[] polygons2 = {};
    private Point ultimo = null;
    private Polygon selecionado = null;

    public Teste2() {
        polygons = new Polygon[2];
        polygons2 = new Polygon[2];
        this.mover();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor(Color.BLUE);
        g2d.fillPolygon(polygons[0]);

        g2d.setColor(Color.RED);
        g2d.fillPolygon(polygons[1]);

        g2d.setColor(Color.BLUE);
        g2d.drawPolygon(polygons2[0]);

        g2d.setColor(Color.RED);
        g2d.drawPolygon(polygons2[1]);
        g2d.dispose();
    }

    public void trianguloGrande1() {
        int x[] = {90, 90, 392};
        int y[] = {512, 248, 512};
        polygons2[0] = new Polygon(x, y, 3);
    }

    public void trianguloMedio1() {
        int x[] = {245, 90, 395};
        int y[] = {110, 242, 242};
        polygons2[1] = new Polygon(x, y, 3);
    }

    public void trianguloGrande2() {
        int x[] = {630, 630, 933};
        int y[] = {283, 19, 283};
        polygons[0] = new Polygon(x, y, 3);
    }

    public void trianguloMedio2() {
        int x[] = {1152, 1000, 1305};
        int y[] = {86, 219, 219};
        polygons[1] = new Polygon(x, y, 3);
    }

    public Polygon[] getPolygons() {
        return polygons;
    }

    public void setPolygons(Polygon[] polygons) {
        this.polygons = polygons;
    }

    public Polygon[] getPolygons2() {
        return polygons;
    }

    public void setPolygons2(Polygon[] polygons2) {
        this.polygons2 = polygons2;
    }

    private void mover() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                onMouseDown(e);
                Graphics2D g = (Graphics2D) getGraphics();
                int x = e.getX();
                int y = e.getY();
                if (e.isMetaDown()) {
                    polygons[0].contains(x, y);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                onMouseDragged(e);
            }
        });
    }

    protected void onMouseDown(MouseEvent e) {
        ultimo = e.getPoint();
        for (Polygon polygon : polygons) {
            if (polygon.contains(ultimo)) {
                selecionado = polygon;
                return;
            }
        }
        selecionado = null;
    }

    protected void onMouseDragged(MouseEvent e) {
        Point now = e.getPoint();
        if (ultimo != null) {
            int xt = now.x - ultimo.x;
            int yt = now.y - ultimo.y;
            if (selecionado != null) {
                selecionado.translate(xt, yt);
                repaint();
            }
            ultimo = now;
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame jFrame = new JFrame("Teste");
                Teste2 teste2 = new Teste2();

                jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jFrame.getContentPane().add(teste2);
                jFrame.pack();
                jFrame.setResizable(false);
                jFrame.setVisible(true);
                jFrame.setExtendedState(MAXIMIZED_BOTH);

                teste2.trianguloGrande1();
                teste2.trianguloGrande2();
                teste2.trianguloMedio1();
                teste2.trianguloMedio2();
                teste2.setPolygons(teste2.polygons);
                teste2.setPolygons2(teste2.polygons2);

            }
        });
    }
}
    
asked by anonymous 16.04.2018 / 17:24

1 answer

1
  

The solution below is intended to detect when the filled triangles will be superimposed on the triangles just bypassed completely.

Detect full overlap, since triangles have very similar sizes, can be done using the contains() method of class Retangle , where in the example below:

retanguloA.contains(retanguloB);

We checked that retanguloA contains retanguloeB completely.

The best time to check this is when the drag of the object is finished, that is, when the mouse button is released, and the method that is called when this occurs is mouseReleased() . It is within it that we will check whether the filled triangles are contained in the fixed:

    @Override
    public void mouseReleased(MouseEvent e) {

        if(fixedPolygons[0].getBounds().contains(polygons[0].getBounds())) {
            JOptionPane.showMessageDialog(null, "Triangulos grandes sincronizados");
        }

        if(fixedPolygons[1].getBounds().contains(polygons[1].getBounds())) {
            JOptionPane.showMessageDialog(null, "Triangulos medios sincronizados");
        }
    }

The method getBounds() returns a rectangle whose area is as minimal as possible to fully envelop the created polygon. However, I had to slightly reduce the moving triangles to work better.

I made a small change in the name of some variables, because it was difficult to identify from which array a given triangle was part, so it is important to provide names that make sense for the use of it, in this case, I renamed the array to fixedPolygons .

The complete code with the modifications looks like this:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MousetriangulosTest extends javax.swing.JPanel {

    private Polygon[] polygons = {};
    private Polygon[] fixedPolygons = {};
    private Point ultimo = null;
    private Polygon selecionado = null;

    public MousetriangulosTest() {
        polygons = new Polygon[2];
        fixedPolygons = new Polygon[2];
        this.mover();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setColor(Color.BLUE);
        g2d.fillPolygon(polygons[0]);

        g2d.setColor(Color.RED);
        g2d.fillPolygon(polygons[1]);

        g2d.setColor(Color.BLUE);
        g2d.drawPolygon(fixedPolygons[0]);

        g2d.setColor(Color.RED);
        g2d.drawPolygon(fixedPolygons[1]);
        g2d.dispose();
    }

    public void trianguloGrande1() {
        int x[] = {90, 90, 392};
        int y[] = {512, 248, 512};
        fixedPolygons[0] = new Polygon(x, y, 3);
    }

    public void trianguloMedio1() {
        int x[] = {245, 90, 395};
        int y[] = {110, 242, 242};
        fixedPolygons[1] = new Polygon(x, y, 3);
    }

    public void trianguloGrande2() {
        int x[] = {630, 630, 923};
        int y[] = {273, 19, 273};
        polygons[0] = new Polygon(x, y, 3);
    }

    public void trianguloMedio2() {
        int x[] = {1152, 1000, 1295};
        int y[] = {86, 209, 209};
        polygons[1] = new Polygon(x, y, 3);
    }

    public Polygon[] getPolygons() {
        return polygons;
    }

    public void setPolygons(Polygon[] polygons) {
        this.polygons = polygons;
    }

    public Polygon[] getPolygons2() {
        return polygons;
    }

    public void setPolygons2(Polygon[] polygons2) {
        this.fixedPolygons = polygons2;
    }

    private void mover() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                onMouseDown(e);
                Graphics2D g = (Graphics2D) getGraphics();
                int x = e.getX();
                int y = e.getY();
                if (e.isMetaDown()) {
                    polygons[0].contains(x, y);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {

                if(fixedPolygons[0].getBounds().contains(polygons[0].getBounds())) {
                    JOptionPane.showMessageDialog(null, "Triangulos grandes sincronizados");
                }

                if(fixedPolygons[1].getBounds().contains(polygons[1].getBounds())) {
                    JOptionPane.showMessageDialog(null, "Triangulos medios sincronizados");
                }
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                onMouseDragged(e);
            }
        });
    }

    protected void onMouseDown(MouseEvent e) {
        ultimo = e.getPoint();
        for (Polygon polygon : polygons) {
            if (polygon.contains(ultimo)) {
                selecionado = polygon;
                return;
            }
        }
        selecionado = null;
    }

    protected void onMouseDragged(MouseEvent e) {
        Point now = e.getPoint();
        if (ultimo != null) {
            int xt = now.x - ultimo.x;
            int yt = now.y - ultimo.y;
            if (selecionado != null) {
                selecionado.translate(xt, yt);
                repaint();
            }
            ultimo = now;
        }   
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame jFrame = new JFrame("Teste");
                MousetriangulosTest teste2 = new MousetriangulosTest();

                jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jFrame.getContentPane().add(teste2);
                jFrame.pack();
                jFrame.setResizable(false);
                jFrame.setVisible(true);
                jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

                teste2.trianguloGrande1();
                teste2.trianguloGrande2();
                teste2.trianguloMedio1();
                teste2.trianguloMedio2();
                teste2.setPolygons(teste2.polygons);
                teste2.setPolygons2(teste2.fixedPolygons);

            }
        });
    }
}

Running, this will happen:

Of course, when you run, you will notice that if you try to overlap the larger triangle with the smaller triangle already overlapped, or vice versa, the message will keep repeating each time you release the mouse. But this I purposely left to you to decide what should happen after the overlaps.

    
18.04.2018 / 17:34