예제 #1
0
 public Object initTest(TestEnvironment env, Result result) {
   Context ctx = new Context();
   ctx.bimg = ((BufImg) env.getModifier(bufimgsrcroot)).getImage();
   if (env.isEnabled(doRenderTo)) {
     Graphics2D g2d = ctx.bimg.createGraphics();
     g2d.setColor(Color.white);
     g2d.fillRect(3, 0, 1, 1);
     g2d.dispose();
   }
   if (env.isEnabled(doRenderFrom)) {
     GraphicsConfiguration cfg =
         GraphicsEnvironment.getLocalGraphicsEnvironment()
             .getDefaultScreenDevice()
             .getDefaultConfiguration();
     VolatileImage vimg = cfg.createCompatibleVolatileImage(8, 1);
     vimg.validate(cfg);
     Graphics2D g2d = vimg.createGraphics();
     for (int i = 0; i < 100; i++) {
       g2d.drawImage(ctx.bimg, 0, 0, null);
     }
     g2d.dispose();
     vimg.flush();
   }
   result.setUnits(1);
   result.setUnitName(getUnitName());
   return ctx;
 }
  private void createImages() {
    int w = 16;
    int h = getPreferredSize().height;
    setTarget(new Rectangle(2, 0, 20, 18));
    Graphics2D g2;

    open = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g2 = open.createGraphics();
    g2.setPaint(getBackground());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.fillRect(0, 0, w, h);
    int[] x = {2, w / 2, 14};
    int[] y = {4, 13, 4};
    Polygon p = new Polygon(x, y, 3);
    g2.setPaint(new Color(204, 204, 204));
    g2.fill(p);
    g2.draw(p);
    g2.dispose();

    closed = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    g2 = closed.createGraphics();
    g2.setPaint(getBackground());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.fillRect(0, 0, w, h);
    x = new int[] {3, 13, 3};
    y = new int[] {4, h / 2, 16};
    p = new Polygon(x, y, 3);
    g2.setPaint(new Color(102, 102, 102));
    g2.fill(p);
    g2.draw(p);
    g2.dispose();
  }
예제 #3
0
 /**
  * Loads an image from a given URL into a BufferedImage. The image is returned in the format
  * defined by the imageType parameter. Note that this is special cased for JPEG images where
  * loading is performed outside the standard media tracker, for efficiency reasons.
  *
  * @param url URL where the image file is located.
  * @param imageType one of the image type defined in the BufferedImage class.
  * @return loaded image at path or url
  * @see java.awt.image.BufferedImage
  */
 public static synchronized BufferedImage loadBufferedImage(URL url, int imageType) {
   BufferedImage image = null;
   // Special handling for JPEG images to avoid extra processing if possible.
   if (url == null || !url.toString().toLowerCase().endsWith(".jpg")) {
     Image tmpImage = loadImage(url);
     if (tmpImage != null) {
       image = new BufferedImage(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType);
       Graphics2D g = image.createGraphics();
       g.drawImage(tmpImage, 0, 0, null);
       g.dispose();
     }
   } else {
     BufferedImage tmpImage = loadBufferedJPEGImage(url);
     if (tmpImage != null) {
       if (tmpImage.getType() != imageType) {
         log.config("Incompatible JPEG image type: creating new buffer image");
         image = new BufferedImage(tmpImage.getWidth(null), tmpImage.getHeight(null), imageType);
         Graphics2D g = image.createGraphics();
         g.drawImage(tmpImage, 0, 0, null);
         g.dispose();
       } else image = tmpImage;
     }
   }
   return image;
 } //  loadBufferedImage
예제 #4
0
  /**
   * Draw some graphics into an Graphics2D object.
   *
   * @param image the image to draw into
   * @throws NoninvertibleTransformException in transform errors.
   */
  private void draw(Graphics2D gr) throws NoninvertibleTransformException {
    gr.setPaint(Color.WHITE);
    gr.fill(new Rectangle(0, 0, tileWidth, tileHeight));

    // AffineTransform[[0.318755336305853, 0.0, 420.03106689453125],
    //                 [0.0, 0.318755336305853, 245.5029296875]]
    AffineTransform transform =
        new AffineTransform(
            0.318755336305853, 0.0, 0.0, 0.318755336305853, 420.03106689453125, 245.5029296875);
    gr.setTransform(transform);

    Shape s = new Rectangle(0, 0, 96, 83);

    // create an enbedded graphics
    Graphics2D grr = (Graphics2D) gr.create();
    // AffineTransform[[1.0, 0.0, -343.9285583496093],
    //                 [0.0, 1.0, -502.5158386230469]]
    grr.clip(s.getBounds());
    transform = new AffineTransform(1.0, 0.0, 0.0, 1.0, -343.9285583496093, -502.5158386230469);
    grr.transform(transform);

    AffineTransform t = transform.createInverse();
    s = t.createTransformedShape(s);

    assertTrue(s.getBounds().intersects(grr.getClip().getBounds2D()));

    grr.setPaint(Color.BLUE);
    grr.draw(s);

    grr.dispose();
    gr.dispose();
  }
예제 #5
0
    private void paintToImage(
        final BufferedImage img, final int x, final int y, final int width, final int height) {
      // clear the prior image
      Graphics2D imgG = (Graphics2D) img.getGraphics();
      imgG.setComposite(AlphaComposite.Clear);
      imgG.setColor(Color.black);
      imgG.fillRect(0, 0, width + blur * 2, height + blur * 2);

      final int adjX = (int) (x + blur + offsetX + (insets.left * distance));
      final int adjY = (int) (y + blur + offsetY + (insets.top * distance));
      final int adjW = (int) (width - (insets.left + insets.right) * distance);
      final int adjH = (int) (height - (insets.top + insets.bottom) * distance);

      // let the delegate paint whatever they want to be blurred
      imgG.setComposite(AlphaComposite.DstAtop);
      if (prePainter != null) prePainter.paint(imgG, adjX, adjY, adjW, adjH);
      imgG.dispose();

      // blur the prior image back into the same pixels
      imgG = (Graphics2D) img.getGraphics();
      imgG.setComposite(AlphaComposite.DstAtop);
      imgG.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
      imgG.setRenderingHint(
          RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
      imgG.drawImage(img, blurOp, 0, 0);

      if (postPainter != null) postPainter.paint(imgG, adjX, adjY, adjW, adjH);
      imgG.dispose();
    }
예제 #6
0
  @Override
  public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
    int w = getWidth() - (SHADOW_SIZE - 2) * 2;
    int h = getHeight() - (SHADOW_SIZE - 2) * 2;
    int arc = 15;
    int shadowSize = SHADOW_SIZE;
    shadow = GraphicsUtilities.createCompatibleTranslucentImage(w, h);
    Graphics2D g2 = shadow.createGraphics();

    if (active) {
      g2.setColor(new Color(243, 238, 39, 150));
    } else {
      g2.setColor(Color.WHITE);
    }

    g2.setColor(Color.WHITE);
    g2.fillOval(0, 0, w, h);
    g2.dispose();
    ShadowRenderer renderer = new ShadowRenderer(shadowSize, 0.5f, Color.BLACK);
    shadow = renderer.createShadow(shadow);
    g2 = shadow.createGraphics();
    g2.setColor(Color.GRAY);
    g2.setComposite(AlphaComposite.Clear);
    g2.fillOval(shadowSize, shadowSize, w, h);
    g2.dispose();
  }
예제 #7
0
  // Method used to paint shadow around the panel
  @Override
  public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);

    int w = getWidth() - 68;
    int h = getHeight() - 68;
    int arc = 30;
    int shadowSize = 20;

    shadow = ImagesUtilities.createCompatibleTranslucentImage(w, h);
    Graphics2D g2 = shadow.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRoundRect(0, 0, w, h, arc, arc);
    g2.dispose();

    ShadowRenderer renderer = new ShadowRenderer(shadowSize, 0.5f, Color.BLACK);
    shadow = renderer.createShadow(shadow);

    g2 = shadow.createGraphics();
    // The color does not matter, red is used for debugging
    g2.setColor(Color.RED);
    g2.setComposite(AlphaComposite.Clear);
    g2.fillRoundRect(shadowSize, shadowSize, w, h, arc, arc);
    g2.dispose();

    if (shadow != null) {
      int xOffset = (shadow.getWidth() - w) / 2;
      int yOffset = (shadow.getHeight() - h) / 2;
      g2.drawImage(shadow, x - xOffset, y - yOffset, null);
    }
  }
예제 #8
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (dst == null) dst = createCompatibleDestImage(src, null);
    BufferedImage tsrc = src;
    float cx = (float) src.getWidth() * centreX;
    float cy = (float) src.getHeight() * centreY;
    float imageRadius = (float) Math.sqrt(cx * cx + cy * cy);
    float translateX = (float) (distance * Math.cos(angle));
    float translateY = (float) (distance * -Math.sin(angle));
    float scale = zoom;
    float rotate = rotation;
    float maxDistance = distance + Math.abs(rotation * imageRadius) + zoom * imageRadius;
    int steps = log2((int) maxDistance);

    translateX /= maxDistance;
    translateY /= maxDistance;
    scale /= maxDistance;
    rotate /= maxDistance;

    if (steps == 0) {
      Graphics2D g = dst.createGraphics();
      g.drawRenderedImage(src, null);
      g.dispose();
      return dst;
    }

    BufferedImage tmp = createCompatibleDestImage(src, null);
    for (int i = 0; i < steps; i++) {
      Graphics2D g = tmp.createGraphics();
      g.drawImage(tsrc, null, null);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

      g.translate(cx + translateX, cy + translateY);
      g.scale(
          1.0001 + scale,
          1.0001
              + scale); // The .0001 works round a bug on Windows where drawImage throws an
                        // ArrayIndexOutofBoundException
      if (rotation != 0) g.rotate(rotate);
      g.translate(-cx, -cy);

      g.drawImage(dst, null, null);
      g.dispose();
      BufferedImage ti = dst;
      dst = tmp;
      tmp = ti;
      tsrc = dst;

      translateX *= 2;
      translateY *= 2;
      scale *= 2;
      rotate *= 2;
    }
    return dst;
  }
  /** @return the bitmapFont */
  public Font getBitmapFont() {
    Font bitmapFont = Font.getBitmapFont(lookupFont);
    if (bitmapFont != null) {
      return bitmapFont;
    }

    BufferedImage image = new BufferedImage(5000, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(
        RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, bitmapAntialiasing);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.setColor(new Color(0xff0000));
    g2d.setFont(java.awt.Font.decode(lookupFont.split(";")[0]));
    FontMetrics metrics = g2d.getFontMetrics();
    FontRenderContext context = g2d.getFontRenderContext();
    int height = (int) Math.ceil(metrics.getMaxDescent() + metrics.getMaxAscent());
    int baseline = (int) Math.ceil(metrics.getMaxAscent());
    String charsetStr = bitmapCharset;
    int[] offsets = new int[charsetStr.length()];
    int[] widths = new int[offsets.length];
    int currentOffset = 0;
    for (int iter = 0; iter < charsetStr.length(); iter++) {
      offsets[iter] = currentOffset;
      String currentChar = charsetStr.substring(iter, iter + 1);
      g2d.drawString(currentChar, currentOffset, baseline);
      Rectangle2D rect = g2d.getFont().getStringBounds(currentChar, context);
      widths[iter] = (int) Math.ceil(rect.getWidth());

      // max advance works but it makes a HUGE image in terms of width which
      // occupies more ram
      if (g2d.getFont().isItalic()) {
        currentOffset += metrics.getMaxAdvance();
      } else {
        currentOffset += widths[iter] + 1;
      }
    }

    g2d.dispose();
    BufferedImage shrunk = new BufferedImage(currentOffset, height, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) shrunk.getGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();

    int[] rgb = new int[shrunk.getWidth() * shrunk.getHeight()];
    shrunk.getRGB(0, 0, shrunk.getWidth(), shrunk.getHeight(), rgb, 0, shrunk.getWidth());
    com.sun.lwuit.Image bitmap =
        com.sun.lwuit.Image.createImage(rgb, shrunk.getWidth(), shrunk.getHeight());

    return com.sun.lwuit.Font.createBitmapFont(lookupFont, bitmap, offsets, widths, charsetStr);
  }
예제 #10
0
  public void paint(Panel panel) {
    Box panelBounds = panel.getAbsoluteBounds();
    int x = panelBounds.x - clip.x;
    int y = panelBounds.y - clip.y;

    Graphics2D graphics =
        (Graphics2D) rootGraphics.create(x, y, panel.getWidth(), panel.getHeight());
    paint(panel, graphics);
    graphics.dispose();
    rootGraphics.dispose();
  }
예제 #11
0
  static BufferedImage getCompositeImage(BufferedImage baseFrame, BufferedImage topFrame) {
    // create top Image
    Graphics2D graphics2D = topFrame.createGraphics();
    graphics2D.drawImage(topFrame, null, 0, 0);
    graphics2D.dispose();

    // draw top on top of baseFrame
    Graphics2D graphics2D1 = baseFrame.createGraphics();
    graphics2D1.drawImage(topFrame, null, 0, 0);
    graphics2D1.dispose();
    return ResourceManager.newFrame(baseFrame);
  }
예제 #12
0
  /**
   * Returns inner shade nine-patch icon.
   *
   * @param shadeWidth shade width
   * @param round corners round
   * @param shadeOpacity shade opacity
   * @return inner shade nine-patch icon
   */
  public static NinePatchIcon createInnerShadeIcon(
      final int shadeWidth, final int round, final float shadeOpacity) {
    // Calculating width for temprorary image
    final int inner = Math.max(shadeWidth, round);
    int width = shadeWidth * 2 + inner * 2;

    // Creating template image
    final BufferedImage bi = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D ig = bi.createGraphics();
    GraphicsUtils.setupAntialias(ig);
    final Area area = new Area(new Rectangle(0, 0, width, width));
    area.exclusiveOr(
        new Area(
            new RoundRectangle2D.Double(
                shadeWidth,
                shadeWidth,
                width - shadeWidth * 2,
                width - shadeWidth * 2,
                round * 2,
                round * 2)));
    ig.setPaint(Color.BLACK);
    ig.fill(area);
    ig.dispose();

    // Creating shade image
    final ShadowFilter sf = new ShadowFilter(shadeWidth, 0, 0, shadeOpacity);
    final BufferedImage shade = sf.filter(bi, null);

    // Clipping shade image
    final Graphics2D g2d = shade.createGraphics();
    GraphicsUtils.setupAntialias(g2d);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
    g2d.setPaint(FlatLafStyleConstants.transparent);
    g2d.fill(area);
    g2d.dispose();

    final BufferedImage croppedShade =
        shade.getSubimage(shadeWidth, shadeWidth, width - shadeWidth * 2, width - shadeWidth * 2);
    width = croppedShade.getWidth();

    // Creating nine-patch icon
    final NinePatchIcon ninePatchIcon = NinePatchIcon.create(croppedShade);
    ninePatchIcon.addHorizontalStretch(0, inner, true);
    ninePatchIcon.addHorizontalStretch(inner + 1, width - inner - 1, false);
    ninePatchIcon.addHorizontalStretch(width - inner, width, true);
    ninePatchIcon.addVerticalStretch(0, inner, true);
    ninePatchIcon.addVerticalStretch(inner + 1, width - inner - 1, false);
    ninePatchIcon.addVerticalStretch(width - inner, width, true);
    ninePatchIcon.setMargin(shadeWidth);
    return ninePatchIcon;
  }
예제 #13
0
  public static void main(String[] args) throws IOException {
    List<Color> colorlist = new ArrayList<Color>();
    for (int i = 0; i < 9000; i++) {
      if (new Random().nextBoolean()) colorlist.add(Color.red);
      else if (i > 600) colorlist.add(Color.green);
      else colorlist.add(Color.yellow);
    }
    int width = 600;
    int height = 30;
    BufferedImage image = null;
    Graphics2D g2 = null;
    int step = width / colorlist.size();
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);

    if (step >= 1) {
      image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      g2 = (Graphics2D) image.getGraphics();
      g2.setBackground(Color.WHITE);
      for (int i = 0; i < colorlist.size(); i++) {
        g2.setColor(colorlist.get(i));
        g2.fillRect(i * step, 0, i * step + step, height);
      }
    } else {
      image = new BufferedImage(colorlist.size(), height, BufferedImage.TYPE_INT_RGB);
      g2 = (Graphics2D) image.getGraphics();
      g2.setBackground(Color.WHITE);
      for (int i = 0; i < colorlist.size(); i++) {
        g2.setColor(colorlist.get(i));
        g2.drawLine(i, 0, i, height);
      }
    }
    g2.dispose();

    Image scaleImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    g2 = (Graphics2D) image.getGraphics();
    g2.drawImage(scaleImage, 0, 0, null);
    g2.dispose();

    ImageIO.write(image, "GIF", imOut);
    byte[] contentByte = bs.toByteArray();
    FileOutputStream f = new FileOutputStream("d:\\test.gif");
    f.write(contentByte);
    f.flush();
    f.close();
    bs.close();
    imOut.close();
  }
  /**
   * returns the resized and cliped img.
   *
   * @param img
   * @param newW
   * @param newH
   * @return
   */
  private static BufferedImage resizeclip(BufferedImage img, int newW, int newH) {

    int startx, width, starty, height;
    int w = img.getWidth();
    int h = img.getHeight();
    double sourceVer = 1.0 * h / w;
    double targetVer = 1.0 * newH / newW;
    if (sourceVer < targetVer) {
      starty = 0;
      height = h;
      startx = (int) (((1.0 * newH / h * w - newW) / 2) * 1.0 * h / newH);
      width = startx + (int) (newW * 1.0 * h / newH);
    } else {
      startx = 0;
      width = w;
      starty = (int) (((1.0 * newW / w * h - newH) / 2) * 1.0 * w / newW);
      height = starty + (int) (newH * 1.0 * w / newW);
    }
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
    Graphics2D g = dimg.createGraphics();
    g.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(img, 0, 0, newW, newH, startx, starty, width, height, null);
    g.dispose();
    return dimg;
  }
예제 #15
0
  private BufferedImage resizeImg(InputStream inputStream) throws IOException {
    BufferedImage originalImage = ImageIO.read(inputStream);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    int newWidth = width;
    int newHeight = height;
    // Ajusta o height de acordo com o min width
    if (width > MAX_WIDTH) {
      newWidth = MAX_WIDTH;
      newHeight = (newWidth * height) / width;

      width = newWidth;
      height = newHeight;
    }
    // Ajusta o width de acordo com o min height
    if (height > MAX_HEIGHT) {
      newHeight = MAX_HEIGHT;
      newWidth = (newHeight * width) / height;

      width = newWidth;
      height = newHeight;
    }

    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, width, height, null);
    g.dispose();

    return resizedImage;
  }
예제 #16
0
  private void drawInstance(InstancePainter painter, boolean isGhost) {
    Graphics2D g = (Graphics2D) painter.getGraphics().create();
    Location loc = painter.getLocation();
    g.translate(loc.getX(), loc.getY());

    Direction from = painter.getAttributeValue(StdAttr.FACING);
    int degrees = Direction.EAST.toDegrees() - from.toDegrees();
    double radians = Math.toRadians((degrees + 360) % 360);
    g.rotate(radians);

    GraphicsUtil.switchToWidth(g, Wire.WIDTH);
    if (!isGhost && painter.getShowState()) {
      g.setColor(painter.getPort(0).getColor());
    }
    g.drawLine(0, 0, 5, 0);

    GraphicsUtil.switchToWidth(g, 1);
    if (!isGhost && painter.shouldDrawColor()) {
      BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
      g.setColor(Value.repeat(Value.FALSE, width.getWidth()).getColor());
    }
    g.drawLine(6, -8, 6, 8);
    g.drawLine(9, -5, 9, 5);
    g.drawLine(12, -2, 12, 2);

    g.dispose();
  }
예제 #17
0
  /**
   * 将照片logo添加到二维码中间
   *
   * @param image 生成的二维码照片对象
   * @param imagePath 照片保存路径
   * @param logoPath logo照片路径
   * @param formate 照片格式
   */
  public static void overlapImage(
      BufferedImage image,
      String formate,
      String imagePath,
      String logoPath,
      MatrixToLogoImageConfig logoConfig) {
    try {
      BufferedImage logo = ImageIO.read(new File(logoPath));
      Graphics2D g = image.createGraphics();
      // 考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5;
      int width = image.getWidth() / logoConfig.getLogoPart();
      int height = image.getHeight() / logoConfig.getLogoPart();
      // logo起始位置,此目的是为logo居中显示
      int x = (image.getWidth() - width) / 2;
      int y = (image.getHeight() - height) / 2;
      // 绘制图
      g.drawImage(logo, x, y, width, height, null);

      // 给logo画边框
      // 构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke
      g.setStroke(new BasicStroke(logoConfig.getBorder()));
      g.setColor(logoConfig.getBorderColor());
      g.drawRect(x, y, width, height);

      g.dispose();
      // 写入logo照片到二维码
      ImageIO.write(image, formate, new File(imagePath));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #18
0
 /**
  * 插入LOGO
  *
  * @param source 二维码图片
  * @param imgPath LOGO图片地址
  * @param needCompress 是否压缩
  * @throws Exception
  */
 private static void insertImage(BufferedImage source, String imgPath, boolean needCompress)
     throws Exception {
   File file = new File(imgPath);
   if (!file.exists()) {
     System.err.println("" + imgPath + " 该文件不存在!");
     return;
   }
   Image src = ImageIO.read(new File(imgPath));
   int width = src.getWidth(null);
   int height = src.getHeight(null);
   if (needCompress) { // 压缩LOGO
     if (width > WIDTH) {
       width = WIDTH;
     }
     if (height > HEIGHT) {
       height = HEIGHT;
     }
     Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
     BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     Graphics g = tag.getGraphics();
     g.drawImage(image, 0, 0, null); // 绘制缩小后的图
     g.dispose();
     src = image;
   }
   // 插入LOGO
   Graphics2D graph = source.createGraphics();
   int x = (QRCODE_WIDTH - width) / 2;
   int y = (QRCODE_HEIGHT - height) / 2;
   graph.drawImage(src, x, y, width, height, null);
   Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
   graph.setStroke(new BasicStroke(3f));
   graph.draw(shape);
   graph.dispose();
 }
        /** Draws the background annoations. */
        private void draw(
            final ExecutionUnit process,
            final Graphics2D g2,
            final ProcessRendererModel rendererModel,
            final boolean printing) {
          if (!visualizer.isActive()) {
            return;
          }

          // background annotations
          WorkflowAnnotations annotations = rendererModel.getProcessAnnotations(process);
          if (annotations != null) {
            for (WorkflowAnnotation anno : annotations.getAnnotationsDrawOrder()) {
              // selected is drawn by highlight decorator
              if (anno.equals(model.getSelected())) {
                continue;
              }

              // paint the annotation itself
              Graphics2D g2P = (Graphics2D) g2.create();
              drawer.drawAnnotation(anno, g2P, printing);
              g2P.dispose();
            }
          }
        }
  public void createImage() {
    if (rect == null) return;

    if (tag == null || tag.size() < 4) return;

    BufferedImage left;
    Dimension size = new Dimension(tag.get(0).getSize());
    Dimension textSize = new Dimension(tag.get(1).getSize());

    if (isSoftkey0Text()) {
      left =
          createImage(
              tag.get(1),
              textSize,
              null,
              softKeyText,
              SWT.CENTER,
              SWT.CENTER,
              tag.textlMargin,
              tag.textrMargin);
    } else if (softKey0Icon == null) {
      left = makeImage(tag.subList(0, 1), size, null);
    } else {
      left = makeImage(tag.subList(0, 1), size, null);
      Dimension iconSize = new Dimension(iconWidth, iconHeight);
      BufferedImage temp = getBufferedImage(softKey0Icon, iconSize);
      Graphics2D g2 = left.createGraphics();
      g2.drawImage(temp, (size.width - iconWidth) / 2, (size.height - iconHeight) / 2, null);
      g2.dispose();
    }

    if (image0 != null) image0.dispose();
    image0 = null;
    image0 = new Image(Display.getDefault(), ImageUtil.convertToSWT(left));
  }
예제 #21
0
  /**
   * Returns the specified image as icon.
   *
   * @param name name of icon
   * @return icon
   */
  public static ImageIcon icon(final String name) {
    ImageIcon ii = ICONS.get(name);
    if (ii != null) return ii;

    Image img;
    if (GUIConstants.scale > 1) {
      // choose large image or none
      final URL url =
          GUIConstants.large() ? BaseXImages.class.getResource("/img/" + name + "_32.png") : null;

      if (url == null) {
        // resize low-res image if no hi-res image exists
        img = get(url(name));
        final int w = (int) (img.getWidth(null) * GUIConstants.scale);
        final int h = (int) (img.getHeight(null) * GUIConstants.scale);
        final BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.drawImage(img, 0, 0, w, h, null);
        g2.dispose();
        img = tmp;
      } else {
        img = get(url);
      }
    } else {
      img = get(name);
    }
    ii = new ImageIcon(img);
    ICONS.put(name, ii);
    return ii;
  }
예제 #22
0
  public ArrayList<BufferedImage> Split(BufferedImage orgBilde) throws IOException {

    ArrayList<BufferedImage> bilder = new ArrayList<>();

    int rader = 3;
    int kolonner = 2;
    int bildeBit = rader * kolonner;

    int splittetBredde = orgBilde.getWidth() / kolonner;
    int splittetHøyde = orgBilde.getHeight() / rader;
    int count = 0;
    BufferedImage imgs[] = new BufferedImage[bildeBit];
    for (int x = 0; x < rader; x++) {
      for (int y = 0; y < kolonner; y++) {
        imgs[count] = new BufferedImage(splittetBredde, splittetHøyde, orgBilde.getType());

        Graphics2D gr = imgs[count++].createGraphics();
        gr.drawImage(
            orgBilde,
            0,
            0,
            splittetBredde,
            splittetHøyde,
            splittetBredde * y,
            splittetHøyde * x,
            splittetBredde * y + splittetBredde,
            splittetHøyde * x + splittetHøyde,
            null);
        gr.dispose();
      }
    }
    bilder.addAll(Arrays.asList(imgs));
    return bilder;
  }
예제 #23
0
  /**
   * Draw the icon at the specified location. Icon implementations may use the Component argument to
   * get properties useful for painting, e.g. the foreground or background color.
   *
   * @param c the component which the icon belongs to
   * @param gorg the graphic object to draw on
   * @param x the upper left point of the icon in the x direction
   * @param y the upper left point of the icon in the y direction
   */
  public void paintIcon(final Component c, final Graphics gorg, final int x, final int y) {
    final Graphics2D g = (Graphics2D) gorg.create();
    if (!mousepressed) {
      g.translate(-1, -1);
    }

    if (mousepressed && mouseover) {
      g.setColor(Color.WHITE);
      g.fillRect(x + 1, y + 1, 14, 14);
    }

    g.setColor(Color.black);
    g.drawRect(x + 1, y + 1, 14, 14);
    if (mouseover) {
      g.setColor(Color.GRAY);
    }

    g.setStroke(new BasicStroke(2));
    // from top left to bottom right
    g.drawLine(x + 5, y + 5, x + 12, y + 12);
    // from bottom left to top right
    g.drawLine(x + 12, y + 5, x + 5, y + 12);

    g.dispose();
  }
  public BufferedImage[] createUI(TComponent component, int w, int h) {
    BufferedImage[] ui = GraphicsUtil.createImage(3, w, h, Transparency.OPAQUE);

    String[] color =
        new String[] {
          "Background Color", "Background Disabled Color", "Background Uneditable Color"
        };
    String[] border =
        new String[] {
          "Background Border Color",
          "Background Border Disabled Color",
          "Background Border Uneditable Color"
        };

    for (int i = 0; i < ui.length; i++) {
      Graphics2D g = ui[i].createGraphics();

      g.setColor((Color) get(color[i], component));
      g.fillRect(0, 0, w, h);

      g.setColor((Color) get(border[i], component));
      g.drawRect(0, 0, w - 1, h - 1);

      g.dispose();
    }

    return ui;
  }
 /**
  * returns the resized img with black bars.
  *
  * @param img
  * @param newW
  * @param newH
  * @return
  */
 private static BufferedImage resize(BufferedImage img, int newW, int newH) {
   int startx, wide, starty, hight;
   int w = img.getWidth();
   int h = img.getHeight();
   double sourceVer = 1.0 * h / w;
   double targetVer = 1.0 * newH / newW;
   if (sourceVer < targetVer) {
     startx = 0;
     wide = newW;
     hight = (int) 1.0 * newW / w * h;
     starty = (int) (newH - hight) / 2;
     hight += starty;
   } else {
     starty = 0;
     hight = newH;
     wide = (int) 1.0 * newH / h * w;
     startx = (int) (newW - wide) / 2;
     wide += startx;
   }
   BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
   Graphics2D g = dimg.createGraphics();
   g.setRenderingHint(
       RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
   Color color = new Color(0, 0, 0);
   g.setColor(color);
   g.fillRect(0, 0, startx == 0 ? newW : startx, starty == 0 ? newH : starty);
   g.drawImage(img, startx, starty, wide, hight, 0, 0, w, h, null);
   g.fillRect(
       startx == 0 ? 0 : wide,
       starty == 0 ? 0 : hight,
       startx == 0 ? newW : startx,
       starty == 0 ? newH : starty);
   g.dispose();
   return dimg;
 }
  private BufferedImage scale(BufferedImage image, int newWidth, int newHeight) {
    float width;
    float height;

    if (newWidth <= 0 || newHeight <= 0) {
      logger.warn("Invalid scale attempt aborted.");
      return null;
    }

    width = (float) newWidth / (float) image.getWidth();
    height = (float) newHeight / (float) image.getHeight();

    BufferedImage out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = out.createGraphics();
    RenderingHints qualityHints =
        new RenderingHints(
            RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setRenderingHints(qualityHints);
    AffineTransform at = AffineTransform.getScaleInstance(width, height);
    g2.drawImage(image, at, null);
    g2.dispose();

    return out;
  }
예제 #27
0
  void registerImage(URL imageURL) {
    if (loadedImages.containsKey(imageURL)) {
      return;
    }

    SoftReference ref;
    try {
      String fileName = imageURL.getFile();
      if (".svg".equals(fileName.substring(fileName.length() - 4).toLowerCase())) {
        SVGIcon icon = new SVGIcon();
        icon.setSvgURI(imageURL.toURI());

        BufferedImage img =
            new BufferedImage(
                icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        ref = new SoftReference(img);
      } else {
        BufferedImage img = ImageIO.read(imageURL);
        ref = new SoftReference(img);
      }
      loadedImages.put(imageURL, ref);
    } catch (Exception e) {
      Logger.getLogger(SVGConst.SVG_LOGGER)
          .log(Level.WARNING, "Could not load image: " + imageURL, e);
    }
  }
예제 #28
0
  /** This template method should set the xlink:href attribute on the input Element parameter */
  protected void handleHREF(
      RenderedImage image, Element imageElement, SVGGeneratorContext generatorContext)
      throws SVGGraphics2DIOException {
    //
    // Create an buffered image if necessary
    //
    BufferedImage buf = null;
    if (image instanceof BufferedImage
        && ((BufferedImage) image).getType() == getBufferedImageType()) {
      buf = (BufferedImage) image;
    } else {
      Dimension size = new Dimension(image.getWidth(), image.getHeight());
      buf = buildBufferedImage(size);

      Graphics2D g = createGraphics(buf);

      g.drawRenderedImage(image, IDENTITY);
      g.dispose();
    }

    //
    // Cache image and set xlink:href
    //
    cacheBufferedImage(imageElement, buf, generatorContext);
  }
예제 #29
0
  @Override
  public void setup() {
    super.setup();
    int sWidth = overrideData.getWidth();
    int sHeight = overrideData.getHeight();

    pixels = new int[tileSizeSquare];
    if (tileSizeBase == sWidth && tileSizeBase == sHeight) {
      overrideData.getRGB(0, 0, sWidth, sHeight, pixels, 0, sWidth);
    } else {
      BufferedImage tmp = new BufferedImage(tileSizeBase, tileSizeBase, 6);
      Graphics2D gfx = tmp.createGraphics();
      gfx.drawImage(
          overrideData,
          0,
          0,
          tileSizeBase,
          tileSizeBase,
          0,
          0,
          sWidth,
          sHeight,
          (ImageObserver) null);
      tmp.getRGB(0, 0, tileSizeBase, tileSizeBase, pixels, 0, tileSizeBase);
      gfx.dispose();
    }

    update();
  }
    @Override
    protected void paintContextualTaskGroupOutlines(
        Graphics g, RibbonContextualTaskGroup group, Rectangle groupBounds) {
      Graphics2D g2d = (Graphics2D) g.create();

      // SubstanceColorScheme scheme = SubstanceColorSchemeUtilities
      // .getBorderColorScheme(ribbon, ComponentState.DEFAULT);

      g2d.translate(groupBounds.x, 0);
      SeparatorPainterUtils.paintSeparator(
          ribbon,
          g2d,
          2,
          groupBounds.height * 3 / 4,
          SwingConstants.VERTICAL,
          false,
          0,
          groupBounds.height / 3,
          true);

      g2d.translate(groupBounds.width - 1, 0);
      SeparatorPainterUtils.paintSeparator(
          ribbon,
          g2d,
          2,
          groupBounds.height * 3 / 4,
          SwingConstants.VERTICAL,
          false,
          0,
          groupBounds.height / 3,
          true);

      g2d.dispose();
    }