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){}

Edited by Dani: Formatting fixed

5 Years
Discussion Span
Last Post by zeroliken
Featured Replies
  • the setColor should be in the paint method Read More

  • [QUOTE] but nothing is drawn.[/QUOTE] the drawing should be done in the paint method, then try to use the values you have in the drawOneCircle method for the coordinates here's an link for more info about this that might help [URL="http://en.wikibooks.org/wiki/Java_Programming/Graphics/Drawing_shapes"]http://en.wikibooks.org/wiki/Java_Programming/Graphics/Drawing_shapes[/URL] Read More


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.