import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
public class test {
public static void main(String args[]) {
JFrame window = new JFrame();
window.setSize(600, 600);
window.setTitle("test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestComponent tc = new TestComponent();
window.add(tc);
window.setVisible(true);
}
}
class Edge extends Line2D.Double {
boolean active = true;
Edge(double x1, double y1, double x2, double y2) {
super(x1, y1, x2, y2);
}
public void click(Point p) {
int HITBOX_SIZE = 10;
int boxX = p.x - HITBOX_SIZE / 2;
int boxY = p.y - HITBOX_SIZE / 2;
System.out.printf("here %d, %d", p.x, p.y);
if (intersects(boxX, boxY, HITBOX_SIZE, HITBOX_SIZE))
active = !active;
}
}
class TestComponent extends JComponent {
List<Edge> edges = new ArrayList<Edge>();
TestComponent() {
edges.add(new Edge(0, 0, 50, 50));
edges.add(new Edge(50, 50, 50, 100));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
for (Edge edge : edges) {
edge.click(e.getPoint());
}
repaint();
}
});
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
for (Edge e : edges) {
if (e.active)
g2.setColor(Color.BLUE);
else
g2.setColor(Color.GRAY);
g2.draw(e);
}
}
}