import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.Timer; import java.awt.Graphics; public class test extends JPanel { public test() { mTimer = new Timer( 100, new ActionListener() { public void actionPerformed(ActionEvent e) { if(++mDegrees == 360) mDegrees = 0; repaint(); } } ); mTimer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); int size = (int)(.45 * Math.min(w, h) + 0.5); int xFirst = 0, yFirst = 0, xOld = 0, yOld = 0; for(int i = 0, degrees = mDegrees; i < N_SIDES; i ++, degrees += STEP) { double radians = Math.toRadians(degrees); int x = (int)(w / 2 + size * Math.cos(radians) + 0.5); int y = (int)(h / 2 + size * Math.sin(radians) + 0.5); if(i == 0) { xFirst = x; yFirst = y; } else { g.drawLine(x, y, xOld, yOld); if(i + 1 == N_SIDES) g.drawLine(x, y, xFirst, yFirst); } xOld = x; yOld = y; } } private Timer mTimer; private int mDegrees; private static final int N_SIDES = 5; private static final int STEP = (360 / N_SIDES); public static void main(String[] args) { JFrame j = new JFrame("test"); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.add(new test()); j.setSize(500, 500); j.setVisible(true); } }