Guest User

Untitled

a guest
Feb 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package jp.takeda_soft.examples
  2.  
  3. import javax.swing.{JPanel,JFrame,JButton}
  4. import java.awt.{Color,Graphics,Point,BorderLayout}
  5. import java.awt.event.{ActionListener,ActionEvent}
  6.  
  7. /**
  8. JButton+Actionの記述をDSL化する
  9. */
  10. class SButton extends JButton{
  11. def this(label:String,action: => Unit){
  12. this()
  13. this.setText(label)
  14. this.addActionListener( new ActionListener{
  15. def actionPerformed( e:ActionEvent ){
  16. //特に意味はない。キャストってこれでいいのか?
  17. val b:SButton = e.getSource.asInstanceOf[SButton]
  18. //コードブロックの実行
  19. action
  20. }
  21. }
  22. )
  23. }
  24. }
  25.  
  26. /**
  27. 設定した関数の計算結果を描画するプロッター。
  28. */
  29. class Plotter(var func:Int => Int) extends JPanel{
  30.  
  31. /**
  32. Swing機構からコールされるフックメソッド。
  33. 全描画処理のエントリポイント。
  34. */
  35. override def paintComponent(g:Graphics) {
  36. prepareAxis(g)
  37. def drawRed = draw(g)(Color.red)_ //curry(部分適用)
  38. drawRed( this.points() )
  39. }
  40.  
  41. /** 関数のセッター */
  42. def setFunc(func:Int=>Int){
  43. this.func = func
  44. }
  45.  
  46. /** Seq[Point]を線分で結ぶ */
  47. private def draw(g:Graphics)(color:Color)(points:Seq[Point]){
  48. g.setColor( color )
  49. for( i <- 0 to points.size - 2 ){
  50. g.drawLine( onx(points( i ).getX),ony(points( i ).getY),
  51. onx(points(i+1).getX),ony(points(i+1).getY) )
  52. }
  53. }
  54.  
  55. /** 設定した関数による各点の計算結果 */
  56. private def points():Seq[Point] = {
  57. val xs = -cx.toInt to cx.toInt //X軸の範囲
  58. //val xs2 = xs.start until xs.end by xs.step+1 //読み飛ばしもできる
  59. xs.map { x => new Point( x, func(x) ) }
  60. }
  61.  
  62. /**中心点 or 軸最大値*/
  63. private var cx = 0
  64. private var cy = 0
  65.  
  66. /**実点 to 描画点変換*/
  67. private def onx(x:Double):Int = cx+x.toInt
  68. private def ony(y:Double):Int = cy-y.toInt
  69.  
  70. /** 軸設定と描画 */
  71. private def prepareAxis(g:Graphics){
  72. cx = getWidth/2
  73. cy = getHeight/2
  74. g.setColor( Color.black )
  75. g.drawLine( cx, 0 ,cx, cy*2 )
  76. g.drawLine( 0, cy, cx*2, cy)
  77. }
  78. }
  79.  
  80. object Main extends JFrame {
  81.  
  82. val f7:Int=>Int = x => if( x > 0 ) f7(x-1) - 1
  83. else if( x < 0 ) f7(x+1) + 1
  84. else 0
  85.  
  86. /** 関数サンプル集 */
  87. val functions = List[Int=>Int](
  88. x => x
  89. , x => x*x / 200
  90. , Math.pow(_,3).toInt/40000
  91. , x => (100*Math.sin( 3.14 * x /180 )).toInt
  92. , x => Math.abs(x)
  93. , x => x match{ case 0 => Int.MaxValue case _ => 200/x }
  94. , x => x % 20 match{ case 0 => x case _ => 0 }
  95. , f7
  96. )
  97.  
  98.  
  99. /** 現在表示中の関数インデックス */
  100. var current:Int = 0
  101.  
  102. def main(args: Array[String]){
  103.  
  104. val plotter = new Plotter( functions(current) )
  105. val button = new SButton("change function",
  106. {
  107. current = if( current == functions.size - 1 ) 0
  108. else current + 1
  109. plotter.setFunc(functions(current))
  110. this.repaint()
  111. }
  112. )
  113. //Swing
  114. this.getContentPane().add(button,BorderLayout.SOUTH)
  115. this.getContentPane().add(plotter)
  116. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  117. this.setBounds(10, 10, 400, 400)
  118. this.setTitle("function plotter")
  119. this.show
  120. }
  121. }
Add Comment
Please, Sign In to add comment