Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image refreshing on JButton problem  RSS feed

 
Sham David
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problem with displaying an image on a JButton. We can display two image shapes in the JButton, viz. a rectangle image (which is the JButton shape too) or a oval/circular (which means the image isn't going to cover whole JButton area)and some background is still left. *********
The problem occurs while displaying a oval/circular image. The part/corners of the JButton which isn't covered by the image (oval) gets changed everytime the mouse is moved from other area of the applet to the JButton.

There is a Repainting problem. Any help would be much appreciated.
I have an Applet with calls the ImageButton Class
--------------------------------------------------
public void actionPerformed(ActionEvent pEvent)
{
String command = pEvent.getActionCommand();
if(pEvent.getSource() == imageButton)
{
imageButton.actionPerformed(pEvent);
}
else if(pEvent.getSource() == Image2Button)
{
Image2Button.actionPerformed(pEvent);
}
}
--------------------------------------------------
The following class prompts a filechooser, which allows a person to get the image and this image is resized
into a JButton.
public class ImageButton extends JButton implements ActionListener
{
//private
private Image image;
public ImageButton() {}
public void paint(Graphics g)
{
if (image != null)
{
Rectangle rect = this.getBounds();
g.drawImage(image, 0, 0, rect.width, rect.height, this);
}
else
{
paintComponent(g);
paintBorder(g);
paintChildren(g);
}
}
public Rectangle getBounds()
{
return super.getBounds();
}
public void actionPerformed(ActionEvent pEvent)
{
FileChooser myFileChooser = new FileChooser();
try
{
ImageIcon myIcon = new ImageIcon(myFileChooser.getFilePath());
System.out.println("myIcon " + myIcon);
image = myIcon.getImage();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!