ayuda, poner una imagen de fondo en un applet
hola, podria alguien decirme como puedo poner una imagen jpg de fondo en un applet, por favor, gracias
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class VentanaImagen extends JFrame implements ActionListener
{
JButton botonArriba = new JButton("+Y");
JButton botonAbajo = new JButton("-Y");
JButton botonIzquierda = new JButton("-X");
JButton botonDerecha = new JButton("+X");
JLabel mono = new JLabel(new ImageIcon("mono.gif"));
final int PASO_X = 20;
final int PASO_Y = 20;
int x = 250, y = 250;
public VentanaImagen()
{
super("Ventana Swing");
iniciarVentana();
}
protected void iniciarVentana()
{
getContentPane().setLayout(new BorderLayout());
addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
JPanel panel = new JPanel()
{
final ImageIcon imageIcon = new ImageIcon("fondo.gif");
Image image = imageIcon.getImage();
{
setOpaque(false);
setLayout(null);
}
public void paintComponent (Graphics g)
{
g.drawImage(image, 0, 0, this);
super.paintComponent(g);
}
};
getContentPane().add( BorderLayout.CENTER, panel );
botonArriba.setBounds(50,50,50,50);
botonArriba.addActionListener( this );
botonAbajo.setBounds(50,150,50,50);
botonAbajo.addActionListener( this );
botonIzquierda.setBounds(25,100,50,50);
botonIzquierda.addActionListener( this );
botonDerecha.setBounds(75,100,50,50);
botonDerecha.addActionListener( this );
mono.setBounds(x, y, 30, 30);
panel.add(botonArriba);
panel.add(botonAbajo);
panel.add(botonIzquierda);
panel.add(botonDerecha);
panel.add(mono);
setSize( 500, 500 );
setVisible( true );
}
public void actionPerformed( ActionEvent e )
{
Object o = e.getSource();
if (o == botonArriba)
y -= PASO_Y;
else
if (o == botonAbajo)
y += PASO_Y;
else
if (o == botonIzquierda)
x -= PASO_X;
else
if (o == botonDerecha)
x += PASO_X;
mono.setLocation(x, y);
}
public static void main ( String[] args )
{
new VentanaImagen();
}
}
import java.awt.event.*;
import javax.swing.*;
class VentanaImagen extends JFrame implements ActionListener
{
JButton botonArriba = new JButton("+Y");
JButton botonAbajo = new JButton("-Y");
JButton botonIzquierda = new JButton("-X");
JButton botonDerecha = new JButton("+X");
JLabel mono = new JLabel(new ImageIcon("mono.gif"));
final int PASO_X = 20;
final int PASO_Y = 20;
int x = 250, y = 250;
public VentanaImagen()
{
super("Ventana Swing");
iniciarVentana();
}
protected void iniciarVentana()
{
getContentPane().setLayout(new BorderLayout());
addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
JPanel panel = new JPanel()
{
final ImageIcon imageIcon = new ImageIcon("fondo.gif");
Image image = imageIcon.getImage();
{
setOpaque(false);
setLayout(null);
}
public void paintComponent (Graphics g)
{
g.drawImage(image, 0, 0, this);
super.paintComponent(g);
}
};
getContentPane().add( BorderLayout.CENTER, panel );
botonArriba.setBounds(50,50,50,50);
botonArriba.addActionListener( this );
botonAbajo.setBounds(50,150,50,50);
botonAbajo.addActionListener( this );
botonIzquierda.setBounds(25,100,50,50);
botonIzquierda.addActionListener( this );
botonDerecha.setBounds(75,100,50,50);
botonDerecha.addActionListener( this );
mono.setBounds(x, y, 30, 30);
panel.add(botonArriba);
panel.add(botonAbajo);
panel.add(botonIzquierda);
panel.add(botonDerecha);
panel.add(mono);
setSize( 500, 500 );
setVisible( true );
}
public void actionPerformed( ActionEvent e )
{
Object o = e.getSource();
if (o == botonArriba)
y -= PASO_Y;
else
if (o == botonAbajo)
y += PASO_Y;
else
if (o == botonIzquierda)
x -= PASO_X;
else
if (o == botonDerecha)
x += PASO_X;
mono.setLocation(x, y);
}
public static void main ( String[] args )
{
new VentanaImagen();
}
}
<DIV STYLE="position:absolute; top:100px; left:100px; width:400px; height:400px; background-image: url(logo2.gif); background-repeat: repeat-y">
Anexalo a ti HTML y listo
Anexalo a ti HTML y listo