Pessoal,
Criei um algoritmo pra exibir uma imagem de background em um JPanel… e que ao instante que é redimensionado, a imagem se ajusta automaticamente (deveria ao menos) só que está um tanto quanto estranho, a imagem as vezes se redimenciona, outras vezes não…
Alguém sabe o que devo fazer pra deixar isso melhor?
package components;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* ImagePanel
*
* @return class
* @ignored extends
* @ignored JPanel
*/
public class ImagePanel extends JPanel
{
private Image original;
private Image img;
private JLabel text = new JLabel();
/**
* ImagePanel
*
* @param img String
*/
public ImagePanel( String img )
{
this( new ImageIcon( img ).getImage() );
}
public static void main( String[] args )
{
JFrame f = new JFrame();
String imgTest = "res/Backgroundbutton.jpg";
ImagePanel p1 = new ImagePanel( imgTest );
ImagePanel p2 = new ImagePanel( imgTest );
ImagePanel p3 = new ImagePanel( imgTest );
ImagePanel p4 = new ImagePanel( imgTest );
ImagePanel p5 = new ImagePanel( imgTest );
p1.setTextx( "Teste1");
p2.setTextx( "Teste1");
p3.setTextx( "Teste1");
p4.setTextx( "Teste1");
p5.setTextx( "Teste1");
f.setLayout( new GridBagLayout() );
f.add( p1, new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
f.add( p2, new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
f.add( p3, new GridBagConstraints( 2, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
f.add( p4, new GridBagConstraints( 3, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
f.add( p5, new GridBagConstraints( 4, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
f.setSize( 600,200 );
f.setVisible( true );
}
/**
* setText
*
* @param t String
*/
public void setTextx( String t )
{
text.setText( t );
}
/**
* ImagePanel
*
* @param img Image
*/
public ImagePanel( Image i )
{
this.setLayout( new GridBagLayout() );
this.img = i;
this.original = i;
add( text, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
addComponentListener( new ComponentAdapter() {
@Override
public void componentResized( ComponentEvent e )
{
recalcule();
}
});
}
/**
* recalcule
*
*/
public void recalcule()
{
Thread t = new Thread(
new Runnable() {
public void run()
{
img = original.getScaledInstance( getWidth(), getHeight(), Image.SCALE_FAST);
repaint();
}
});
t.start();
}
@Override
public void paintComponent( Graphics g )
{
g.drawImage( img, 0, 0, null );
}
}
Grato