0

My goal is to create the following: a series of concentric circles, each with a random colour as a its outline, hopefully creating something pretty. My method for doing this is to draw a series of ovals(unfilled) with increasing diameter, ranging from a variable innerDiameter towards outerDiameter.

I'm having several problems and will phrase them in such a way that others can take information from this thread as opposed to it just helping me.

1) On Eclipse, I have an exclamation mark next to the declaration of my JPanel: mainPanel, saying that it is never used, when it clearly is later on in the code. I've put a comment beside this line to make it clearer. I've tried closing eclipse and cutting and repasting the source code etc. so I'm guessing it's something stupid.

2) I'm having NullPointerExceptions thrown, firstly at the line that tries to assign y's value to the frame's height less the variable i. I have no idea why this is happening; I understand that on the first loop round the value of y would be zero but why this means an error occurs I have no idea. Again see comments on source.

3) To check this I then changed the value of y to a constant number, at which point the next line throws the error, and I honestly have no idea why and have been at this for a while.

4) Debugging is not helping, I put a breakpoint at the beginning of a method and try to "Step Over" line by line only to find the debugger skips and ends up at the errors mentioned above. Any idea why this is happening?

Happy to rephrase or edit the post to make it more useful to others, I'm new to Java so appreciate all the help everyone provides here. Thanks

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

public class CircleGenerator {
    private int outerDiameter;
    private int innerDiameter;
    private int colorRange;
    private Color bgColor;
    private JFrame frame;        // 1)this is where my first point occurs.
    private JPanel mainPanel;
    //private JButton nextCircleButton;
    private CircleDrawer drawer;

    public CircleGenerator(int outer, int inner, int colorRange, Color bgColor) {
        this.outerDiameter = outer;
        this.innerDiameter = inner;
        this.colorRange = colorRange;
        this.bgColor = bgColor;
    }

    public static void main(String[] args) {
        CircleGenerator myGenerator = new CircleGenerator(30,3,200,Color.RED);
        myGenerator.setupGUI();
        //myGenerator.genCircle();
    }
    public void setupGUI() {
        // Sets up the environment for the circles to be drawn
        JFrame frame = new JFrame("Beautiful Circles");
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(bgColor);
        //JButton nextCircleButton = new JButton("Next Circle");          //will use later to regenerate more circles
        CircleDrawer drawer = new CircleDrawer();
        mainPanel.add(drawer);
        frame.getContentPane().add(mainPanel);
        frame.setSize(outerDiameter+innerDiameter, outerDiameter+innerDiameter);
        frame.setVisible(true);
        genCircle();    
    }

    private void genCircle() {
        // generate the necessary parameters to send to the Circle Drawer
        for (int i = innerDiameter; i < outerDiameter+1; i++) {
            int red = (int) (Math.random() * colorRange) + 1;
            int green = (int) (Math.random() * colorRange + 1);
            int blue = (int) (Math.random() * colorRange + 1);
            int x = i - innerDiameter;
            int y = frame.getContentPane().getHeight() - i;         //2) the first NullPointerException
            drawer.updateValues(i, x, y, red, green, blue);         //3) where another NullPointerException is thrown.
            drawer.repaint();
        }

    }

    class CircleDrawer extends JPanel {
        private int diameter;
        private int x;       //  the two x and y values are for the
        private int y;       //  coordinates of the upper left corner of the oval.
        private int red;
        private int green;
        private int blue;

        void updateValues(int diameter, int x, int y, int red, int green, int blue) {
            this.diameter = diameter;
            this.x = x;
            this.y = y;
            this.red = red;
            this.green = green;
            this.blue = blue;
        }

        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(new Color(red,green,blue));
            g.drawOval(x,y,diameter,diameter);
        }
    }

}
quantum285
  • 1,032
  • 2
  • 11
  • 23

0 Answers0