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 5996411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:05:58+00:00 2026-05-23T00:05:58+00:00

I am new to Applets, and am trying to get comfortable with some basics

  • 0

I am new to Applets, and am trying to get comfortable with some basics – like how to display a jpg image in one. I’ve read what I think are the relevant parts of the Java Tutorials on Applets, and I don’t understand why this doesn’t work, all I get is a blank applet area. “/cards/as.jpg” – that file exists in subdirectory of the directory the applet is running from. Can anyone see what I’m doing wrong? I’m at a loss.

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        }
        catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }

    private void createGUI() {
        JPanel newContentPane = new JPanel();

        ImageComponent card = new ImageComponent();
        card.setImage(getCodeBase() + "/cards/as.jpg");

        newContentPane.add(card);        

        newContentPane.setOpaque(true);
        setContentPane(newContentPane);
    }
}


import java.awt.*;
import javax.swing.JComponent;
import javax.imageio.*;
import java.net.*;
import java.io.*;

/**
   A component that draws an Image.
*/
public class ImageComponent extends JComponent {  
    private Image image;
    private String url;

    public ImageComponent() {
        image = null;
        url = null;
    }

    public void setImage(String urlCardName) {
        url = urlCardName;
        if (url != null) {
            try {
                image = ImageIO.read(new URL(url));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        else {
            image = null;
        }
        this.repaint();
    }

    public void paintComponent(Graphics g) { 
        if (image == null) return;
        // draw the images in the upper-left corner of the component
        g.drawImage(image, 0, 0, null);
    }
}
  • 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-23T00:05:58+00:00Added an answer on May 23, 2026 at 12:05 am

    I think it’s your layouts. Normally a contentPane uses BorderLayout by default, but when you use your own JPanel as contentPane, you’re using the JPanel default layout which is FlowLayout, and components added to a container using FlowLayout won’t expand to fill the container as they would with BorderLayout. Check to see how large your ImageComponent is when rendered — I’ll bet you it’s very small, 0 size even.

    A solution is to use the contentPane that’s already present in the applet with its default BorderLayout, or else if you must use your own JPanel, give it a BorderLayout and add the new ImageComponent BorderLayout.CENTER. For example:

        JPanel newContentPane = new JPanel();
    
        newContentPane.setLayout(new BorderLayout()); // *** added this
    
        ImageComponent card = new ImageComponent();
        card.setImage(getCodeBase() + "/cards/as.jpg");
    
        newContentPane.add(card, BorderLayout.CENTER);  // *** changed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've come across some strange behavior trying to get files that start with a
I have a java applet trying to pass some GET data to a php
I'm trying to get a Java Applet to display on a webpage. The applet
New to rails/ruby (using rails 3 and ruby 1.9.2), and am trying to get
New Question I am looking for a way in Javascript to get the parent
new_story GET /story/new(.:format) {:action=>new, :controller=>stories} edit_story GET /story/edit(.:format) {:action=>edit, :controller=>stories} story GET /story(.:format) {:action=>show,
New to Subsonic 3.0 and wondering how to perform LIKE operators on object attributes.
New to all this so forgive my ignorance. I am trying to figure out
new to JBoss and am configuring some applications. I know how to do this
I'm trying to get the mouse move event from external applet that has been

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.