I have just started using Java's GUI stuff so my understanding isn't great. Im trying to produce a program where a user can drag the mouse and can produce a rectangle at the same time. I have attempted to do this but I'm facing issues with scoping. Any advice would be helpful. Ive attached what I assume to be the relevant sections of my code.
void draw(Graphics g)
{
int canvaswidth= canvas.getWidth();
int canvasheight=canvas.getHeight();
g.setColor(new Color(0.8F, 0.8F, 0.8F));
for(int j=0;j<canvas.getHeight();j = j+10) {
g.drawLine(0,j,canvas.getWidth(),j);
}
for(int i=0;i<canvas.getWidth();i=i+10){
g.drawLine(i, 0, i,canvas.getHeight());
}
for(int k=0;k<canvas.getHeight();k=k+50){
g.setColor(new Color(0.6F, 0.6F, 0.6F));
g.drawLine(0, k, canvas.getWidth(), k);
}
for(int l=0;l<canvas.getWidth();l=l+50){
g.drawLine(l, 0, l, canvas.getHeight());
}
g.setColor(Color.red);
}
public void mouseDragged(MouseEvent event)
{
int x2 =event.getX();
int y2=event.getY();
mouseMoved(event);
canvas.repaint();
}
}