StretchIcon class doesn't work when returned from a method. Causes error

不打扰是莪最后的温柔 提交于 2020-01-16 10:31:36

问题


A direct call - new StretchIcon(imageStr); - works.

But not when returned from a method. Causes error.

public static ImageIcon getPhotoSI(String imageStr){
    return new StretchIcon(imageStr);
}

What could be the problem?

StretchIcon Link

Error

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: 
  Uncompilable source code - Erroneous ctor sym type: library.StretchIcon.<init>
    at library.IImage.getPhotoSI(IImage.java:36)
    at Users.populateTable(Users.java:448)
    at FrmUsers.btnSearch_ActionPerformed(FrmUsers.java:455)
    at FrmUsers.access$600(FrmUsers.java:18)

回答1:


I'm new to Java. Seems like the problem is not including its package name to the helper class (StretchIcon.java).

Like

package library;

to StretchIcon.java. Running the app without it causes runtime error and failing of netbeans to include some relevant elements to the compiled source.

Like

   package library; // <- this one. 

import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.swing.ImageIcon;

    public class StretchIcon extends ImageIcon {

      /**
       * Determines whether the aspect ratio of the image is maintained.
       * Set to <code>false</code> to allow th image to distort to fill the component.
       */
      protected boolean proportionate = true;

      /**
       * Creates a <CODE>StretchIcon</CODE> from an array of bytes.
       *
       * @param  imageData an array of pixels in an image format supported by
       *             the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
       *
       * @see ImageIcon#ImageIcon(byte[])
       */
      public StretchIcon(byte[] imageData) {
        super(imageData);
      }

      /**
       * Creates a <CODE>StretchIcon</CODE> from an array of bytes with the specified behavior.
       *
       * @param  imageData an array of pixels in an image format supported by
       *             the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
       * @param proportionate <code>true</code> to retain the image's aspect ratio,
       *        <code>false</code> to allow distortion of the image to fill the
       *        component.
       *
       * @see ImageIcon#ImageIcon(byte[])
       */
      public StretchIcon(byte[] imageData, boolean proportionate) {
        super(imageData);
        this.proportionate = proportionate;
      }

    }


来源:https://stackoverflow.com/questions/48278718/stretchicon-class-doesnt-work-when-returned-from-a-method-causes-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!