// Scott DeRuiter
// 3/12/2013 - Greenstein: Change from applet to JFrame
// March 12, 2013
// Sine.java
// Creates a multicolored sine curve.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Sine
{
JFrame frame;
DisplaySine panel;
public static void main (String[] args)
{
Sine sin = new Sine();
sin.Run();
}
public void Run()
{
frame = new JFrame("Sine Wave");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create JPanel and add to frame
panel = new DisplaySine();
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 DisplaySine extends JPanel
{
DisplaySine()
{
setBackground(Color.black);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for ( int i = 0; i <= 400; i++ )
{
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.fillOval ( i, 150 + (int)(150 * Math.sin(2 * Math.PI * i / 200)), 10, 10 );
}
}
}
Back to Lesson 24 Examples
Back to Java Main Page