Originally Posted by
The_Mexican
Hello, I'm attempting to make a program which will draw a line with a length of 10 units wherever the user clicks. However, it's not working, and clicking anywhere in the window does nothing.
Here is the program:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class LineCreator extends Applet implements MouseListener{
int MouseClicked = 0;
public void init(){
resize(501,501);
this.addMouseListener(this);
resize(501, 501);
}
public void paint (Graphics g)
{
if(MouseClicked == 1)
{
int MouseXValue = MouseInfo.getPointerInfo().getLocation().x;
int MouseYValue = MouseInfo.getPointerInfo().getLocation().y;
g.drawLine(MouseXValue, MouseYValue, MouseXValue+10, MouseYValue+10);
MouseClicked = MouseClicked + 1;
}
}
public void mouseClicked(MouseEvent e) {
MouseClicked = MouseClicked + 1;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
Any help would be greatly appreciated!
mouseClicked() has a parameter which you didn't pass in your if statement.
Also, mouseClicked is void anyway and so cannot return anything so it isn't equal to anything.
oh, I see, that's an int.
To get it to draw a line from where it was pressed to where it was released, you could have some variable that will show location of mouse is where it was pressed and where it was released with the other methods you implemented.