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.
All you have to do is to subclass
GridPanelinstead. If you change your first line forit works.
EDIT:
I don’t know what error you get—of course now you need to create the
ImagePanelwith arguments for the number of rows and columns…