在您將其標(biāo)記為重復(fù)之前我已經(jīng)在互聯(lián)網(wǎng)上進(jìn)行了大量搜索,并嘗試了每種解決方案,但是沒有人以與我相同的方式來做。在我的情況下,輪換是一門精打細(xì)算的課程。我創(chuàng)建了一個繼承JLabel類的Java類,在我的類中有一個BufferedImage使用該paintComponent(Graphics g)方法繪制的箭頭。我試圖使箭頭指向特定的點(我是從另一種方法獲得的),但是出了點問題,箭頭旋轉(zhuǎn)了錯誤的方向。我認(rèn)為:由于imageLocation是相對于標(biāo)簽的,因此計算不正確。這是我的代碼:package pkg1;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import javax.imageio.*;import javax.swing.*;public final class ImageLabel extends JLabel { private float angle = 0.0f; // in radians private Point imageLocation = new Point(); private File imageFile = null; private Dimension imageSize = new Dimension(50, 50); private BufferedImage bi; private BufferedImage resizeImage(BufferedImage originalImage, int img_width, int img_height) { int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizedImage = new BufferedImage(img_width, img_height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, img_width, img_height, null); g.dispose(); return resizedImage; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (bi == null) { return; } imageLocation = new Point(getWidth() / 2 - bi.getWidth() / 2, getHeight() / 2 - bi.getHeight() / 2); Graphics2D g2 = (Graphics2D) g; g2.rotate(angle, imageLocation.x + bi.getWidth() / 2, imageLocation.y + bi.getHeight() / 2); g2.drawImage(bi, imageLocation.x, imageLocation.y, null); } public void rotateImage(float angle) { // rotate the image to specific angle this.angle = (float) Math.toRadians(angle); repaint(); } public void pointImageToPoint(Point target) { calculateAngle(target); repaint(); }
添加回答
舉報
0/150
提交
取消