java 矩形 旋转_java-旋转矩形并将其在sin波中移动-使用grap...

SSCCE为1

1)不要忘记调用super.paintComponent();作为重写的paintComponent(..)方法中的第一条语句.

2)Swing UI应该在EDT上创建并与Swing Timers结合使用

3)类的Java变量命名约定是每个新单词的大写字母,即WithRotation.

4)不需要frame.getContentPane.add(..),只需使用add(..),因为所有调用都将转发到其contentPane.

这是我制作的示例(基本上是实现了上述修复的代码),该示例仅旋转跟随图形的矩形,而不旋转使用AffineTransform#createTransformedShape()的整个图形对象:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.Shape;

import java.awt.event.ActionEvent;

import java.awt.geom.AffineTransform;

import javax.swing.AbstractAction;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.Timer;

public class WithRotation {

private JFrame frame;

private WavyPanel wp;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

new WithRotation();

}

});

}

public WithRotation() {

initComponents();

}

private void initComponents() {

frame = new JFrame("Wavy!");

wp = new WavyPanel();

frame.add(wp, BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

createAndStartTimers();

}

private void createAndStartTimers() {

new Timer(50, new AbstractAction() {

@Override

public void actionPerformed(ActionEvent ae) {

wp.repaint();

}

}).start();

new Timer(10, new AbstractAction() {

@Override

public void actionPerformed(ActionEvent ae) {

wp.tick();

}

}).start();

}

class WavyPanel extends JPanel {

private final Dimension size = new Dimension(640, 480);

private int amplitude = 50;

private int frequency = 5;

private double x1 = 0;

private double y1 = 500;

private int yBase = 0;

WavyPanel() {

super(true);

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.clearRect(0, 0, this.getSize().width, this.getSize().height);

g2.setColor(Color.BLACK);

Rectangle rect = new Rectangle((int) x1, (int) y1, 20, 80);

AffineTransform transform = new AffineTransform();

transform.rotate(Math.toRadians(-30), rect.getX() + rect.width / 2, rect.getY() + rect.height / 2);

Shape transformed = transform.createTransformedShape(rect);

g2.fill(transformed);

}

@Override

public Dimension getPreferredSize() {

return size;

}

@Override

public Dimension getMinimumSize() {

return size;

}

@Override

public Dimension getMaximumSize() {

return size;

}

public void tick() {

x1 = x1 + 1;

final int waveLength = size.width / frequency;

yBase = (++yBase) % waveLength;

final double normalized = (double) yBase / (double) waveLength;

final double radians = normalized * Math.PI * 2;

final double sine = Math.sin(radians);

y1 = (int) (sine * amplitude);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值