Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6933423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:49:51+00:00 2026-05-27T11:49:51+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:49:52+00:00Added an answer on May 27, 2026 at 11:49 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am starting to use some Java code which was written by someone else.
Kind of looking for affirmation here. I have some hand-written code, which I'm not
I have been playing with image flood fill that I found here on Stack
I'm starting to code up my own window manager, and was wondering how to
I mainly code small programs for myself, but recently, I've been starting to code
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have the following code print Starting stage 1<br> # Something that takes about
The code bellow is the small starting point for a new way ( for
I'm starting to investigate T4 for Code Generation. I get that you have a
We are starting to write more and more code for an ASP.Net web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.