如何围绕程序的中心旋转椭圆? [英] How to rotate an ellipse around the center of program?

查看:126
本文介绍了如何围绕程序的中心旋转椭圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我使用了 http://zetcode.com/tutorials/javagamestutorial/basics/并且我正在使用Donut程序.对于任何不熟悉的人,它都会执行 http://zetcode.com/img/gfx/javagames/donut .png 我想使椭圆围绕窗口的中心旋转,使其看起来像是在圆周上移动.

So I used tutorials from http://zetcode.com/tutorials/javagamestutorial/basics/ and I'm using the Donut program. For anyone unfamiliar it does this http://zetcode.com/img/gfx/javagames/donut.png I want to make the ellipses rotate around the center of the window, to make it look like it's moving in a circle.

到目前为止,代码是网页中的代码.

The code so far is the code from the webpage.

package donut;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

public class Board extends JPanel{
public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2 = (Graphics2D) g;

    RenderingHints rh =
            new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

    rh.put(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);

    g2.setRenderingHints(rh);

    Dimension size = getSize();
    double w = size.getWidth();
    double h = size.getHeight();

    Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
    g2.setStroke(new BasicStroke(1));
    g2.setColor(Color.gray);


    for (double deg = 0; deg < 360; deg += 5) {
        AffineTransform at =
                AffineTransform.getTranslateInstance(w / 2, h/2);
        at.rotate(Math.toRadians(deg));
        g2.draw(at.createTransformedShape(e));
    }


    while(true)
    {

    }
}
}

我通常至少可以从理论上了解到如何做到这一点(我对编程非常陌生,尤其是GUI和图形),但这次我不知道该怎么做.也许是RotateAnimation或类似的东西?希望我不会被标记为菜鸟

I can usually get the theory behind how to do it at the least(I am very new to programming, especially GUIs and graphics) but I dont know how this time. Maybe, RotateAnimation or something similar? Hopefully I dont get flagged for being a noob

推荐答案

您不能在paint方法中执行for循环,因为它将立即竞争所有循环,而不会产生阴离子.另外,在绘制过程中或事件线程中的任何地方进行一阵(true)循环都会阻止您的程序进行绘制,这会使您的程序瘫痪.相反:

You can't do that for loop in the paint method as it will compete all looping instantly resulting in no aniation. Also a while (true) loop in paint or anywhere in the event thread will prevent your program from painting which will cripple your program to a stand-still. Instead:

  • 使用Swing计时器来驱动动画.
  • 在类中而不是在绘画方法中创建您的Ellipse实例.
  • 在计时器中增加旋转角度并调用repaint().
  • 覆盖paintComponent而不绘制
  • 仅以这种方法绘画,没有逻辑.逻辑会出现在您的计时器中.
  • 在paintComponent方法中,使用Timer更改的角度进行仿射变换并绘制正确的椭圆.
  • 在此站点上搜索Java Swing动画以获取示例,因为已经多次询问该示例,因此为此而产生了许多示例程序(有些是我编写的).
  • Use a Swing Timer to drive your animation.
  • Create your Ellipse instance in the class, not in a painting method.
  • In the Timer increment your angle of rotation and call repaint().
  • Override paintComponent not paint
  • Only do painting in this method, no logic. The logic goes in your Timer.
  • In the paintComponent method, use the angle changed by the Timer to do your affine transformation and draw the correct ellipse.
  • Search this site for Java Swing animation for examples as this has been asked many times resulting in many sample programs for this (some written by me).

例如:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class RotateEllipse extends JPanel {
   private static final double ELLIPSE_W = 80;
   private static final double ELLIPSE_H = 130;
   private static final int PREF_W = 400;
   private static final int PREF_H = 400;
   private static final Stroke STROKE = new BasicStroke(5f);
   private static final Color ELLIPSE_COLOR = Color.red;
   private static final Color BACKGROUND = Color.black;
   private static final double ELLIPSE_X = PREF_W / 2 - ELLIPSE_W / 2;
   private static final double ELLIPSE_Y = PREF_H / 2 - ELLIPSE_H / 2;
   private static final int TIMER_DELAY = 15;
   private static final double DELTA_THETA = Math.toRadians(2);

   private Ellipse2D ellipse2D;
   private AffineTransform transform = new AffineTransform();
   private double theta = 0;

   public RotateEllipse() {
      ellipse2D = new Ellipse2D.Double(ELLIPSE_X, ELLIPSE_Y, ELLIPSE_W,
            ELLIPSE_H);
      setBackground(BACKGROUND);
      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setStroke(STROKE);
      g2.setColor(ELLIPSE_COLOR);
      g2.setTransform(transform);
      g2.draw(ellipse2D);
      g2.dispose();
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         theta += DELTA_THETA;
         transform = AffineTransform.getRotateInstance(theta, 
               ELLIPSE_X + ELLIPSE_W / 2, ELLIPSE_Y + ELLIPSE_H / 2);
         repaint();
      }
   }

   private static void createAndShowGUI() {
      RotateEllipse paintEg = new RotateEllipse();

      JFrame frame = new JFrame("RotateEllipse");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

这篇关于如何围绕程序的中心旋转椭圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆