雪見酒を改良してみた。

YukiMiColor

どこにくっついていた雪が一番成長していたのか?
地面に絵の具をぶちまけ、雪が漂着したらその色に染まるようにしてみた。
カラフルになったが、想像通りの結果で、イマイチ。
種の繁栄と滅亡のようにも見える。

package asanmath
    
import java.awt.Color
import java.awt.image.BufferedImage
import javax.swing._

/** 雪見酒 TURBO GRAPHICS P89 */
object YukiMiColor {
    val Width = 640
    val Height = 1000
    val Blank = 0xFF000000
    val image = new BufferedImage(Width, Height, BufferedImage.TYPE_4BYTE_ABGR)
    var top:Int = Height - 1        // 雪のもっとも高い位置

    /** 0..(s-1)の乱数を生成する */
    def random(s:Int):Int = (math.random * s).toInt

    /** 雪がくっついた色は? */
    def getStickColor(x:Int, y:Int):Option[Int] = {
        val nextLine = y + 1
        //println(x+" "+y)
        val c = image.getRGB(x % Width, nextLine)
        if (c != Blank) return Some(c)
        val c1 = image.getRGB((x + Width - 1) % Width, nextLine)
        if (c1 != Blank) return Some(c1)
        val c2 = image.getRGB((x + 1) % Width, nextLine)
        if (c2 != Blank) return Some(c2)
        None
    }

    def falling() {
        var y = top - 40
        var x = random(Width)
        var color:Option[Int] = None
        do {
            x = random(3) match {
                case 0 => x
                case 1 => (x + Width - 1) % Width
                case 2 => (x + 1) % Width
            }
            y += 1
            color = getStickColor(x, y)
        } while (color == None/* && y < Height - 2*/)
        if (y < top) top = y
        image.setRGB(x, y, color.get)
        println(x+" "+y)
    }

    def main(args:Array[String]) {
        val frame = new JFrame("カラフルな雪見酒")
        val panel = new JLabel(new ImageIcon(image))
        frame.getContentPane.add(panel)
        frame.pack()
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        frame.setVisible(true)
        val g = image.getGraphics()
        g.setColor(Color.BLACK)
        g.fillRect(0, 0, Width, Height)
        // カラフルな絵の具を敷き詰める
        for (x <- 0 until Width) {
            val hue = x.toFloat / Width
            val c = Color.getHSBColor(hue, 1.0f, 1.0f)
            g.setColor(c)
            g.drawLine(x, Height-1, x, Height-1)
        }

        top = Height - 1
        while (top > 40) {
            falling()
            panel.repaint()
            Thread.sleep(1)
        }
        println("fallen.")  // 積もった!
    }
}