Skip to content

Instantly share code, notes, and snippets.

@takedasoft
Created November 6, 2008 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takedasoft/22547 to your computer and use it in GitHub Desktop.
Save takedasoft/22547 to your computer and use it in GitHub Desktop.
/*
もうちょっと関数と描画ロジックを分離したい
Type Bindingとか使ってかっこよく抽象化したい。
*/
package takedasoft
import javax.swing.{JPanel,JFrame}
import java.awt.{Color,Graphics,Graphics2D,Dimension}
import java.awt.image.{BufferedImage}
import scala.concurrent.ops.spawn
abstract class PlotterPanel extends JPanel {
var plotter:Plotter = _
def preparePlotter(graphics:Graphics2D){
plotter = new Plotter(graphics)
}
def drawTo(dx:double, dy:double){
plotter.drawRelative(dx, dy)
//println( dx + "," + dy )
repaint()
}
def moveTo(dx:double, dy:double){
plotter.moveTo(dx, dy)
}
def sleep(time:Int){
Thread.sleep(time)
}
}
class CaosPlotterPanel(caos:Dragon) extends PlotterPanel {
caos.panel = this
val WIDTH:Int = 400
val HEIGHT:Int = 400
setPreferredSize(new Dimension( WIDTH , HEIGHT ))
var image:BufferedImage = null //描画領域
override def paint(g:Graphics) {
if (image == null) startDraw()
else g.drawImage(image, 0, 0, this) //imageからアプレットへのコピー
}
def startDraw(){
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB)
var graphics:Graphics2D = image.createGraphics
graphics.translate(0, HEIGHT)
graphics.scale(1, -1)
graphics.setColor(Color.red)
graphics.setBackground(getBackground())
preparePlotter(graphics)
moveTo(100, 100)
draw
}
def draw(){
spawn { caos.draw( 10, 200, 0, 1 ) }
}// end of draw
}//end of class CaosPlotterPanel
trait Caos{
var panel:PlotterPanel = _
def draw( n:int, dx:double, dy:double, sign:int )
}
class Dragon extends Caos{
def draw( n:int, dx:double, dy:double, sign:int ):Unit = {
if( n == 0 ){
panel.drawTo(dx,dy)
panel.sleep(100)
} else {
draw(n - 1, (dx - sign * dy) / 2, (dy + sign * dx) / 2, 1)
draw(n - 1, (dx + sign * dy) / 2, (dy - sign * dx) / 2, -1)
}
}
}
class CaosPlotter(title:String) extends JFrame{
var panel:PlotterPanel = new CaosPlotterPanel(new takedasoft.Dragon);
getContentPane.add(panel)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack()
setTitle(title)
setResizable(false)
show
}
object CaosPlotterMain extends Application{
new CaosPlotter("dragon curve plotter")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment