Created
October 23, 2008 08:15
-
-
Save takedasoft/18959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ドラゴンカーブを描くアプレット | |
追々カオス理論のPlotterを作りたい。 | |
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 | |
class DragonPanel extends JPanel { | |
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()) | |
var plotter:Plotter = new Plotter(graphics) | |
plotter.moveTo(100, 100) | |
spawn { draw(plotter, 10, 200, 0, 1) } | |
} | |
def draw( pt:Plotter, n:int, dx:double, dy:double, sign:int ) { | |
if (n == 0) { | |
pt.drawRelative(dx, dy) | |
repaint() | |
Thread.sleep(10) | |
return | |
}else{ | |
draw(pt, n - 1, (dx - sign * dy) / 2, (dy + sign * dx) / 2, 1) | |
draw(pt, n - 1, (dx + sign * dy) / 2, (dy - sign * dx) / 2, -1) | |
} | |
} | |
} | |
object DragonPlotter extends Application{ | |
var panel:DragonPanel = new DragonPanel(); | |
var frame:JFrame = new JFrame(); | |
frame.getContentPane.add(panel) | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack() | |
frame.setTitle("dragon curve plotter") | |
frame.setResizable(false) | |
frame.show | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Plotter.scala | |
*/ | |
package takedasoft | |
import java.awt.{Graphics2D,BasicStroke} | |
import java.awt.geom.Line2D | |
class Plotter { | |
var g:Graphics2D = null | |
var x:double = 0 | |
var y:double = 0 | |
var line:Line2D.Double = new Line2D.Double() | |
def this(g:Graphics2D){ | |
this() | |
this.g = g | |
} | |
def setLineWidth(lineWidth:double):Unit={ | |
g.setStroke(new BasicStroke(lineWidth.toFloat)); | |
} | |
def moveTo( x:double, y:double ) { | |
this.x = x | |
this.y = y | |
} | |
def moveRelative( dx:double, dy:double ) { | |
moveTo(x + dx, y + dy); | |
} | |
def drawTo( x:double, y:double ) { | |
line.setLine(this.x, this.y, x, y) | |
g.draw(line) | |
this.x = x | |
this.y = y | |
} | |
def drawRelative( dx:double, dy:double ) { | |
drawTo(x + dx, y + dy); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment