hi nate nice to see u.
i need a background image in JFrame.i need some labels dragging on that image(above the image).
i had painted an image in JFrame
i had added jLabels on ContentPane of that frame
how can i paint my image on contentpane?.i think that 'll solve the problom.
here is a sample code..
save 2 gif images on current directory and rename as 'backgroundimage.gif' and 'labelimage.gif'. then run the code
import javax.swing.*;
import java.awt.*;
public class Sample1 extends javax.swing.JFrame
{
public Sample1()
{
initComponents ();
draw();
setSize(400,300);
}
private void initComponents()
{//GEN-BEGIN:initComponents
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt)
{
exitForm(evt);
}
}
);
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt)
{//GEN-FIRST:event_exitForm
System.exit (0);
}//GEN-LAST:event_exitForm
public void draw()
{
for(int i=0;i<10;i++)
{
alabel=new ALabel();
this.getContentPane().add(alabel,0);
alabel.setBounds(20,20+(i<<2),140,200);
alabel.setVisible(true);
}
this.alabel.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(java.awt.event.MouseEvent me)
{
}
public void mouseReleased(java.awt.event.MouseEvent me)
{
((javax.swing.JLabel)me.getSource()).setBounds(me.getX(),me.getY(),140,200);
}
});
this.alabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
public void mouseDragged(java.awt.event.MouseEvent me)
{
Component c = (Component)me.getSource();
Point p = me.getPoint();
p = SwingUtilities.convertPoint( c, p, Sample1.this );
c.setLocation( p );
c.setBounds((int)p.getX(),(int)p.getY(),140,200);
c.setVisible(true);
}
});
}
public void paint(java.awt.Graphics g)
{
g.drawImage((new javax.swing.ImageIcon("backgroundimage.gif")).getImage(),10,10,this);
//g.drawImage(Toolkit.getDefaultToolkit().createImage("backgroundimage.gif"),10,10,this);
//not getting the image
}
/**
* @param args the command line arguments
*/
public static void main (
String args[])
{
new Sample1 ().show ();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel alabel;
// End of variables declaration//GEN-END:variables
}
// label
class ALabel extends javax.swing.JLabel {
public ALabel()
{
this.setIcon(new javax.swing.ImageIcon("labelimage.gif"));
}
}
i need bacground images on Desktoppane also.how can i achieve that using aggregation instead of subclassing and overriding Paint() of jDesktopPane?.
lovingly
basha