java 线绕点旋转_围绕字符旋转图像(JAVA)

你的意思是…

e535f7ea48c6085711a1b4cb48b031c6.png

(请注意,红线是指示中心angular度的引导线,您不需要它;))

花哨的, getSwordHandlePoint东西在getSwordHandlePoint方法中,它计算沿着向量应该放置句柄的点…

public class TestSword { public static void main(String[] args) { new TestSword(); } public TestSword() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new SwordPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class SwordPane extends JPanel { private BufferedImage character; private BufferedImage sword; private double angle = 0; public SwordPane() { try { character = ImageIO.read(new File("character.png")); sword = ImageIO.read(new File("Sword.png")); } catch (IOException exp) { exp.printStackTrace(); } Timer timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { angle += 10; if (angle > 360) { angle -= 360; } repaint(); } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); } @Override public Dimension getPreferredSize() { int width = character.getHeight() + sword.getWidth(); int height = character.getHeight() + sword.getWidth(); return new Dimension(width * 2, height * 2); } protected Point getSwordHandlePoint() { int radius = 272; // This is the height of the character... int x = Math.round(getWidth() / 2); int y = Math.round(getHeight() / 2); double rads = Math.toRadians(angle - 180); // Make 0 point out to the right... // If you add sword.getWidth, you might be able to change the above... int fullLength = Math.round((radius / 2f)) - sword.getWidth(); // Calculate the outter point of the line int xPosy = Math.round((float) (x + Math.cos(rads) * fullLength)); int yPosy = Math.round((float) (y - Math.sin(rads) * fullLength)); return new Point(xPosy, yPosy); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); int x = (getWidth() - character.getWidth()) / 2; int y = (getHeight() - character.getHeight()) / 2; g2d.drawImage(character, x, y, this); x = getWidth() / 2; y = getHeight() / 2; Point p = getSwordHandlePoint(); g2d.setColor(Color.RED); g2d.drawLine(x, y, px, py); AffineTransform at = AffineTransform.getTranslateInstance(px, py); at.rotate(Math.toRadians(-angle)); g2d.setTransform(at); g2d.drawImage(sword, 0, 0, this); g2d.dispose(); } } }

现在,我的触发是无望的,没有那么绝望。 我从网上“借用”algorithm,并根据自己的需要调整。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值