【问题标题】:Rotating Animated Lines in Graphics2D (Java)在 Graphics2D (Java) 中旋转动画线
【发布时间】:2018-08-16 21:28:39
【问题描述】:

我目前正在使用 Java 制作 Color Switch 游戏的副本,我使用 Graphics 创建了一个 4 色矩形(作为障碍物),但我不确定如何使用中心作为点来旋转所有线条旋转(制作动画)。

我想让它以一定的速度旋转,我怎样才能开始旋转并让它保持旋转?

这是我的代码;

package rectangle;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

class Frame extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = 1L;
public static final Color colors[] = {new Color(50, 226, 241),
        new Color(244, 222, 14), new Color(140, 18, 251), new Color(255, 0, 128)};
public static int xval = 0;
public static int yval = 0;
public static int strokeval = 20;
public static int xsizeFrame = 450;
public static int ysizeFrame = 450;
public static int xMidFrame = xsizeFrame/2;
public static int yMidFrame = ysizeFrame/2;

public Frame(){
    JPanel panel=new JPanel();
    getContentPane().add(panel);
    setSize(xsizeFrame,ysizeFrame);

}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(new BasicStroke(strokeval));

    g2.setColor(colors[0]);
    Line2D lin2 = new Line2D.Float(xMidFrame-50-strokeval,yMidFrame-50,xMidFrame-50-strokeval,yMidFrame+50);
    g2.draw(lin2);

    g2.setColor(colors[1]);
    Line2D lin1 = new Line2D.Float(xMidFrame-50,yMidFrame-50-strokeval,xMidFrame+50,yMidFrame-50-strokeval);
    g2.draw(lin1);

    g2.setColor(colors[2]);
    Line2D lin3 = new Line2D.Float(xMidFrame+50+strokeval,yMidFrame-50,xMidFrame+50+strokeval,yMidFrame+50);
    g2.draw(lin3);

    g2.setColor(colors[3]);
    Line2D lin4 = new Line2D.Float(xMidFrame-50,yMidFrame+50+strokeval,xMidFrame+50,yMidFrame+50+strokeval);
    g2.draw(lin4);

}

public static void main(String []args){
    Frame s=new Frame();
    s.setVisible(true);
  }
}

谢谢。

【问题讨论】:

标签: java swing jframe awt


【解决方案1】:

这里有一个小例子,可以帮助你理解如何在摇摆中提供动画。

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class SwingAnimationExample extends JPanel implements ActionListener {

    private static final int MAX_STATE = 4;

    private int state;

    public SwingAnimationExample() {
        // start timer to provide state change and repaint
        Timer t = new Timer(250, this);
        t.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // painting depended  on the current state
        if (state == 0) {
            g.drawLine(0, 0, getWidth(), getHeight());
        } else if (state == 1) {
            g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
        } else if (state == 2) {
            g.drawLine(getWidth(), 0, 0, getHeight());
        } else if (state == 3) {
            g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // change the current state and invoke repaint
        state++;
        if (state == MAX_STATE) {
            state = 0;
        }
        repaint();
    }

    public static void main(String[] args) {
        JFrame frm = new JFrame("Test timer");
        frm.add(new SwingAnimationExample());
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setSize(500, 400);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

这个动画非常基础。要提供 smothe 动画,您应该提供更多状态并减少计时器延迟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多