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

  • Home
  • SEARCH
  • 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 7509059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:47:46+00:00 2026-05-29T22:47:46+00:00

I’ve been programming in Java for a little while now but have never really

  • 0

I’ve been programming in Java for a little while now but have never really done much with the swing packages. I am currently designing a GUI for an A.I. call and response program despite the relative complexity (for me at least) of the rest of what I have been doing, this image loading problem, which seemed extremely simple to implement is stumping me.

The below classes work if not in a package, which is what really confuses me. I’ve tried several different implementation suggestions (one from Head First Java, one from the docs.oracle.com tutorials and another using what http://leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html suggests).

package CaRII;


import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class P{


public static void main(String [] args){

    P p = new P();
    p.go();

}
public void go(){

    JFrame frame = new JFrame("CaRRI: Call and Response Intelligent Improviser");
    PBackground mainPanel = new PBackground();

    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);

    frame.setSize(800,500);
    frame.setVisible(true);


}

}

package CaRII;
import java.awt.*;
import javax.swing.*;

public class PBackground extends JPanel{

public Image backgroundImage;


public PBackground(){

    backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");

}

public PBackground(LayoutManager layout){

    backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");

}
public void paint(Graphics g){

    g.drawImage(backgroundImage,0,0,null);
}
}

Like I said the strange thing is that it doesn’t display the image if these two classes are in package CaRRI; but if I compile and run them without the package declaration they run fine(albiet the image not loading until the window resizes… but i have seen solutions online for that so I will be able to sort that once I get it loading within the package). I have been writing in XCode and JEdit and the image is stored within the package folder inside source (/src/CaRII/P.java … /src/CaRII/CaRIIBackGround.jpg), I have also tried storing the image in a resources folder within /src/ and using

ImageIcon(getClass().getResource("/resources/CaRIIBackGround.jpg)).getImage(); 

but that that causes another error when run

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at CaRII.PBackground.<init>(PBackground.java:19)
at CaRII.P.go(P.java:21)
at CaRII.P.main(P.java:15)

Any help would be much appreciated as despite its simplicity this has been stumping me all morning and I have a lot of other classes to write.

Thanks
(Heres the image(im a new user so i cant post images but this is a link to it))
http://imageshack.us/photo/my-images/189/cariibackground.jpg

  • 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-29T22:47:47+00:00Added an answer on May 29, 2026 at 10:47 pm
    package CaRII;
    
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    
    public class P{
    
        public static void main(String [] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    P p = new P();
                    p.go();
                }
            });
        }
    
        public void go(){
            try {
                JFrame frame = new JFrame("CaRRI: Call and Response Intelligent Improviser");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                PBackground mainPanel = new PBackground();
    
                frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    
                //frame.setSize(800,500);
                frame.pack();
                frame.setMinimumSize(frame.getSize());
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    class PBackground extends JPanel{
    
        public BufferedImage backgroundImage;
    
    
        public PBackground() throws Exception {
            URL url = new URL("http://desmond.imageshack.us/Himg189/" + 
                "scaled.php?server=189&filename=cariibackground.jpg&res=medium");
            // You might form that URL using something like..
            //URL url = this.getClass().getResource("/CaRIIBackGround.jpg");
    
            backgroundImage = ImageIO.read(url);
            Dimension d = new Dimension(
                backgroundImage.getWidth(),
                backgroundImage.getHeight());
            setPreferredSize(d);
        }
    
        /*  What was this supposed to achieve?
        public PBackground(LayoutManager layout){
            backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
        }
        */
    
        // Don't override paint() in a Swing panel!
        //public void paint(Graphics g){
        @Override
        public void paintComponent(Graphics g) {
            // USE the ImageObserver!
            //g.drawImage(backgroundImage,0,0,null);
            g.drawImage(backgroundImage,0,0,getWidth(),getHeight(),this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.