Note that there are some explanatory texts on larger screens.

plurals
  1. POjava drawImage not working from event
    primarykey
    data
    text
    <p>I have a class that extends jlabel and draw on it using paintComponent as follows this is paintPhotos.java</p> <pre><code>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); } } </code></pre> <p>when i try to draw from a constructor of another class like this (AddScore.java).</p> <pre><code>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); } </code></pre> <p>the above work fine and draw the image as specified.</p> <p>but when i try to draw the image from an actionperform event of another class (AddScore.java) like this.</p> <pre><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); } </code></pre> <p>the above statement did not work as the paintcomponent is not working, what am i doing wrong?</p> <p>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?</p> <pre><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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload