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)
{
}
}
}
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
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:
repaint()
.For example: