在 scala 中绘制形状

发布于 2024-12-08 20:32:19 字数 2061 浏览 0 评论 0原文

我在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2024-12-15 20:32:19

如果你看看 translate 做了什么,Javadoc 会说

“将 Graphics2D 上下文的原点转换为点 (x, y)
当前坐标系中。”

所以

  g.translate(0,0)

什么也不做。尝试

  g.translate(-x, -y)

一下,如果是我,我不会弄乱 Graphics 对象的原点。我会修改 drawRect和drawArc语句来获取位置,并将位置坐标作为参数传递给该方法。

If you look at what translate does, the Javadoc says

"Translates the origin of the Graphics2D context to the point (x, y)
in the current coordinate system."

So

  g.translate(0,0)

does nothing. Try

  g.translate(-x, -y)

Although, if it were me, I wouldn't mess with the Graphics object's origin. I'd modify the drawRect and drawArc statements to take a location, and pass the location co-ordiates as an argument to the method.

痴情 2024-12-15 20:32:19

我看到您正在对 g: Graphics2D 调用 translate,这会改变该对象。但是,一旦完成绘制翻译的对象,您就不会撤消该突变。此外,还有一个问题是您是否期望翻译是累积的(例如,第二个翻译是绝对200、150,而不是绝对150、50)。

这可能是您看到的问题吗?

I see you are calling translate on g: 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文