Hey, this isn't an, "I have an error" post!
It's more like I'm taking a basic java course in High School and want to include an image as a background in a project in swing. I found some code online that seems to do this quite well, but I need to build my project into it, using the design tab. For the design tab to work, we have to separate classes, the XApp.java class and the XFrame.java class. The design tab only works with the XFrame.java class. We use it to auto generate the code by doing it visually. So I need to separate the code into the Frame and App classes. I tried for a couple hours shuffling code with a friend, but I can't seem to get it to work. Any assistance is appreciated.

package swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Multiply {
    public Multiply() {
        try {
        } catch (Exception ex) {

    // Set up contraints so that the user supplied component and the
    // background image label overlap and resize identically
    private static final GridBagConstraints gbc;
    static {
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;

     * Wraps a Swing JComponent in a background image. Simply invokes the overloded
     * variant with Top/Leading alignment for background image.
     * @param component - to wrap in the a background image
     * @param backgroundIcon - the background image (Icon)
     * @return the wrapping JPanel
    public static JPanel wrapInBackgroundImage(JComponent component,Icon backgroundIcon) {
        return wrapInBackgroundImage(

     * Wraps a Swing JComponent in a background image. The vertical and horizontal
     * alignment of background image can be specified using the alignment
     * contants from JLabel.
     * @param component - to wrap in the a background image
     * @param backgroundIcon - the background image (Icon)
     * @param verticalAlignment - vertical alignment. See contants in JLabel.
     * @param horizontalAlignment - horizontal alignment. See contants in JLabel.
     * @return the wrapping JPanel
    public static JPanel wrapInBackgroundImage(JComponent component,
                                               Icon backgroundIcon,
                                               int verticalAlignment,
                                               int horizontalAlignment) {

        // make the passed in swing component transparent

        // create wrapper JPanel
        JPanel backgroundPanel = new JPanel(new GridBagLayout());

        // add the passed in swing component first to ensure that it is in front
        backgroundPanel.add(component, gbc);

        // create a label to paint the background image
        JLabel backgroundImage = new JLabel(backgroundIcon);

        // set minimum and preferred sizes so that the size of the image
        // does not affect the layout size
        backgroundImage.setPreferredSize(new Dimension(1, 1));
        backgroundImage.setMinimumSize(new Dimension(1, 1));

        // align the image as specified.

        // add the background label
        backgroundPanel.add(backgroundImage, gbc);

        // return the wrapper
        return backgroundPanel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Background Image Panel Example");

        // Create some GUI
        JPanel foregroundPanel = new JPanel(new BorderLayout(768, 1024));
                BorderFactory.createEmptyBorder(768, 1024, 0, 0));

    //    foregroundPanel.add(new JLabel("Comment:"), BorderLayout.NORTH);
  //      foregroundPanel.add(new JScrollPane(new JTextArea(10, 30)),
 //                           BorderLayout.CENTER);

                new ImageIcon(Multiply.class.getResource("bg2.jpg"))));


    private void jbInit() throws Exception {

Oh and by the way, it's not like I'm going to claim this part of the code as my own, so don't be concerned.
Code commented out was the beginnings of an interface example by the original author, unnecessary for my project.
Thanks in advance,

9 Years
Discussion Span
Last Post by rpk5000

I'm going to try a different method/ ask my teacher tom if the other way doesn't work.
Thanks to anyone who looked.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.