Skip to content

Commit f60273d

Browse files
committed
refactor, add fx package with alt fx impl
1 parent 866c03a commit f60273d

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/main/scala/introprog/examples/TestPixelWindow.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ package introprog.examples
55
* PixelWindow for easy 2D game programming.
66
*/
77
object TestPixelWindow {
8-
import introprog._
8+
import introprog.PixelWindow
99

1010
/** A reference to an instance of class `PixelWindow`. */
11-
// val w = new PixelWindow(400, 400, "Hello PixelWindow!")
12-
val w = new FxPixelWindow(400, 400, "Hello FxPixelWindow!")
11+
val w = new PixelWindow(400, 400, "Hello FxPixelWindow!")
1312

1413
/** The color used by `square`. */
1514
var color = java.awt.Color.red

src/main/scala/introprog/Fx.scala renamed to src/main/scala/introprog/fx/Fx.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package introprog
1+
package introprog.fx
22

33
/** A wrapper with utils for simpler access to javafx. The main feature is the
44
* ability to create stages without having to treat the first stage as special:
55
* Simply do: `val myStage = Fx.newStage { s => ... }` and initialize a stage `s`
66
* The underlying javafx app ceremony is hidden. */
7-
object Fx {
7+
private[fx] object Fx {
88

99
def toFxColor(c: java.awt.Color): javafx.scene.paint.Color =
1010
javafx.scene.paint.Color.rgb(c.getRed, c.getGreen, c.getBlue, c.getAlpha / 255.0)

src/main/scala/introprog/FxPixelWindow.scala renamed to src/main/scala/introprog/fx/FxPixelWindow.scala

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package introprog
1+
package introprog.fx
22

3-
/** A module ready to use in the Scala REPL or in a main Scala program */
3+
/** A window for pixel-based drawing in an underlying javafx window. */
44
object FxPixelWindow {
55
def exit(): Unit = System.exit(0)
66

@@ -103,8 +103,9 @@ class FxPixelWindow(
103103
/** Draw a line from (`x1`, `y1`) to (`x2`, `y2`) using `color` and `lineWidth`. */
104104
def line(x1: Int, y1: Int, x2: Int, y2: Int, color: java.awt.Color = foreground, lineWidth: Int = 1): Unit = withGC { gc =>
105105
gc.setStroke(Fx.toFxColor(color))
106-
gc.setLineWidth(lineWidth)
107-
gc.strokeLine(x1,y1,x2,y2)
106+
gc.setLineWidth(lineWidth.toDouble)
107+
gc.strokeLine(x1.toDouble,y1.toDouble,x2.toDouble,y2.toDouble)
108+
gc.strokeLine(x1.toDouble,y1.toDouble,x2.toDouble,y2.toDouble)
108109
}
109110

110111
// def setLineWidth(width: Double): Unit = withGC(_.setLineWidth(width))
@@ -118,7 +119,7 @@ class FxPixelWindow(
118119
def fill(x: Int, y: Int, width: Int, height: Int, color: java.awt.Color = foreground): Unit = withGC { gc =>
119120
//gc.setStroke(Fx.toFxColor(color))
120121
gc.setFill(Fx.toFxColor(color))
121-
gc.fillRect(x,y,width,height)
122+
gc.fillRect(x.toDouble,y.toDouble,width.toDouble,height.toDouble)
122123
}
123124

124125
def setPixel(x: Int, y: Int, color: java.awt.Color = foreground): Unit =
@@ -144,9 +145,9 @@ class FxPixelWindow(
144145
fontName: String = "Monospaced Bold"
145146
) = withGC { gc =>
146147
gc.setFill(Fx.toFxColor(color))
147-
gc.setFont(new javafx.scene.text.Font(fontName, size))
148+
gc.setFont(new javafx.scene.text.Font(fontName, size.toDouble))
148149
gc.setFontSmoothingType(javafx.scene.text.FontSmoothingType.LCD)
149-
gc.fillText(text, x, y + size)
150+
gc.fillText(text, x.toDouble, y.toDouble + size.toDouble)
150151
}
151152

152153

@@ -169,12 +170,12 @@ class FxPixelWindow(
169170
s.setTitle(title)
170171
root.setBackground(javafx.scene.layout.Background.EMPTY)
171172
canvas.getGraphicsContext2D.setFill(Fx.toFxColor(background))
172-
canvas.getGraphicsContext2D.fillRect(0,0,width,height)
173+
canvas.getGraphicsContext2D.fillRect(0.0,0.0,width.toDouble,height.toDouble)
173174
canvas.getGraphicsContext2D.setStroke(Fx.toFxColor(foreground))
174175
s.setScene(scene)
175176
root.setCenter(canvas)
176-
s.setMinWidth(width)
177-
s.setMinHeight(height)
177+
s.setMinWidth(width.toDouble)
178+
s.setMinHeight(height.toDouble)
178179
s.show
179180

180181
//if (initBasicMenu) root.getChildren.add(0, basicMenuBar)

0 commit comments

Comments
 (0)