Scala Swing 图像

发布于 2024-11-02 16:38:49 字数 34 浏览 1 评论 0原文

如何在 Scala Swing 组件中加载和显示图像?

How do I load and display an image in a Scala Swing component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-11-09 16:38:49

我通常使用 Label 和 ImageIcon 来实现...例如:

val label = new Label {
  icon = new ImageIcon("path to the image file")
}

I usually use Label with ImageIcon for that ... like:

val label = new Label {
  icon = new ImageIcon("path to the image file")
}
他是夢罘是命 2024-11-09 16:38:49

下面是一个 ImagePanel 的实现,它扩展了 scala 的 Panel

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

Here is an implementation of an ImagePanel that extends scala's Panel

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")
    }   
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文