在 scala 中创建的图像看起来不像预期的那样。不知道为啥
Posted
技术标签:
【中文标题】在 scala 中创建的图像看起来不像预期的那样。不知道为啥【英文标题】:image created in scala does not look as expected. not sure why在 scala 中创建的图像看起来不像预期的那样。不知道为什么 【发布时间】:2020-06-03 00:27:15 【问题描述】:我首先创建一个值为 2 的二维数组。然后将数组右下角的一些值设置为 5
var map = Array.fill(5, 10)(2);
map(4)(9) = 5
map(3)(9) = 5
map(4)(8) = 5
map(3)(8) = 5
此数组将由图像表示。每个数字 2 应该是一个蓝色方块。其余的应该是白色的
import scala.io.Source
import java.io._
import java.awt.image.BufferedImage
import java.awt.Graphics2D,Color,Font,BasicStroke
import java.awt.geom._
object DRAWPROB
def draw(map : Array[Array[Int]])
val size = (30 * (map(0).length-1), 30 * (map.length-1))
val canvas = new BufferedImage(size._1, size._2, BufferedImage.TYPE_INT_RGB)
val g = canvas.createGraphics()
g.setColor(Color.WHITE)
g.fillRect(0, 0, canvas.getWidth, canvas.getHeight)
g.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON)
var d = 0;
for(d <- 0 to map.length-1)
var e = 0;
for(e <- 0 to map(0).length-1)
g.setColor(Color.BLUE)
if(map(d)(e) == 2) g.fill(new Rectangle2D.Double(d*30.0, e*30.0, 30.0, 30.0)); // ill find a way to do this once i can load the image
else println("not at "+ d + " " + e)
g.dispose()
javax.imageio.ImageIO.write(canvas, "png", new java.io.File("island_drawing.png"))
它只是做了 1 个大的蓝色方块,但我希望右下角缺少一个部分
expectation vs reality
我为什么会得到这个
【问题讨论】:
这能回答你的问题吗? How to combine multiple PNGs into one big PNG file? 【参考方案1】:此代码部分的主要问题:new Rectangle2D.Double(d*30.0, e*30.0...
- 第一个参数是 x
坐标,因此它应该乘以 map
列值,而不是 row ,并且与第二个参数 e*30
相同,即 @ 987654325@ 所以它应该乘以map
行值。
我已经重构了你的代码以便更好地理解,至少对我自己来说,所以请看一个例子:
import java.awt.image.BufferedImage
import java.awt.Color, Rectangle, RenderingHints
import javax.imageio.ImageIO
import scala.reflect.ClassTag
/**
* Immutable table model, possibly empty inside
*/
class Table[T](underlying: Array[Array[T]])
def columnsCount: Int = underlying.headOption.map(_.length).getOrElse(0)
def rowsCount: Int = underlying.length
def rows: Seq[Int] = underlying.indices
def columns: Seq[Int] = underlying.headOption.map(_.indices).getOrElse(Nil)
def apply(row: Int, column: Int): Option[T] =
val rowExists = row < rowsCount && row >= 0
val columnExists = column < columnsCount && column >= 0
if(rowExists && columnExists) Some(underlying(row)(column)) else None
def update(row: Int, column: Int, value: T): Table[T] =
val copy = underlying.clone()
copy(row)(column) = value
new Table[T](copy)
def foreach[U](f: ((Int, Int, T)) => U): Unit =
val rowColumnValue = for
row <- rows
column <- columns
value <- apply(row, column)
yield (row, column, value)
rowColumnValue.foreach[U](f)
override def toString: String = underlying.map(_.mkString(",")).mkString("\n")
object Table
def apply[T: ClassTag](rows: Int, columns: Int, initValue: T): Table[T] =
new Table[T](Array.fill(rows, columns)(initValue))
class Draw(cellSize: Int)
def draw(table: Table[Int])
val width = cellSize * table.columnsCount
val height = cellSize * table.rowsCount
val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val graphics = canvas.createGraphics()
graphics.setColor(Color.WHITE)
graphics.fillRect(0, 0, canvas.getWidth, canvas.getHeight)
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
table.foreach case (row, column, value) =>
if (value == 2)
graphics.setColor(Color.BLUE)
// This is main fix part
val x = column * cellSize
val y = row * cellSize
graphics.fill(new Rectangle(x, y, cellSize, cellSize))
graphics.dispose()
ImageIO.write(canvas, "png", new java.io.File("island_drawing.png"))
object DrawApp
def main(args: Array[String]): Unit =
val draw = new Draw(30)
val table = Table(5, 10, 2)
.update(4 ,9, 5)
.update(4, 8, 5)
.update(3, 9, 5)
.update(3, 8, 5)
println(table)
draw.draw(table)
这产生了预期的图像。希望这会有所帮助!
【讨论】:
以上是关于在 scala 中创建的图像看起来不像预期的那样。不知道为啥的主要内容,如果未能解决你的问题,请参考以下文章
glBindAttribLocation 不像预期的那样工作