Reputation: 131
I am currently doing a copy of Color Switch game using Java, I have created a 4 colored rectangle(as an obstacle) using Graphics but I am not sure how can I rotate all of the lines using the center as the point of rotation(make an animation).
I wanted to make it rotates for a certain speed and how can I start doing the rotation and make it stay rotating?
This is my code;
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);
}
}
Thank you.
Upvotes: 0
Views: 378
Reputation: 11327
Here is a small example that helps you to understand, how to provide animation in swing.
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);
}
}
This animation is very basic. To provide a smothe animation you should provide more states and decrease the timer delay.
Upvotes: 2