gpt4 book ai didi

Scala - 简单地将 2D 数组写入屏幕的最简单的 2D 图形?

转载 作者:行者123 更新时间:2023-12-04 09:42:44 25 4
gpt4 key购买 nike

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

6年前关闭。




Improve this question




对于将二维像素数组写入屏幕,您有什么建议?

我的第一个想法是一些 SWT 绑定(bind),但还有其他绑定(bind)吗?也许处理?

最佳答案

在 Swing 中并不太难 - 您可以剪切和粘贴下面的内容。如果您不想要颜色或绘制到任何大小的窗口的能力,或者它总是相同的大小,您可以稍微简化它。

定义一个 Panel 类:

class DataPanel(data: Array[Array[Color]]) extends Panel {

override def paintComponent(g: Graphics2D) {
val dx = g.getClipBounds.width.toFloat / data.length
val dy = g.getClipBounds.height.toFloat / data.map(_.length).max
for {
x <- 0 until data.length
y <- 0 until data(x).length
x1 = (x * dx).toInt
y1 = (y * dy).toInt
x2 = ((x + 1) * dx).toInt
y2 = ((y + 1) * dy).toInt
} {
data(x)(y) match {
case c: Color => g.setColor(c)
case _ => g.setColor(Color.WHITE)
}
g.fillRect(x1, y1, x2 - x1, y2 - y1)
}
}
}

然后制作一个 Swing 应用程序:
import swing.{Panel, MainFrame, SimpleSwingApplication}
import java.awt.{Color, Graphics2D, Dimension}

object Draw extends SimpleSwingApplication {

val data = // put data here

def top = new MainFrame {
contents = new DataPanel(data) {
preferredSize = new Dimension(300, 300)
}
}
}

您的数据可能类似于
  val data = Array.ofDim[Color](25, 25)

// plot some points
data(0)(0) = Color.BLACK
data(4)(4) = Color.RED
data(0)(4) = Color.GREEN
data(4)(0) = Color.BLUE

// draw a circle
import math._
{
for {
t <- Range.Double(0, 2 * Pi, Pi / 60)
x = 12.5 + 10 * cos(t)
y = 12.5 + 10 * sin(t)
c = new Color(0.5f, 0f, (t / 2 / Pi).toFloat)
} data(x.toInt)(y.toInt) = c
}

这会给你:

enter image description here

您可以轻松使用 map在现有数组上运行以将其映射到颜色。

关于Scala - 简单地将 2D 数组写入屏幕的最简单的 2D 图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com