Am trying to draw image on jpanel or jlabel when button is been click on, so i added actionperformed on my button as follows.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Image imag = new ImageIcon(this.getClass().getResource("img/top_bg.jpg")).getImage();
showPix1.setLayout(new BorderLayout());
showPix1.add(new paintPhotos(imag,20,20), BorderLayout.CENTER);
}
I have a separate class called paintPhotos that extends jLabel as follows
package myApp;
import java.awt.*;
import javax.swing.*;
public class paintPhotos extends javax.swing.JLabel {
public Image img; int w; int h;
public paintPhotos(Image img, int w, int h) {
this.img = img; this.w = w; this.h = h;
System.out.println("am paintclass");
}
@Override
public void paintComponent(Graphics p) {
System.out.println("am here");
super.paintComponent(p);
Graphics2D g2 = (Graphics2D) p;
p.drawImage(img, 0, 0, w, h, this);
}
}
Now when i click on the button it never draw the image, what am i doing wrong? and how can i draw the image on jLabel from my actionperformed event? pls any help will be highly appreciated. Thanks in advance