1. /**
  2. * Moire.java
  3. * @author Jose M Vidal
  4. * Created on Jan 31, 2012
  5. * A simple Moire circular pattern.
  6. */
  7. import java.awt.BasicStroke;
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.Stroke;
  12. 
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. 
  16. public class Moire extends JPanel {
  17. 
  18. public void paintComponent(Graphics g){
  19. Graphics2D g2 = (Graphics2D) g;
  20. g2.setColor(Color.black);
  21. Stroke s = new BasicStroke(5);
  22. g2.setStroke(s);
  23. int count = 0;
  24. for (int r = 0; r < 500; r+= 20){
  25. if (count % 2 == 0)
  26. g2.setColor(Color.blue);
  27. else
  28. g2.setColor(Color.red);
  29. count++;
  30. g2.drawOval(250- r/2, 300-r/2, r, r);
  31. g2.drawOval(350-r/2, 300-r/2, r, r);
  32. }
  33. }
  34. 
  35. 
  36. // You do NOT need to change anything in main(). Leave it as is.
  37. public static void main(String[] args) {
  38. JFrame frame = new JFrame("Window"); //frame is the window
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. Moire panel = new Moire(); //panel is the graphics area where we can draw
  41. frame.add(panel); //put the panel inside the window
  42. frame.setSize(640,640); //set the window size to 640x640 pixels
  43. frame.setVisible(true);
  44. }
  45. }