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

public class RandomCircles extends Canvas{

   private static int delay;
   private static int numCircles;
   private static int frameSize;
   public static Graphics g;

  public void paint(Graphics g){
   }

   public static void main(String args[]){

      frameSize = 800;
      numCircles = 1;
      delay = 5;
      RandomCircles circles = new RandomCircles();
      JFrame frame = new JFrame("Random Circles");
      frame.setBounds(0, 0, frameSize, frameSize);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(circles);
      frame.setVisible(true);

      drawAllCircles();
   }
   private static void drawAllCircles(){
      for(int j = 0; j < numCircles; j++){
         int red = (int)(256 * Math.random());
         int green = (int)(256 * Math.random());
         int blue = (int)(256 * Math.random());
         Color color = new Color(red, green, blue);
         int radius = (int)((frameSize / 4 + 1) * Math.random());
         int xCenter = radius + (int)((frameSize - 2 * radius + 1) * Math.random());
         int yCenter = radius + (int)((frameSize - 2 * radius + 1) * Math.random());
         drawOneCircle(xCenter, yCenter, radius, color);
      }
   }
   public static void drawOneCircle(int xCenter, int yCenter, int radius, Color color){
      g.setColor(color);
      for(int angle = 360; angle >= 1; angle--){
         double radians = (angle + 90)* Math.PI / 180;
         int xEdge = xCenter + (int)(Math.sin(radians) * radius);
         int yEdge = yCenter - (int)(Math.sin(radians) * radius);
         try{
            Thread.sleep(delay);
         }  catch(InterruptedException e){}
      }
   }
}

Recommended Answers

All 3 Replies

the setColor should be in the paint method

the setColor should be in the paint method

Thank you so much for your reply. That did indeed take care of the null pointer. Now the frame opens with no error messages, but nothing is drawn. Am I leaving something out in that drawOneCircle method?

By the way I got the outline of this program from Java Programming from the Beginning by K. N. King (which is a pretty good book, I think) but he uses his own package for graphics which seems to be outdated and doesn't work.

Again, thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.