Como deixar uma imagem de background em um JPanel? e também colocar outras imagens como botões nesse fundo?
Tenta com este código aqui
import javax.swing.*;
import java.awt.*;
class Testing extends JFrame{
public Testing() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
setLocation(200,100);
JPanel panel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
/* Color amarelo = new Color(255,255,210);
Color azul = new Color(225,245,255);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradiente = new GradientPaint (0, 0, azul, 600, 600, amarelo);
g2d.setPaint(gradiente);
g2d.fillRect(0,0, 846, 491);*/
Image imagem = new ImageIcon("SUA IMAGEM").getImage();
g.drawImage(imagem, 0, 0, this);}};
getContentPane().add(panel);
}
public static void main(String[] args){
new Testing().setVisible(true);
}
}
flw :!:
O problema é que eu gero essa interface pelo NetBeans, e ele gera esse código nojento, preciso colocar essa imagem de background do 2º JPanel , será que é possível pela palheta dele? ou que trecho do código eu colacaria ao seu exemplo?
/*
* Principal.java
*
* Created on 31 de Maio de 2006, 13:06
*/
package javaapplication4;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author fabio.pedrosa
*/
public class Principal extends javax.swing.JFrame {
/** Creates new form Principal */
public Principal() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(221, 231, 255));
jPanel1.setBackground(new java.awt.Color(153, 205, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jLabel6.setText("jLabel6");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(jLabel2)
.add(jLabel6))
.add(14, 14, 14)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jTextField3)
.add(jTextField1)
.add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
.addContainerGap(62, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel6)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(24, Short.MAX_VALUE))
);
jPanel2.setBackground(new java.awt.Color(212, 210, 223));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 275, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 11, Short.MAX_VALUE)
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 275, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 1, Short.MAX_VALUE)
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(15, 15, 15)
.add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration//GEN-END:variables
}
Pq vc não cria um JPanelBackGroundImage e adiciona ele como componente da sua paleta?
Já tentou?
Um abraço.
Não tenho idéia de como fazer isso.
Cria uma nova Classe : MeuPainel por exemplo…
Coloca o código abaixo no novo contrutor que vc vai criar
public MeuPainel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image imagem = new ImageIcon("SUA IMAGEM").getImage();
g.drawImage(imagem, 0, 0, this)
}
Depois vc clica com o direito do mouse , vai aparecer várias opções, porcura por algo parecido com addPallet…o foda que estou no trampo e a máquina aqui é uma carroça…vou fazer um passo a passo lá de casa e coloco aqui hj pode ser? :roll:
T+
package testes;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
public class imageTeste extends JComponent {
JFrame frame;
JPanel panel;
imageTeste() {
frame = new JFrame("Game");
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.WHITE);
// nao funcionou...
panel.add(this);
this.setBounds(50, 50, 100, 100);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
try {
Graphics2D g2 = (Graphics2D) g;
InputStream imgStream = getClass().getResourceAsStream("eu.jpg");
BufferedImage image = ImageIO.read(imgStream);
g2.drawImage(image, 0,0, panel);
System.out.println("Mostrando imagem");
}
catch(Exception e) {
System.out.println("Imagem não localizada");
}
}
public static void main(String[] args) {
new imageTeste();
}
}
Este codigo aqui ta funcionando no jcreator, o q foi postado por [b] dsfextreme[b] aqui na minha maquina nao funcionou no jcreator…