JavaFX Simple Sketch

ソースコード Source Code

ドラッグで入力された点を直線で繋いで曲線を描くプログラムです. ユーザはマウスのボタンを押し, ドラッグし, そして離すことで点を入力します.

Using polyline of dragged points, this program draws curves. To input points, you press a button of mouse, drag the mouse, and release the button of the mouse.

f:id:Jumpaku:20161006223332p:plain

説明 Explanation

main で launch を呼び出されると InputLine オブジェクトが生成され, start が 実行されます.

24行目から28行目まででは次のように canvasイベントハンドラが設定されます.

  • マウスが押された時 : previous に始点を設定する.
  • マウスがドラッグされた時と離された時 : drawLine を呼び出す.

31, 32行目では canvas をウィンドウに貼り付けて, それを表示しています.

35行目の drawLine は今回のマウスイベントの位置を取得し, 前回の点から strokeLine を利用して線分を伸ばします. その後, 今回の点を保存します.

When launch in main is called, start of new InputLines object is called.

From line 24 to line 28, event handlers of canvas are set as follows;

  • When mouse is pressed, previous is initialized by beginning point.
  • When mouse is dragged or released, drawLine is called.

Line 31 sets canvas to the window and line 32 shows that.

Using strokeLine, drawLine at line 35 draws line from the point of current mouse event to previous point. After that, save current point.