Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,33 +1,26 @@
import scala.swing._
import scala.swing.Swing._
import scala.actors._
import scala.actors.Actor._
import java.awt.{Color, Graphics}
import java.awt.Color
import scala.actors.Actor
import scala.swing.{ Graphics2D, MainFrame, Panel, SimpleSwingApplication }
import scala.swing.Swing.pair2Dimension
object Pendulum extends SimpleSwingApplication {
val length = 100
val prefSizeX = 2*length+50
val prefSizeY = length/2*3
val length = 100
lazy val ui = new Panel {
import math._
import scala.math.{ cos, Pi, sin }
background = Color.white
preferredSize = (prefSizeX, prefSizeY)
preferredSize = (2 * length + 50, length / 2 * 3)
peer.setDoubleBuffered(true)
var angle: Double = Pi/2;
var angle: Double = Pi / 2
def pendular = new Actor {
var angleAccel, angleVelocity = 0.0;
var dt = 0.1
var angleVelocity = 0.0
val dt = 0.1
def act() {
while (true) {
angleAccel = -9.81 / length * sin(angle)
angleVelocity += angleAccel * dt
angleVelocity += (-9.81 / length * sin(angle)) * dt
angle += angleVelocity * dt
repaint()
Thread.sleep(15)
@ -38,14 +31,11 @@ object Pendulum extends SimpleSwingApplication {
override def paintComponent(g: Graphics2D) = {
super.paintComponent(g)
g.setColor(Color.white);
g.fillRect(0, 0, size.width, size.height);
val anchorX = size.width / 2
val anchorY = size.height / 4
val ballX = anchorX + (sin(angle) * length).toInt
val ballY = anchorY + (cos(angle) * length).toInt
val (anchorX, anchorY) = (size.width / 2, size.height / 4)
val (ballX, ballY) =
(anchorX + (sin(angle) * length).toInt, anchorY + (cos(angle) * length).toInt)
g.setColor(Color.lightGray)
g.drawLine(anchorX-2*length, anchorY, anchorX+2*length, anchorY)
g.drawLine(anchorX - 2 * length, anchorY, anchorX + 2 * length, anchorY)
g.setColor(Color.black)
g.drawLine(anchorX, anchorY, ballX, ballY)
g.fillOval(anchorX - 3, anchorY - 4, 7, 7)
@ -58,7 +48,7 @@ object Pendulum extends SimpleSwingApplication {
def top = new MainFrame {
title = "Rosetta Code >>> Task: Animate a pendulum | Language: Scala"
contents = ui
centerOnScreen
ui.pendular.start
}
}