1

我从在这里找到的代码开始,如下所示:

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 中遇到错误。我的论据太少了。我不知道如何在这里准确地添加行和列的新参数。

4

1 回答 1

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
于 2011-12-12T10:29:22.657 回答