1

I have a class that extends jlabel and draw on it using paintComponent as follows this is paintPhotos.java

package myApp;
import java.awt.*;
import javax.swing.*;
/**
*
* @author PAGOLINA
*/
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);
}

}

when i try to draw from a constructor of another class like this (AddScore.java).

public AddScore() {
    initComponents();
    setLocationRelativeTo(null);
    removeNotify();
    setUndecorated(true);
    Image imag = new  ImageIcon(this.getClass().getResource("img/top_bg.jpg")).getImage();
    showPix1.setLayout(new BorderLayout());
    showPix1.add(new paintPhotos(imag,40,40), BorderLayout.CENTER);
}

the above work fine and draw the image as specified.

but when i try to draw the image from an actionperform event of another class (AddScore.java) like this.

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);
}

the above statement did not work as the paintcomponent is not working, what am i doing wrong?

Can someone pls help me on this cos i have try all posible method to call my paintPhotos class yet is not working, what is wrong with this code?

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);
}
easyscript
  • 171
  • 3
  • 6
  • 1
    1) *" i have a class that extends jlabel and draw on it using paintComponent"* Why? Draw on a `BufferedImage` and display it in a label, as shown [here](http://stackoverflow.com/questions/12208574/using-addmouselistener-and-paintcomponent-for-jpanel/12216139#12216139). 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 29 '12 at 08:56
  • Please learn java naming conventions and stick to them. – kleopatra Oct 29 '12 at 09:02
  • 2
    You already added the components to your GUI. What you need at the `actionPerformed()` method is simply to update the image of `PaintPhotos` class and call `repaint()` – nIcE cOw Oct 29 '12 at 09:05

1 Answers1

5
  1. put Image as Icon / ImageIcon to JLabel

  2. in this case don't load images on fly, load that as local variable

  3. for Icon/ImageIcon in JLabel to use proper LayoutManager

  4. common issue is Icon/ImageIcon doesn't returns any exceptions, have to test for null value

  5. for better help sooner post an SSCCE

mKorbel
  • 109,525
  • 20
  • 134
  • 319