Java drawimage on jlabel

Discussion in 'Java' started by easycode, Oct 29, 2012.

  1. easycode

    easycode

    Joined:
    Oct 24, 2012
    Messages:
    1
    Please 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?
    easycode, Oct 29, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Puccini
    Replies:
    0
    Views:
    397
    Michele Puccini
    Nov 9, 2003
  2. Wolfgang
    Replies:
    3
    Views:
    6,090
    Knute Johnson
    Feb 10, 2004
  3. OtisUsenet
    Replies:
    3
    Views:
    12,530
    OtisUsenet
    Feb 24, 2004
  4. Lash Rambo
    Replies:
    5
    Views:
    648
    Lash Rambo
    Jul 15, 2004
  5. Rene
    Replies:
    5
    Views:
    908
    Larry Barowski
    Aug 26, 2004
Loading...

Share This Page