3

JLabel に追加された画像を回転できます。唯一の問題は、高さと幅が等しくない場合、回転した画像が JLabel の原点 (0,0) に表示されなくなることです。

これが私がやっていることです。また、AffineTransform を使用して画像自体を回転させてみましたが、結果は同じでした。

Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2);
super.paintComponent(g2d);

幅が高さよりも大きい画像がある場合、この方法を使用してその画像を回転させてからペイントすると、画像はポイント 0,0 の上に垂直に、ポイント 0,0 の右側に水平にペイントされます。

4

4 に答える 4

4

g2d.transform() を使用して、画像を必要な場所に戻します。計算はどうなるか想像しているだけですが、次のようなものだと思います。

int diff = ( image.getWidth() - image.getHeight() ) / 2;
g2.transform( -diff, diff );

ところで、優先サイズを報告するラベルに問題がある可能性があります.getPreferredSize()をオーバーライドし、回転した場合は画像の幅と高さを交換する必要があるかもしれません.

于 2010-04-22T03:54:51.053 に答える
3
AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, image.getHeight(), 0.0);
g2d.transform(trans);
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
于 2010-04-22T06:09:15.503 に答える
2

次の関数は、完全な正方形であるかどうかが不確定なバッファリングされた画像を回転させます。

public BufferedImage rotate(BufferedImage image)
{
  /*
   * Affline transform only works with perfect squares. The following
   *   code is used to take any rectangle image and rotate it correctly.
   *   To do this it chooses a center point that is half the greater
   *   length and tricks the library to think the image is a perfect
   *   square, then it does the rotation and tells the library where
   *   to find the correct top left point. The special cases in each
   *   orientation happen when the extra image that doesn't exist is
   *   either on the left or on top of the image being rotated. In
   *   both cases the point is adjusted by the difference in the
   *   longer side and the shorter side to get the point at the 
   *   correct top left corner of the image. NOTE: the x and y
   *   axes also rotate with the image so where width > height
   *   the adjustments always happen on the y axis and where
   *   the height > width the adjustments happen on the x axis.
   *   
   */
  AffineTransform xform = new AffineTransform();

  if (image.getWidth() > image.getHeight())
  {
    xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth());
    xform.rotate(_theta);

    int diff = image.getWidth() - image.getHeight();

    switch (_thetaInDegrees)
    {
    case 90:
      xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
      break;
    case 180:
      xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
      break;
    default:
      xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth());
      break;
    }
  }
  else if (image.getHeight() > image.getWidth())
  {
    xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight());
    xform.rotate(_theta);

    int diff = image.getHeight() - image.getWidth();

    switch (_thetaInDegrees)
    {
    case 180:
      xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
      break;
    case 270:
      xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
      break;
    default:
      xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight());
      break;
    }
  }
  else
  {
    xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight());
    xform.rotate(_theta);
    xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth());
  }

  AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR);

  BufferedImage newImage =new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
  return op.filter(image, newImage);
}
于 2012-06-20T15:17:53.397 に答える
1

Rotated Iconクラスが探しているものかもしれません。

于 2010-04-22T02:45:27.317 に答える