I am attempting to load an 3 icon image but my code doesn't do so:

package com.proxy;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class ImageIconProxy extends ImageIcon implements Runnable{

      static final ImageIcon ONE = new ImageIcon("/IFDesign/images/green.jpg");
      static final ImageIcon TWO = new ImageIcon("/IFDesign/images/yellow.jpg");
      ImageIcon CURRENT = ONE;
      protected String fileName;
      protected JFrame callBackFrame;

      public ImageIconProxy(String fileName){
          super(ONE.getImage());
          this.fileName = fileName;
      }

      public void load(JFrame callBackFrame){
          this.callBackFrame = callBackFrame;
          CURRENT = TWO;
          callBackFrame.repaint();
          new Thread(this).start();
      }

      public void run(){
          CURRENT = new ImageIcon(fileName);
          callBackFrame.pack();
      }

      public int getIconWidth() {
          return CURRENT.getIconWidth();
      }

      public int getIconHeight(){
          return CURRENT.getIconHeight();
      }

      public synchronized void paintIcon(Component c, Graphics g, int x, int y){
          CURRENT.paintIcon(c, g, x, y);
      }
}


package com.proxy;

import java.awt.Graphics;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class JF extends JFrame {

      protected static JF j;
      protected static ImageIconProxy ip;

      public JF(){
          this.setTitle("TEST");
          this.setSize(400,400);
      }

      public void paint(Graphics g){
          super.paint(g);
          ip.paintIcon(this, g, 100, 100);
      }

      public static void main(String[] args){
          j = new JF();
          ip = new ImageIconProxy("/IFDesign/images/blue.jpg");
          ip.load(j);
          j.setVisible(true);
      }
}

Did I miss something? Your help is kindly appreciated.

Thank You.

Recommended Answers

All 4 Replies

is there an errormessage? have you added a check which verifies the image is found on the url you provide?

hai solomon_13000,

this is just suggestion only :

i think that might be problem with your class couldn't find the path what you specified for ImageIcon class Objects

so try to change the creation of ImageIcon class Object like as follows at line no 11 and 12 in your code

ImageIcon ONE = new ImageIcon(getClass().getResource("/IFDesign/images/green.jpg"));

check it once and also do same for the remaining image icons creation code

let me know the status

happy coding

new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it's just empty. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
Use the exists() method of the File class with the path/name of the file to confirm that your program can find the file. Alternatively, check the width/height of the image to confirm they are >0

As suggested I changed the location path of the images:

package com.proxy;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class ImageIconProxy extends ImageIcon implements Runnable{

      static final ImageIcon ONE = new ImageIcon("images/green.jpg");
      static final ImageIcon TWO = new ImageIcon("images/yellow.jpg");
      ImageIcon CURRENT = ONE;
      protected String fileName;
      protected JFrame callBackFrame;

      public ImageIconProxy(String fileName){
          super(ONE.getImage());
          this.fileName = fileName;
      }

      public void load(JFrame callBackFrame){
          this.callBackFrame = callBackFrame;
          CURRENT = TWO;
          callBackFrame.repaint();
          new Thread(this).start();
      }

      public void run(){
          CURRENT = new ImageIcon(fileName);
          callBackFrame.pack();
      }

      public int getIconWidth() {
          return CURRENT.getIconWidth();
      }

      public int getIconHeight(){
          return CURRENT.getIconHeight();
      }

      public synchronized void paintIcon(Component c, Graphics g, int x, int y){
          CURRENT.paintIcon(c, g, x, y);
      }
}



package com.proxy;

import java.awt.Graphics;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class JF extends JFrame {

      protected static JF j;
      protected static ImageIconProxy ip;

      public JF(){
          this.setTitle("TEST");
          this.setSize(400,400);
      }

      public void paint(Graphics g){
          super.paint(g);
          ip.paintIcon(this, g, 50, 50);
      }

      public static void main(String[] args){
          j = new JF();
          ip = new ImageIconProxy("images/blue.jpg");
          ip.load(j);
          j.setVisible(true);
      }
}

It works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.