Scalaã§ã²ã¼ã ããã°ã©ãã³ã° - vol1 Windowã表示ãã¦ã¿ã -
Javaã§ä½ãããã«JFrameãç¨ãã¦è¡¨ç¤ºãã¾ããã²ã¼ã ããã°ã©ãã³ã°åãã«é«éã§å¦çã§ããããã¢ã¯ãã£ãã¬ã³ããªã³ã°ããã¾ãã
ãã®ãããã¯ã¬ã¤ã¤ãä½ãã®ã§ãScalaã£ã¦ããããJavaã£ã½ããããããããã¾ããã(ã©ã¤ãã©ãªçã«)
importã«ã¤ãã¦
Scalaã®å ´åãå
¨ã¦ãé¸æããå ´åã«ã_ããå©ç¨ãã¾ãã
import javax.swing._ import java.awt._ import java.awt.image._
swingã¨ããã®ã¯awtã®æ¡å¼µã§ãã
awt㯠" abstruct window toolkit " ã®ç¥ã®ããã§windowå¨ãã®ç®¡çã«ä½¿ãã¾ãã
Environment
ã¾ããç°å¢ã«åãããè¨å®ãã¡ã¤ã«ãä½ãã¾ãããã(å¾ã®å¤æ´ã«å¼·ããªãã¾ã)
objectã¨ããsingletonãã¼ã¯ã¼ããç¨ãã¦ã©ãããã§ãã¢ã¯ã»ã¹ã§ããããã«ãã¦ããã¾ãã
ããã¯å®ç¾©ã ããªã®ã§ããã£ã¨è¦ã¨ãã¦ãã ãããã
object Environment { //----------------------------------------------------- // GENERAL //----------------------------------------------------- object General { val VERSION = "0.0.1" } //----------------------------------------------------- // SCREEN //----------------------------------------------------- object Screen { val TITLE = "drons" // GAME TITLE val WIDTH = 640 // WINDOW WIDTH val HEIGHT = 480 // WINDOW HEIGHT val DEPTH = 32 // BIT COLOR DEPTH } }
valã¯åºå®å¤(immutable)ã表ãã¦ãã¦ããã°ã©ã ã®å®è¡ä¸ã«å¤æ´ããããã¨ã¯ããã¾ããã
ä»åã¯windowã表示ãããã ããªã®ã§ãå¹
ã¨é«ãã¨è²æ
å ±ãè¼ãã¦ããã¾ãã
DEPTH=32ã¨ããã®ã¯ã32bitã«ã©ã¼ã§32bit=2^32éãã®è²ã表ç¾ãããã¨ãã§ãã¾ãã
大ããã«ã¤ãã¦ã¯640x480ã§ãããã¨ãå¤ãã§ãã(800x600ãã¡ããã¡ãã)
MainFrame extends JFrame
Scalaã®å ´åã¯ã³ã³ã¹ãã©ã¯ã¿ãå®ç¾©ããªãã¦ãä½ãç¡ãã¨ãããã³ã³ã¹ãã©ã¯ã¿ã«ãããããã§ãã
class A { println("hello") } // => hello
JFrameã¨ããã¦ã£ã³ãã¦ç®¡çã¯ã©ã¹ãç¶æ¿ãã¾ããããå½¼ãä»å¾ç®¡çãã¦ããã¾ãã
ã¡ãªã¿ã«ãprivateãªãã®ã¯å¾ãã«æã£ã¦ããã¨ä½¿ãç«å ´ã®äººã¯æ¥½ã§ããã(å
±åéçºã®æãªããã«)
frame.scala
class MainFrame extends JFrame { //----------------------------------------------------- // objects //----------------------------------------------------- val WIDTH = Environment.Screen.WIDTH val HEIGHT = Environment.Screen.HEIGHT val DEPTH = Environment.Screen.DEPTH def update() = { val buffer = getBufferStrategy() if( !buffer.contentsLost ) buffer.show //else Debugger.push("\nerror!\n Contents Lost\n") Toolkit.getDefaultToolkit().sync() } //----------------------------------------------------- // initialize //----------------------------------------------------- private val mBufferStrategy = getBufferStrategy() { setTitle(Environment.Screen.TITLE) setBounds(0,0,WIDTH,HEIGHT) setIgnoreRepaint(true) setResizable(false) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) setVisible(true) val BUFFER_NUM = 2 createBufferStrategy(BUFFER_NUM) } }
ã¨ããããã³ã³ã¹ãã©ã¯ã¿ããè¦ã¦ããã¾ããããã
ã¦ã£ã³ãã¦ã®åºæ¬æ å ±
setTitle(Environment.Screen.TITLE) setBounds(0,0,WIDTH,HEIGHT) setIgnoreRepaint(true) setResizable(false)
ãã®ãããã¯é¢æ°åããæ¨æ¸¬ã§ãããã§ããã
setTitleã¯ã¦ã£ã³ãã¦ã®ã¿ã¤ãã«ãã¼ã®æååãè¨å®ã§ãã¾ãã
ãã®é¢æ°ã¯éè¦ã§ããéçºã®åæã®ã¿ã¤ãã³ã°ã§ã¯ãããã°ã«ä½¿ãã¾ãã
setBoundsã¯ã¦ã£ã³ãã¦ã®å¤§ããã決ãã¦ããããã§ãã(0,0)ãã(WIDTH,HEIGHT)ã¾ã§ã®å¤§ããã¨ãããã¨ã§ãã
setResizableã¯ã¦ã£ã³ãã¦ã®å¤§ãããå¤æ´ã®å¯å¦ã§ãã
setIgnoreRepaintã«ã¤ãã¦ã§ããåæç»ãç¡è¦ããã¨ããæå³ã«ãªãã¾ãã
æç»å¦çã¯èªåã§å¶å¾¡ãããã®ã§ãã¡ãã®ã¿ã¤ãã³ã°ã®ã¿ã«ããããã«èªåã§æç»ããã®ã¯ç¡è¦ããã¦ããã¾ãã
çµäºæ å ±ã®è¨å®
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setVisible(true)
ã¦ã£ã³ãã¦ãéããã¨ãã®å¦çã決å®ãã¾ãã
å³ä¸ã®ãããã³ãæ¼ãã¨çµäºããããã«è¨å®ãã¾ãã
setVisibleãtrueã«ãããã¨ã§ããããã¦ã£ã³ãã¦ã表示ããã¾ãã
ããã«ãããã¡ãªã³ã°
val BUFFER_NUM = 2 createBufferStrategy(BUFFER_NUM)
setVisibleãã¦ããã¨ããã®ãå¤ãªæ°ããã¾ãããé çªãå¤ããã¨åãã¾ãããã
createBufferStrategy()ã«ã¦ãããã¡ãçæãã¾ãã
ããã§ã¯ç»é¢ï¼æåã®ãããã¡ãçæãã¦ããããã§ããã
ãã®ï¼æã®ç»é¢ã交äºã«åãæ¿ãã¦è¡¨ç¤ºãã¾ããããã«ãã£ã¦æ¿ããæç»ã§ããã©ãããé²æ¢ãããããã§ãã
ãã¦ãããããupdateé¢æ°ãè¦ã¦ã¿ã¾ãããã(def updateã§ãã)
ç»é¢ã®æ´æ°(æ¯ãã¬ã¼ã å¼ã°ãã)
def update() = { val buffer = getBufferStrategy() if( !buffer.contentsLost ) buffer.show //else Debugger.push("\nerror!\n Contents Lost\n") Toolkit.getDefaultToolkit().sync() }
ã¾ããgetBufferStrategyã«ã¦ç»é¢ã®ãããã¡(ã¡ã¢ãªæ
å ±)ãåå¾ãã¾ãã
contentsLostã®é¢æ°ã§ã¯ãããã¡ãæ¶å¤±ãã¦ãããã©ãããå¤å®ã§ãã¾ãã
æ£å¸¸ã§ããã°ãbuffer.show()ã«ã¦ç»é¢ãåãæ¿ããæå¾ã«Toolkitã®syncé¢æ°ãç¨ãã¦æ´æ°ãã¾ãã
以ä¸ãåºæ¬çãªå
容ã§ããããã§ã¯ã表示ããã¦ã¿ã¾ãããã
main.scala
object Drons { def main(args:Array[String]) : Unit = { val frame = new MainFrame; while(true) { frame.update() Thread.sleep(20) } } }
é©åã«ã¨ã³ããªã¼ãã¤ã³ãã¨ãªããªãã¸ã§ã¯ãã¨ã¡ã½ãããå®ç¾©ãã¾ãã
ããã«å
ã»ã©ä½ã£ãMainFrameã¯ã©ã¹ãçæãã¦ãç¡éã«ã¼ãã§ãã
ã©ãã§ãããã表示ãããã§ããããã
次ã¯ãã¼å
¥åããã£ã¦ãããã«ã¹ã¯ãªã¼ã³åãããã¨æãã¾ãã
ãã«ã¹ã¯ãªã¼ã³ããæ»ããªãã¨å°ãã¾ããããã¼ã(æéå¶ã§ãããã®ã ããã©ã)