Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created September 11, 2012 04:42
Show Gist options
  • Save chintanparikh/3696008 to your computer and use it in GitHub Desktop.
Save chintanparikh/3696008 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
* An applet to create a mood ring
*/
public class MoodRingApplet extends JApplet{
public void paint(Graphics page) {
Random generator = new Random();
Color ringColor = new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256));
Color backgroundColor = Color.black;
int width = getWidth();
int height = getHeight();
int ringWidth = generator.nextInt(10) + 5;
int innerRadius = generator.nextInt(Math.min(width, height)/2 - ringWidth);
int outerRadius = innerRadius + ringWidth;
// Draw black background
page.setColor(backgroundColor);
page.fillRect(0, 0, width, height);
page.setColor(ringColor);
page.fillOval(width/2 - outerRadius, height/2 - outerRadius, outerRadius*2, outerRadius*2);
page.setColor(backgroundColor);
page.fillOval(width/2 - innerRadius, height/2 - innerRadius, innerRadius*2, innerRadius*2);
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
page.repaint();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment