在 scala 中绘制形状
我在用 scala 语言实现绘图时遇到了一些严重的问题。我当前的代码如下所示:
package edu.luc.cs.laufer.cs473.shapealgebra
import java.awt.Graphics2D
class Draw {
def draw(g: Graphics2D)(s: Shape): Unit = s match {
case Ellipse(hw, hh) => g.drawArc(-hw, -hh, 2 * hw, 2 * hh, 0, 360)
case Rectangle(w, h) => g.drawRect(0, 0, w, h)
case Location(x: Int, y: Int, shape: Shape) => {
g.translate(x, y)
draw(g)(shape)
g.translate(0,0)
}
case Group(shapes @ _*) => {
shapes foreach(draw(g)(_))
}
}
}
object Draw extends Draw {
def apply(g: Graphics2D) = draw(g)(_)
}
这里的问题是我的组案例。它无法正确绘制一组形状。这两个测试用例使用以下形状:
val simpleLocation = Location(70, 30, Rectangle(80, 120))
val complexGroup = Location(50, 100,
Group(
Ellipse(20, 20),
Location(150, 50,
Group(
Rectangle(50, 30),
Rectangle(300, 60),
Location(100, 200,
Ellipse(50, 50)
)
)
),
Rectangle(100, 200)
)
)
复合体继续失败,我不明白为什么。
package edu.luc.cs.laufer.cs473.shapealgebra
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import java.awt.image.BufferedImage
import TestFixtures._
@RunWith(classOf[JUnitRunner])
class TestDraw extends FunSuite with BufferedImageEquality {
test("simple") {
val s = simpleLocation
val i = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
Draw(i.createGraphics())(s)
val j = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
val g = j.createGraphics()
g.translate(70, 30)
g.drawRect(0, 0, 80, 120)
assertEquals(i, j)
}
test("complex") {
val s = complexGroup
val i = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
Draw(i.createGraphics())(s)
val j = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
val g = j.createGraphics()
paintComplexGroup(g)
assertEquals(i, j)
}
}
测试用例如上所示。我从单元测试的结果中得到“0 不等于 255”。
I am having some serious troubles implementing a draw in the scala language. my current code looks like:
package edu.luc.cs.laufer.cs473.shapealgebra
import java.awt.Graphics2D
class Draw {
def draw(g: Graphics2D)(s: Shape): Unit = s match {
case Ellipse(hw, hh) => g.drawArc(-hw, -hh, 2 * hw, 2 * hh, 0, 360)
case Rectangle(w, h) => g.drawRect(0, 0, w, h)
case Location(x: Int, y: Int, shape: Shape) => {
g.translate(x, y)
draw(g)(shape)
g.translate(0,0)
}
case Group(shapes @ _*) => {
shapes foreach(draw(g)(_))
}
}
}
object Draw extends Draw {
def apply(g: Graphics2D) = draw(g)(_)
}
The problem here is with my group case. It does not draw a group of shapes properly. The two test cases uses the following shapes:
val simpleLocation = Location(70, 30, Rectangle(80, 120))
val complexGroup = Location(50, 100,
Group(
Ellipse(20, 20),
Location(150, 50,
Group(
Rectangle(50, 30),
Rectangle(300, 60),
Location(100, 200,
Ellipse(50, 50)
)
)
),
Rectangle(100, 200)
)
)
The complex continues to fail and I can't figure out why.
package edu.luc.cs.laufer.cs473.shapealgebra
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import java.awt.image.BufferedImage
import TestFixtures._
@RunWith(classOf[JUnitRunner])
class TestDraw extends FunSuite with BufferedImageEquality {
test("simple") {
val s = simpleLocation
val i = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
Draw(i.createGraphics())(s)
val j = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
val g = j.createGraphics()
g.translate(70, 30)
g.drawRect(0, 0, 80, 120)
assertEquals(i, j)
}
test("complex") {
val s = complexGroup
val i = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
Draw(i.createGraphics())(s)
val j = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB)
val g = j.createGraphics()
paintComplexGroup(g)
assertEquals(i, j)
}
}
The test case is shown above. I get a "0 did not equal 255" from the result of the unit test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你看看
translate
做了什么,Javadoc 会说所以
什么也不做。尝试
一下,如果是我,我不会弄乱 Graphics 对象的原点。我会修改
drawRect
和drawArc语句来获取位置,并将位置坐标作为参数传递给该方法。If you look at what
translate
does, the Javadoc saysSo
does nothing. Try
Although, if it were me, I wouldn't mess with the Graphics object's origin. I'd modify the
drawRect
anddrawArc
statements to take a location, and pass the location co-ordiates as an argument to the method.我看到您正在对
g: Graphics2D
调用translate
,这会改变该对象。但是,一旦完成绘制翻译的对象,您就不会撤消该突变。此外,还有一个问题是您是否期望翻译是累积的(例如,第二个翻译是绝对200、150,而不是绝对150、50)。这可能是您看到的问题吗?
I see you are calling
translate
ong: Graphics2D
, which mutates that object. However, you are not undoing that mutation once you finish drawing the translated objects. Furthermore, there's also the question of whether you expect the translation to be cumulative (for example, the second translation be absolute 200, 150, instead of absolute 150, 50).Might this the problem you see?