Boa Tarde Galera,
Estou com uma dúvida, creio que simples, e gostaria da ajuda de vocês,
Por exemplo, nesta imagem abaixo:

É a abertura do Visio 2010, para quem não conhece, um programa para criar UML, fluxoramas, etc…
Bom, a dúvida é o seguinte, como eu faria para criar um programa, com imagens, como a em degrade verde e amarelo do visio?
Para ser mais detalhado, como eu faria para colocar imagens de fundo, como se fosse um background numa tela do swing?
Desculpem a formulação da pergunta =D
Vlw galera.
Bom meu camarada!
Se sua dúvida é apenas adicionar uma imagem a um Label, basta utilizar a propriedade icon.
Exemplo:
jLabel.setIcon(new javax.swing.ImageIcon("C:\\imagem.png"));
Não é bem isso,
Por exemplo, imagine que tenho um JPanel e quero colocar uma imagem de fundo, algo assim.
Cara, se o problema é o jpanel, vc pode adicionar o label:
[code]ImageIcon img = new ImageIcon(“imagem.jpg”);
//aqui vc cria um label e joga a imagem nele
JLabel label = new JLabel(img);
//label adicionado ao panel
JPanel panel = new JPanel();
panel.add(label, BorderLayout.NORTH); [/code]
Então eu já tinha tentado isso, mas o que acontece,
Adiciono um JPanel sobre o JFrame,
adiciono um ImageIcon a um JLabel,
adiciono este JLabel ao JPanel,
Porém o problema é o seguinte, preciso adicioar texto e um JButton sobre este JPanel que tem a imagem.
Só que quando adiciono, este texto e este JButton ficam ao lado do JPanel entende.
O que quero é criar uma imagem de fundo para o programa e adicionar elementos como JButtons, JLabels, etc.
Procurei alguns tutoriais, mas nãoa chei nada especifíco.
Ola
Eu utilizo a seguinte classe encontrada aqui mesmo no GUJ.
package clickimoveis;;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* A panel that contains a background image. The background image is
* automatically sized to fit in the panel.
*/
public class JImagePanel extends JPanel
{
private BufferedImage image = null;
private FillType fillType = FillType.RESIZE;
/**
* Creates a new panel with the given background image.
*
* @param img The background image.
*/
public JImagePanel(BufferedImage img)
{
setImage(img);
}
/**
* Creates a new panel with the given background image.
*
* @param img The background image.
* @throws IOException, if the image file is not found.
*/
public JImagePanel(File imgSrc) throws IOException
{
this(ImageIO.read(imgSrc));
}
/**
* Creates a new panel with the given background image.
*
* @param img The background image.
* @throws IOException, if the image file is not found.
*/
public JImagePanel(String fileName) throws IOException
{
this(new File(fileName));
}
/**
* Changes the image panel image.
*
* @param img The new image to set.
*/
public final void setImage(BufferedImage img)
{
if (img == null)
throw new NullPointerException("ERROR: Buffered image cannot be null!");
this.image = img;
invalidate();
}
/**
* Changes the image panel image.
*
* @param img The new image to set.
* @throws IOException If the file does not exist or is invalid.
*/
public void setImage(File img) throws IOException
{
setImage(ImageIO.read(img));
}
/**
* Changes the image panel image.
*
* @param img The new image to set.
* @throws IOException If the file does not exist or is invalid.
*/
public void setImage(String fileName) throws IOException
{
setImage(new File(fileName));
}
/**
* Returns the image associated with this image panel.
*
* @return The associated image.
*/
public BufferedImage getImage()
{
return image;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
fillType.drawImage(this, g2d, image);
g2d.dispose();
}
/**
* Returns the way this image fills itself.
*
* @return The fill type.
*/
public FillType getFillType()
{
return fillType;
}
/**
* Changes the fill type.
*
* @param fillType The new fill type
* @throws IllegalArgumentException If the fill type is null.
*/
public void setFillType(FillType fillType)
{
if (fillType == null)
throw new IllegalArgumentException("Invalid fill type!");
this.fillType = fillType;
invalidate();
}
public static enum FillType
{
/**
* Make the image size equal to the panel size, by resizing it.
*/
RESIZE
{
@Override
public void drawImage(JPanel panel, Graphics2D g2d,
BufferedImage image)
{
g2d.drawImage(image, 0, 0, panel.getWidth(), panel.getHeight(),
null);
}
},
/**
* Centers the image on the panel.
*/
CENTER
{
@Override
public void drawImage(JPanel panel, Graphics2D g2d,
BufferedImage image)
{
int left = (panel.getWidth() - image.getWidth()) / 2;
int top = (panel.getHeight() - image.getHeight()) / 2;
g2d.drawImage(image, left, top, null);
}
},
/**
* Makes several copies of the image in the panel, putting them side by
* side.
*/
SIDE_BY_SIDE
{
@Override
public void drawImage(JPanel panel, Graphics2D g2d,
BufferedImage image)
{
Paint p = new TexturePaint(image, new Rectangle2D.Float(0, 0,
image.getWidth(), image.getHeight()));
g2d.setPaint(p);
g2d.fillRect(0, 0, panel.getWidth(), panel.getHeight());
}
};
public abstract void drawImage(JPanel panel, Graphics2D g2d,
BufferedImage image);
}
}
Flw