Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java drawimage on jlabel

Reply
Thread Tools

Java drawimage on jlabel

 
 
easycode easycode is offline
Junior Member
Join Date: Oct 2012
Posts: 1
 
      10-29-2012
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug in Graphics.drawImage ?! Rene Java 5 08-26-2004 06:49 PM
Double-Buffering Without drawImage? Lash Rambo Java 5 07-14-2004 11:32 PM
drawImage(Image, x, y, ImageObserver) in a server-side app (not Swing/AWT) OtisUsenet Java 3 02-24-2004 02:08 PM
Is graphics2d.drawImage(image, 0, 0, w, h, null) an asynchronous operation? Wolfgang Java 3 02-10-2004 07:24 PM
Your votes please ( drawImage bad interpolation bug id 4950176) Michele Puccini Java 0 11-09-2003 11:06 AM



Advertisments