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
– Sveen
May I suggest a different approach, similar to the one I used in this question ?
– user28595
You can, @Articuno...
– andré122
Your code is statistical, there is no movement or animation. You want to detect collision?
– user28595
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)...
– andré122
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.
– user28595
I’m implementing this drag and click part... My biggest doubt is on the question of overlap... I apologize if I could not express
– andré122
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.
– user28595
I updated the code... @Articuno
– andré122