I have an assignment to make a paint program using java but i am kind of stuck at a roadblock. I have a pencil tool and i used the mouseDragged, mousePressed, and mouseReleased methods of mouse(motion)listener to draw my path using general paths (lineTo and moveTo).

My question is, is there a way to draw a line using general paths AND be able to see the line being drawn at the same time. I got it to draw lines but I can not see the line being drawn.

@Override
  public void mouseDragged(final MouseEvent the_event)
  {
//    System.err.println("mouse dragging: " + the_event.getX() + ", " + the_event.getY());
    
//    AbstractTool.MY_PATH.moveTo(the_event.getX(), the_event.getY());
  }

  @Override
  public void mousePressed(final MouseEvent the_event)
  {
//    System.err.println("mousePressed: " + the_event.getX() + ", " + the_event.getY());
    AbstractTool.MY_PATH.moveTo(the_event.getX(), the_event.getY());

  }

  @Override
  public void mouseReleased(final MouseEvent the_event)
  {
//    System.err.println("mouse released: " + the_event.getX() + ", " + the_event.getY());
    AbstractTool.MY_PATH.lineTo(the_event.getX(), the_event.getY());
  }

Thanks for your help!

You can add a repaint() call right after you update your path.

In my paint component method?

Your listener just needs to call repaint(). That will trigger a call to your paintComponent() method on the event dispatch thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.