Search code examples
imageswingscalapanelgridpanel

ImagePanel adaptation in scala


I am starting with the code which was found here and can be seen below:

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()                                              
} 

Usage:

object ImagePanelDemo extends SimpleSwingApplication
{

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

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

I want to extend this and give the image panel the form of a GridPanel. I want the Image panel to be a GridPanel with an image background. Does anyone know how to implement this?

My current implementation is as follows:

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()                                              
}

I get an error in the object ImagePanel. I have too few arguments. I don't know how to exactly add the new arguments of rows and columns here.


Solution

  • All you have to do is to subclass GridPanel instead. If you change your first line for

    class ImagePanel(rows0: Int, cols0: Int) extends GridPanel(rows0, cols0)
    

    it works.

    EDIT:

    I don't know what error you get—of course now you need to create the ImagePanel with arguments for the number of rows and columns...

    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