i am tyring to get a line to re-draw from where the mouse is clicked. the *** in my code is where i am trying to redraw the line but i cant find the right code to make it work. Thanks in advance for any help.
class MousePressListener implements MouseListener{
public void mousePressed(MouseEvent event){
int x = event.getX();
int y = event.getY();
***
repaint();
}
public void mouseReleased(MouseEvent event){}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
}
MouseListener listener = new MousePressListener();
addMouseListener(listener);
}
public void paint(Graphics g){
Line2D.Double jimsline = new Line2D.Double(x,y,x2,y2);
Graphics2D g2 = (Graphics2D)g;
g2.draw(jimsline);
}
private Line2D.Double jimsline;
private static final int x = 100;
private static final int y = 100;
private static final int x2 = 20;
private static final int y2 = 30;
}
[ArchAngel added CODE tags and indented code]
10-21-2003, 06:50 AM
ArchAngel
I'm assuming that you're trying to overwrite the x and y class values in the following code:
Code:
public void mousePressed(MouseEvent event){
int x = event.getX();
int y = event.getY();
...
However, you're not doing this as you've declared these 'x' and 'y' variables, thereby making them *local* variables. Also you class variables 'x' and 'y' are declared as final:
Code:
private static final int x = 100;
private static final int y = 100;
Lastly, I would suggest you make 'MousePressListener' extend 'MouseAdapter', that way you don't need to declare all those unused methods:
Code:
public void mouseReleased(MouseEvent event){}
public void mouseClicked(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}