Пример #1
0
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (image != null) {
      Dimension imageSize = calcDimension(image);
      System.out.println(imageSize + " or " + imageSize.getWidth());
      g.drawImage(
          image,
          getWidth() / 2 - imageSize.width / 2,
          getHeight() / 2 - imageSize.height / 2,
          imageSize.width,
          imageSize.height,
          this);

      g.setXORMode(Color.white);

      Font font = g.getFont().deriveFont(8);
      g.setFont(font);
      int fileNameWidth = g.getFontMetrics(font).stringWidth(fileName);

      if (fileNameWidth > imageSize.width) {
        pathStrings = fileName.split("\\\\");
        result = pathStrings[0] + "\\...\\" + pathStrings[pathStrings.length - 1];
        resultWidth = g.getFontMetrics(font).stringWidth(result);

        // TODO:
        // Add another if for truncating to just a few letters if needed.
      } else {
        result = fileName;
        resultWidth = g.getFontMetrics(font).stringWidth(result);
      }

      g.drawString(
          result,
          getWidth() / 2 + imageSize.width / 2 - resultWidth - 5,
          getHeight() / 2 + imageSize.height / 2 - 5);
    } else g.drawImage(image, 0, 0, this);
  }
Пример #2
0
    public void doPaint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;

      g2.setColor(Color.black);

      BufferedImage bimg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
      Graphics ig = bimg.getGraphics();
      Color alphared = new Color(255, 0, 0, 128);
      Color alphagreen = new Color(0, 255, 0, 128);
      Color alphablue = new Color(0, 0, 255, 128);
      ig.setColor(alphared);
      ig.fillRect(0, 0, 200, 200);
      ig.setColor(alphagreen);
      ig.fillRect(25, 25, 150, 150);
      ig.setColor(alphablue);
      ig.fillRect(75, 75, 125, 125);
      g.drawImage(bimg, 10, 25, this);

      GradientPaint gp = new GradientPaint(10.0f, 10.0f, alphablue, 210.0f, 210.0f, alphared, true);
      g2.setPaint(gp);
      g2.fillRect(10, 240, 200, 200);
    }