Drawing lines with mouse on canvas : Java awt

后端 未结 3 1430
难免孤独
难免孤独 2021-01-05 05:35

The attempt is to enable drawing of figures(a line for now) with mouse on the awt canvas . Iam trying out java graphics for the first time . So not sure how to go about it .

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-05 06:29

    Make use of Line2D object in the AWT package and do the following steps:

    1. Create mouse (X,Y) values for first and second clicks
    2. Create a boolean variable to check if the click is the first or the second
    3. Make a List container to contain your Line2D objects
    4. Draw them in your Can object
    5. Assign the before and after (X,Y) values through the mouse listener's event

    The step number 5 could be achieved through:

    1. e.getX()
    2. e.getY()

    Where e is the mouse event and could be accesed through the parameter of the mouse listener method.

提交回复
热议问题