Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

avoiding flickering

 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers,

how can i put an image on background in a JPanel without overriding its paint() method?.i am having some dragging components over it.
i had overrided paint() method of JPanel to put background image,but inconsistent flickering occurs.how can i resolve the problom?.

anybody plz..
basha
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the code for the JPanel you are having the problems with?

Though, one of my first suggestions would be to override paintComponent() rather than paint()...
 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... there were lots of problems with the code... I *think* the problem that was causing the flickering was the overridden paint method for the JFrame, you should probably make it a JPanel and override the JPanel's paintComponent() method. Also, you were re-loading the background image every time it needed to repaint. This was also probably adding to the flicker problem. Here's a fixed version that I think does what you want it to:



Here is another post that I gave code for how to add a background image to a JDesktopPane...
 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks nate..
u r really helpfull to me.i love people like u who really having some value on most dimensions...
take care ...
basha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic