我从在这里找到的代码开始,如下所示:
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 中遇到错误。我的论据太少了。我不知道如何在这里准确地添加行和列的新参数。