我如何使我的星星旋转?我可以使星星旋转,但不知道如何旋转 [英] How do I make my star rotate? I am able to make the star but do not know how to make it rotate
问题描述
这是我绘制星星的代码
g2d.setStroke(new BasicStroke(5.0f));
g2d.setPaint(Color.BLACK);
g2d.drawLine(-40, -90, 40, 40);
g2d.drawLine(80, -90, 40, 40);
g2d.drawLine(80, -90, -80, 0);
g2d.drawLine(-80, 0, 160, 0);
g2d.drawLine(160, 0, -40, -90);
推荐答案
这是一种方法.另一种方法是在 Polygon
类中构建星形,然后旋转Polygon本身或旋转图形上下文.请注意,我是通过反复试验 wing 造星的.使用三角函数计算出星星的一个点,然后旋转以绘制所有五个点.
Here is one way. Another would be to build the star in a Polygon
class and either rotate the Polygon itself or rotate the graphics context. Note that I winged the star creation by trial and error. The one point of the star was calculated using trigonometry and then rotated to draw all five points.
此外,请注意,我是在 paintComponent
方法中建立星号的.最好不要这样做,因为将事件分发线程中的处理保持在最少水平很重要.
Also, note that I built the star in the paintComponent
method. Probably not the best thing to do as it is important to keep processing in the Event Dispatch Thread to a minimum.
速率旋转受角度增量和计时器延迟的控制.在这种情况下,该计时器是一个Swing计时器.
The rate rotation is controlled by both the angle increment and the timer delay. And in this case the timer is a Swing timer.
如果您想看到星星的旋转,只需注释掉我的 drawStar
方法调用,然后在 paintComponent
If you want to see your version of the star rotate, just comment out my drawStar
method call and and uncomment your code within paintComponent
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class StarPicture {
JFrame frame = new JFrame();
MyPanel panel = new MyPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new StarPicture().start());
}
public void start() {
Timer t = new Timer(0, (ae)-> frame.repaint());
t.setDelay(50); //50 milliseconds per rotational increment
t.start();
}
public StarPicture() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
// center on screen
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class MyPanel extends JPanel {
double angle = 0;
static double RADIUS = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
// visually smooth the lines
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(2.0f));
g2d.setPaint(Color.BLACK);
// set the origin to the center of the panel
g2d.translate(250,250);
g2d.rotate(angle);
// draw the star in the center of the panel
drawStar(g2d);
// g2d.drawLine(-40, -90, 40, 40);
// g2d.drawLine(80, -90, 40, 40);
// g2d.drawLine(80, -90, -80, 0);
// g2d.drawLine(-80, 0, 160, 0);
// g2d.drawLine(160, 0, -40, -90);
// adjust the amount of rotation per timer interval
angle += Math.PI/200;
g2d.dispose();
}
public static void drawStar(Graphics2D g) {
int a = (int)(RADIUS*Math.cos(Math.toRadians(54)));
int b = (int)(RADIUS*Math.sin(Math.toRadians(54)));
int y = (int)(a*Math.tan(Math.toRadians(72)))+b;
for (int i = 0; i < 5; i++) {
g.drawLine(-a,b,0,y);
g.drawLine(a,b,0,y);
g.rotate(Math.toRadians(72));
}
}
public Dimension getPreferredSize() {
return new Dimension(500,500);
}
}
我建议您阅读有关Graphics2D类和AffineTransform类中的所有方法的更多信息,以了解您可以做什么.另外,请查看 Java教程,以了解有关绘画的更多信息.
I suggest you read more about all the methods in the Graphics2D classes, and AffineTransform classes to understand what all you can do. Also, check out the Java Tutorials to learn more about painting.
这篇关于我如何使我的星星旋转?我可以使星星旋转,但不知道如何旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!