Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can't draw from a loop inside of a actionPerformed method

oznog65oznog65 Posts: 2Member
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), but as it stands, it will not even paint the one circle in the following code:

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

Comments

  • oznog65oznog65 Posts: 2Member
    public void paintComponent (Graphics page)
    {
    super.paintComponent(page);

    circle.draw(page);
    }

    Changing circle.draw(page) to an if statement will eliminate the NullPointerException, but it will still not draw anything into the top panel.

    if (circle != null) circle.draw(page);

    There must be a general structural problem here. Please note that I have only been working with Java for a few weeks. We have not even covered layout managers yet ... well, the class has but I'm still stuck on this first program! What I'm saying is any answer that you may post needs to use only the concepts already included in this program. It can't be too complex. Simple painting methods or those using super perhaps. Thanks!
Sign In or Register to comment.