0

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();




}
 }
Abby
  • 3
  • 4
  • What errors you get? Welcome to SO btw – DarkCygnus Oct 09 '17 at 21:17
  • Possible duplicate of [Create rectangle with mouse drag, not draw](https://stackoverflow.com/questions/15776549/create-rectangle-with-mouse-drag-not-draw) – user3437460 Oct 09 '17 at 21:23
  • I thought the easiest way to do this was to access the coordinates from mouse dragged and use them to draw the rectangle in the graphics class but there are scoping issues (That's the error) – Abby Oct 09 '17 at 21:43

0 Answers0