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);

   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){
      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);
         }  catch(InterruptedException e){}

Recommended Answers

the setColor should be in the paint method

Jump to Post

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 learning and sharing knowledge.