Scala中的ImagePanel适应

时间:2011-12-12 10:15:38

标签: image swing scala panel gridpanel

我从找到here的代码开始,可以在下面看到:

import swing._                                                                

import java.awt.image.BufferedImage                                           
import java.io.File                                                           
import javax.imageio.ImageIO                                                  

class ImagePanel extends Panel                                                
{                                                                             
  private var _imagePath = ""                                                 
  private var bufferedImage:BufferedImage = null                              

  def imagePath = _imagePath                                                  

  def imagePath_=(value:String)                                               
  {                                                                           
    _imagePath = value                                                        
    bufferedImage = ImageIO.read(new File(_imagePath))                        
  }                                                                           


  override def paintComponent(g:Graphics2D) =                                 
  {                                                                           
    if (null != bufferedImage) g.drawImage(bufferedImage, 0, 0, null)         
  }                                                                           
}                                                                             

object ImagePanel                                                             
{                                                                             
  def apply() = new ImagePanel()                                              
} 

用法:

object ImagePanelDemo extends SimpleSwingApplication
{

  def top = new MainFrame {
    title = "Image Panel Demo"

    contents = new ImagePanel
    {   
      imagePath = ("../testImage.jpg")
    }   
  }
}

我想扩展它,并为图像面板提供GridPanel的形式。我希望图像面板是带有图像背景的GridPanel。有谁知道如何实现这个?

我目前的实施如下:

class ImagePanel(rows0: Int, cols0: Int) extends GridPanel(rows0, cols0)                                                
{                                                                             
  private var _imagePath = ""                                                 
  private var bufferedImage:BufferedImage = null                              

  def imagePath = _imagePath                                                  

  def imagePath_=(value:String)                                               
  {                                                                           
    _imagePath = value                                                        
    bufferedImage = ImageIO.read(new File(_imagePath))                        
  }                                                                           


  override def paintComponent(g:Graphics2D) =                                 
  {                                                                           
    if (null != bufferedImage) g.drawImage(bufferedImage, 0, 0, null)         
  }                                                                           
}                                                                             

object ImagePanel                                                             
{                                                                             
  def apply() = new ImagePanel()                                              
}

我在对象ImagePanel中收到错误。我的论点太少了。我不知道如何在这里准确添加行和列的新参数。

1 个答案:

答案 0 :(得分:3)

您所要做的就是继承GridPanel。如果您更改

的第一行
class ImagePanel(rows0: Int, cols0: Int) extends GridPanel(rows0, cols0)

它有效。

修改

我不知道你得到了什么错误 - 当然你需要创建带有行数和列数参数的ImagePanel ...

import scala.swing._
import java.awt.image.BufferedImage
import java.net.URL
import javax.imageio.ImageIO

class ImagePanel(rows0: Int, cols0: Int) extends GridPanel(rows0, cols0) {
  private var _imagePath = ""                                                 
  private var buf = Option.empty[BufferedImage]

  def imagePath = _imagePath
  def imagePath_=(value: String): Unit = {
    _imagePath = value
    buf.foreach(_.flush()); buf = None
    buf = Some(ImageIO.read(new URL(value)))
    repaint()
  }

  override def paintComponent(g: Graphics2D): Unit = {
    super.paintComponent(g)
    buf.foreach(g.drawImage(_, 0, 0, null))
  }
}

val f        = new Frame()
val p        = new ImagePanel(3, 2)
p.imagePath  = "http://www.scala-lang.org/api/current/lib/package_big.png"
p.contents ++= Seq.tabulate(p.rows * p.columns)(i => new Label((i + 1).toString))
f.contents   = p
f.visible    = true