Why won't this paint the circles??? This program is supposed to paint the number of circles that I enter into the JText field (I will max it at 99 circles probably), but as it stands, it will not even paint the one circle in the following code. I have tried adding if circle != null in the paintComponent to catch the nullPointerException, but still no circles. By the way, I am new to
Java (a few weeks) so any replies should speak in terms of only the items I've used here. In other words, I don't have the know-how to completely re-write the program. Is there something structurally wrong with it as it is (such as a loop being inside of the listener method, or something like that)?
MAIN (Circles class) ---------------------------------------
import java.awt.*;
import javax.swing.*;
public class Circles
{
public static final short FRAME_WIDTH = 400;
public static final short FRAME_HEIGHT = 300;
public static final short TEXT_AREA = 80;
public static void main (
String[] args)
{
Panel bottomPanel;
TopPanel topPanel;
topPanel = new TopPanel();
bottomPanel = new Panel();
JFrame frame = new JFrame ("Brian Turnpaugh");
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Center frame on screen & set visibility state
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension framesize = frame.getSize();
if (framesize.height > screensize.height)
framesize.height = screensize.height;
if (framesize.width > screensize.width)
framesize.width = screensize.width;
frame.setLocation((screensize.width - framesize.width) / 2,
(screensize.height - framesize.height) / 2);
JPanel primary = new JPanel();
primary.setBackground (Color.black);
primary.add (topPanel);
primary.add (bottomPanel);
frame.getContentPane().add(primary);
frame.setVisible(true);
}
}
Panel Class and TopPanel class------------------------------
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Panel extends JPanel
{
public final short FRAME_WIDTH = 400;
public final short FRAME_HEIGHT = 300;
public final short TEXT_AREA = 80;
JTextField inputBox;
JLabel inputLabel, holdingLabel;
private Circle circle;
public Panel ()
{
final short LABEL_WIDTH = 215;
final short LABEL_HEIGHT = 20;
setBackground (Color.black);
setPreferredSize (new Dimension(FRAME_WIDTH, TEXT_AREA));
inputLabel = new JLabel ("Enter the number of circles:");
inputLabel.setFont (new Font("Arial", Font.BOLD, 16));
inputLabel.setPreferredSize (new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
inputLabel.setForeground (Color.yellow);
add (inputLabel);
inputBox = new JTextField("", 2);
inputBox.addActionListener (new TextListener());
add (inputBox);
holdingLabel = new JLabel ();
holdingLabel.setForeground (Color.yellow);
holdingLabel.setPreferredSize (new Dimension(20, 20));
add (holdingLabel);
}
private class TextListener extends JPanel implements ActionListener
{
public static final short LOWEST_WHITE = 245;
private String circleCount;
private byte count;
private int x, y, rgb, size;
private Color randomWhite;
private Color shade;
private Circle circle;
public void actionPerformed (ActionEvent event)
{
circleCount = inputBox.getText();
holdingLabel.setText(circleCount);
//System.out.println(circleCount);
count = Byte.parseByte(circleCount);
Random randomizer = new Random();
for (byte index = 0; index <= count - 1; index++)
{
size = (randomizer.nextInt(41) + 10);
rgb = randomizer.nextInt(11) + LOWEST_WHITE;
Color randomWhite = new Color(rgb, rgb, rgb);
Color shade = randomWhite;
x = (randomizer.nextInt(FRAME_WIDTH + 1));
y = (randomizer.nextInt(FRAME_HEIGHT - TEXT_AREA + 1));
circle = new Circle (size, shade, x, y);
};
}
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);
circle.draw(page);
}
}
final class TopPanel extends JPanel
{
public final short FRAME_WIDTH = 400;
public final short FRAME_HEIGHT = 300;
public final short TEXT_AREA = 80;
public TopPanel ()
{
setBackground (Color.black);
setPreferredSize (new Dimension(FRAME_WIDTH, FRAME_HEIGHT - TEXT_AREA));
}
}
CIRCLE CLASS--------------------------------------------
import java.awt.*;
public class Circle
{
private int diameter, x, y;
private Color color;
public Circle (int size, Color shade, int upperX, int upperY)
{
diameter = size;
color = shade;
x = upperX;
y = upperY;
}
public void draw (Graphics page)
{
page.setColor (color);
page.fillOval (x, y, diameter, diameter);
}
}