Detect overlay of figures after dragging with the mouse

Asked

Viewed 150 times

1

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

When moving the figure, I need to detect if it was dragged and released in its corresponding figure. For example, if the Big Triangle was dragged and dropped into the Big Triangle and the Middle the same thing.

Follows 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);

            }
        });
    }
}
  • You would need to create a Jpanel for each square, and capture Jpanel events

  • May I suggest a different approach, similar to the one I used in this question ?

  • You can, @Articuno...

  • Your code is statistical, there is no movement or animation. You want to detect collision?

  • Type, I wanted to know if the triangle that will be placed on the other (for example: the Big Triangle), is in fact a Big Triangle (which would be correct) or a Middle Triangle (which would be wrong, in this case)...

  • The problem is that your code is quite incomplete (although reproducible), suggesting something like this would be too broad, you didn’t implement anything of movement or click and drag. The question becomes meaningless because it is assumed that this would already be implemented and that its doubt was only to detect overlapping.

  • I’m implementing this drag and click part... My biggest doubt is on the question of overlap... I apologize if I could not express

  • I understand, so the best thing would be to add the drag implementation first and then worry about overlapping. Without this implementation, there is no way to suggest anything, because we would have to create this implementation, which completely evades doubt.

  • I updated the code... @Articuno

Show 4 more comments

1 answer

0


The solution below underlies that what is intended is to detect when the filled triangles will be superimposed on the only circumvented triangles altogether.

Detecting complete overlap, given that the triangles have very similar sizes, can be done using the method contains() class Retangle, where in the example below:

retanguloA.contains(retanguloB);

we check whether the retanguloA contains the retanguloeB in full.

The best time to check this is when the drag of the object is completed, that is, when the mouse button is released, and the method that is called when this occurs is the 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 the minimum 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 which array a given triangle was part of, so it is important to provide names that make sense to use it, in this case I renamed the array to fixedPolygons.

The complete code with the modifications was 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 occur:

triangulos grandes sobrepostos

triangulos medios sobrepostos

Of course, when running, you will notice that if you try to superimpose the larger triangle with the smaller one already superimposed, or vice versa, the message will be repeating every time you release the mouse. But this I have purposely left for you to decide what should occur after the overlaps.

  • Thank you very much! You helped me very much! Vlw mesmoooo!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.