Пример #1
0
 /** Clears fixed pixels width caches. */
 protected void updateCachedWidthData() {
   cachedWidth0 = null;
   cachedWidth1 = null;
   getFixedPixelsWidth(true);
   getFixedPixelsWidth(false);
 }
Пример #2
0
  /**
   * Constructs new NinePatchIcon using the specified nine-patch image.
   *
   * @param bufferedImage nine-patch image
   * @param component component atop of which icon will be stretched
   * @param parsePatches whether should parse image patches or not
   */
  protected NinePatchIcon(
      final BufferedImage bufferedImage, final Component component, final boolean parsePatches) {
    super();

    if (parsePatches) {
      // Incorrect image
      if (bufferedImage.getWidth() < 3 || bufferedImage.getHeight() < 3) {
        throw new IllegalArgumentException("Buffered image must be atleast 3x3 pixels size");
      }

      // Componet for which this icon will be streched
      this.component = component;

      // Creating actual image in a compatible format
      final int w = bufferedImage.getWidth() - 2;
      final int h = bufferedImage.getHeight() - 2;
      rawImage = ImageUtils.createCompatibleImage(bufferedImage, w, h);
      final Graphics2D g2d = rawImage.createGraphics();
      g2d.drawImage(
          bufferedImage,
          0,
          0,
          w,
          h,
          1,
          1,
          bufferedImage.getWidth() - 1,
          bufferedImage.getHeight() - 1,
          null);
      g2d.dispose();

      // Parsing stretch variables
      horizontalStretch =
          NinePatchUtils.parseIntervals(bufferedImage, NinePatchIntervalType.horizontalStretch);
      verticalStretch =
          NinePatchUtils.parseIntervals(bufferedImage, NinePatchIntervalType.verticalStretch);

      // Incorrect image
      if (!((horizontalStretch.size() > 1
              || horizontalStretch.size() == 1 && !horizontalStretch.get(0).isPixel())
          && (verticalStretch.size() > 1
              || verticalStretch.size() == 1 && !verticalStretch.get(0).isPixel()))) {
        throw new IllegalArgumentException("There must be stretch constraints specified on image");
      }

      // Parsing content margins
      final List<NinePatchInterval> vc =
          NinePatchUtils.parseIntervals(bufferedImage, NinePatchIntervalType.verticalContent);
      final List<NinePatchInterval> hc =
          NinePatchUtils.parseIntervals(bufferedImage, NinePatchIntervalType.horizontalContent);
      final int top = vc.size() == 0 ? 0 : vc.get(0).getStart();
      final int bottom = vc.size() == 0 ? 0 : rawImage.getHeight() - vc.get(0).getEnd() - 1;
      final int left = hc.size() == 0 ? 0 : hc.get(0).getStart();
      final int right = hc.size() == 0 ? 0 : rawImage.getWidth() - hc.get(0).getEnd() - 1;
      margin = new Insets(top, left, bottom, right);

      // Forcing cached data calculation on initialization
      getFixedPixelsWidth(true);
      getFixedPixelsWidth(false);
      getFixedPixelsHeight(true);
      getFixedPixelsHeight(false);
    } else {
      // Componet for which this icon will be streched
      this.component = component;

      // Actual image
      this.rawImage = bufferedImage;

      // Stretch variables
      horizontalStretch = new ArrayList<NinePatchInterval>();
      verticalStretch = new ArrayList<NinePatchInterval>();
    }
  }