Bei einem Mausklick alle segmente durchgehen
public void getMousePos(final Graphics2D g){
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(final MouseEvent e ) {
MousePosX = e.getX();
MousePosY = e.getY();
repaint();
}
});
checkPoint(g,MousePosX, MousePosY);
}
private void checkPoint(Graphics g,int MousePosX, int MousePosY){
for(int i = 0; i < Points.size(); i++) {
int a,b;
a = (int)((MousePosX - Points.elementAt(i).x)/(Points.elementAt(i+1).x-Points.elementAt(i).x));
b = (int)((MousePosY - Points.elementAt(i).y)/(Points.elementAt(i+1).y-Points.elementAt(i).y));
if (a == b) {
((Graphics2D) g).setStroke( new BasicStroke( 3 ) );
g.drawLine((int)Points.elementAt(i).x, (int)Points.elementAt(i).y, (int)Points.elementAt(i+1).x, (int)Points.elementAt(i+1).y);
((Graphics2D) g).setStroke( new BasicStroke( 1 ) );
}
}
Das Graphics, was du dort durschleifst, kann prinzipbedingt nicht das sein, was in paintComponent übergeben wird.
private Vector<Point> Points = new Vector<Point>();
void doSomething()
{
int something = Points.elementAt(i).x + Points.elementAt(i+1).x * Points.elementAt(i).x / Points.elementAt(i+1).x
}
// List statt Vector, Variablenname klein. (Das "new Vector" ist OK!)
private List<Point> points = new Vector<Point>();
void doSomething()
{
// 'get' statt 'elementAt'. Subjektiv: Mehrfach benutzte Werte in Variablen speichern
Point p0 = points.get(i);
Point p1 = points.get(i+1);
int something = p0.x + p1.x * p0.x / p1.x
}
public Shape getClickedShape(int x, int y) {
Iterator<Shape> i = shapes.iterator();
while (i.hasNext()) {
Shape s = i.next();
if (s.intersects(new Rectangle2D.Double(x - 1, y - 1, 5, 5))) return s;
}
return null;
}
class TheClass
{
private Line2D highlightedLine = null; // Evtl. eine List, wenn's mehrere geben soll
@Override
public void mouseClicked(MouseEvent e)
{
highlightedLine = null;
for (all Points p0, p1)
{
Line2D line = new Line2D.Double(p0, p1);
if (line.ptSegDistSq(e.getPoint()) < 5)
{
highlightedLine = line;
}
}
}
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
// Male alles wie vorher
...
if (highlightedLine != null)
{
g.setStroke( new BasicStroke( 3 ) );
g.draw(highlightedLine);
}
}
}