Add Background to a Girdbaglayout

Asked

Viewed 92 times

0

Good afternoon guys, I’m struggling here trying to add a background image inside a Gridbaglayout but I can’t. Some charitable soul could help me?

Follows Code >

package View;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TESTE extends JFrame implements ActionListener {

    /**
    * 
    */
    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_WIDTH = 353;
    public static final int DEFAULT_HEIGHT = 200;

    private JButton jBExtrair;
    private JButton jBSair;

    public TESTE() throws IOException {

    super("Extract - Files");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    this.getContentPane().setLayout(new GridBagLayout());

    jBExtrair = new JButton("Extrair");
    jBSair = new JButton("Sair");

    add(jBExtrair, jBSair);
    add("/images/Fundo.jpg"); // Caminho da imagem.. packge Images

    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    this.setResizable(false);
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension tamanhoTela = kit.getScreenSize();
    int width = tamanhoTela.width;
    int height = tamanhoTela.height;
    this.setLocation(width / 2, height / 2);
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // pra nao sair
                                                        // caso clique
                                                        // no x e
                                                        // escolha nao

    // Classe para perguntar ao clicar no X ao inves de SAIR
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
        int teste = JOptionPane.showConfirmDialog(null, "Deseja Sair?",                         "Exit", JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE);
                if (teste == 0) {
            System.exit(0);
                }
            }
        });
    }


    /**
    * Recebe dois botoes Extrair e Sair
    * 
    * @param jButton1
    *            Botao recebido por parametro
    * @param jButton2
    *            Botao recebido por parametro    
    */
    public void add(JButton jButton1, JButton jButton2) {

        GridBagConstraints cons = new GridBagConstraints();

        cons.weightx = 300;
        cons.weighty = 300;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);

        jButton1 = new JButton();
        jButton2 = new JButton();
        jButton1 = jBExtrair;
        jButton2 = jBSair;

        cons.gridx = 0;
        cons.gridy = 4;
        cons.gridwidth = 2;
        this.getContentPane().add(jButton1, cons);

        cons.gridx = 3;
        cons.gridy = 5;
        cons.gridwidth = 2;
        this.getContentPane().add(jButton2, cons);

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
    }

    /**
    * Caminho da imagem do plano de fundo
    * 
    * @param caminhoImg
    *            String com diretorio da imagem
    */
    public void add(String caminhoImg) {

        JPanelWithBackground background = null;
        background = new JPanelWithBackground(caminhoImg);
        background.setSize(50, 50);

        GridBagConstraints cons = new GridBagConstraints();

        cons.weightx = 700;
        cons.weighty = 700;
        cons.anchor = GridBagConstraints.CENTER;
        cons.ipadx = 350;
        cons.ipady = 170;
        cons.gridx = 0;
        cons.gridy = 0;
        cons.gridwidth = 5;
        cons.gridheight = 6;
        // cons.fill = GridBagConstraints.BOTH;

        this.getContentPane().add(background, cons);

    }

    @Override
    public void actionPerformed(ActionEvent e) {


    }

}

In fact if I on this line:

add("/images/Fundo.jpg"); // Caminho da imagem.. packge Images

If I glue for example:

add("C:\\Fundo.jpg"); // Caminho da imagem.. Diretorio C:

So it works, when I put the path of a directory, but when I try to pass the path of the package it will not..

I posted all this so there’s no doubt what I’m trying to do.. Thank you in advance

1 answer

0

I got...

Stayed like this...

Pass Pro metodo add the way >>>

    add("/View/Fundo.jpg");

Method in the Gridbaglayout >>>

/**
 * Caminho da imagem do plano de fundo
 * 
 * @param caminhoImg
 *            String com diretorio da imagem
 */
public void add(String caminhoImg) {


    Object image = null;
    try {
        image = ImageIO.read(getClass(). getResource(caminhoImg));
        } catch (IOException e) {
            e.printStackTrace();
            } JPanel j = new JPanelWithBackground((Image) image);


    GridBagConstraints cons = new GridBagConstraints();

    cons.weightx = 700;
    cons.weighty = 700;
    cons.anchor = GridBagConstraints.CENTER;
    cons.ipadx = 350;
    cons.ipady = 170;
    cons.gridx = 0;
    cons.gridy = 0;
    cons.gridwidth = 5;
    cons.gridheight = 6; // cons.fill = GridBagConstraints.BOTH;
    this.getContentPane().add(j, cons);

}

Class that manages Image >>>

package View;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;


public class JPanelWithBackground extends JPanel {
private static final long serialVersionUID = 1L;
Image imageOrg = null;

public JPanelWithBackground(Image image2) {
    imageOrg = image2;
    setOpaque(false);
}

@Override
public Dimension getPreferredSize() {
    return new Dimension(imageOrg.getWidth(this), imageOrg.getHeight(this));
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (imageOrg != null) {
        System.err.println("painting");
        g.drawImage(imageOrg, 0, 0, getWidth(), getHeight(), this);
    }
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.