//	Scott DeRuiter
//  3/12/2013 - Greenstein: Change from applet to JFrame
//	March 12, 2013
//  Swirl.java
//  Creates a multicolored swirl

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

public class Swirl 
{
	JFrame frame;
	DisplaySwirl panel;
	
	public static void main(String[] args) 
	{
		Swirl swirl = new Swirl();
		swirl.Run();
	}

	public void Run() 
	{
		frame = new JFrame("Swirl");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Create JPanel and add to frame
		panel = new DisplaySwirl();
		frame.getContentPane().add(panel, BorderLayout.CENTER);	// add panel to frame
		
		frame.setSize(500, 500);		// explicitly set size in pixels
		frame.setVisible(true);		// set to false to make invisible

	}
}

class DisplaySwirl extends JPanel 
{
	DisplaySwirl() 
	{
		setBackground(Color.lightGray);
	}

	public void paintComponent(Graphics g) 
	{
		super.paintComponent(g);
		int i = 1;
		while ( i <= 1100 )   
		{
			int value = i % 6 + 1;
			switch ( value )   
			{
				case 1:  g.setColor ( Color.blue );   break;
				case 2:  g.setColor ( Color.black );   break;
				case 3:  g.setColor ( Color.green );   break;
				case 4:  g.setColor ( Color.gray );   break;
				case 5:  g.setColor ( Color.yellow );   break;
				case 6:  g.setColor ( Color.red );   break;
			}
			g.drawOval ( 250 + (int)(i / 4.0 * Math.sin(2 * Math.PI * i / 400)), 
				250 + (int)(i / 4.0 * Math.cos(2 * Math.PI * i / 400)),10, 10 );
			i++;
		}
	}
}

Back to Lesson 24 Examples

Back to Java Main Page