We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trying to fill loop with shapes

So my title was meant to be array not loop
I need to create an application that will generate random spots so far my work is this

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;


public class Shape{
  private int x;
  private int y;
  private int width;
  private int height;
  private Color color;
  
// random values with particular limits
  public int randomRange(int low, int high){
   Random generator = new Random();
   return generator.nextInt(high - low +1) + low;
  }
  
  public Shape(int width, int height, int x, int y, Color color){
    Random r = new Random();
    this.width = r.nextInt(30) + 10;
    this.height = this.width;
    this.x = r.nextInt(400 - this.width) + 0;
    this.y = r.nextInt(400 - this.height) + 0;
    this.color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  }
  public void display(Graphics page) {
    page.setColor(color);
    page.drawOval(x, y, width, height); 
    page.fillOval(x, y, width, height);
  }
}
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;

public class ShapePanel extends JPanel{
  private Shape [] drawObjects = new Shape[20];
  private JButton addShape;
  private JTextField one;
  private JLabel label;
  private int count;
  
  DrawingPanel drawPanel = new DrawingPanel();
  
  public static void main (String[] args){
    JFrame frame = new JFrame();
    ShapePanel shape = new ShapePanel();
    frame.getContentPane().add (new ShapePanel());
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
  
  public ShapePanel(){
    JPanel controlPanel = new JPanel();
    addShape = new JButton("Add Shape");
    controlPanel.setPreferredSize (new Dimension(100, 400));
    one = new JTextField();
    controlPanel.add (addShape);
    label = new JLabel ("Count:");
    controlPanel.add (label);
    controlPanel.add (one);
    add(controlPanel);
    add(drawPanel);
    
    ButtonListener listener = new ButtonListener();
    addShape.addActionListener (listener);
  }
  
  private class ButtonListener implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {
      if (event.getSource() == addShape){
        if(count < drawObjects.length){
          count++;
          one.setText(" " + count);
          drawPanel.repaint();
          
        }
      }
      
    }
  }
  
  private class DrawingPanel extends JPanel{
    public DrawingPanel(){
      setPreferredSize (new Dimension(400, 400));
      setBackground (Color.pink);
      
    }
  }
  public void paintComponent (Graphics g)
  {
    super.paintComponent(g); 
    for(int i = 0; i < drawObjects.length; i++){
      drawObjects[i].display(g);
      
    }
  }
}

I get an error of null at this line drawObjects.display(g) i also tried

for(Shape s : drawObjects)
s.display(g);
3
Contributors
2
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
Mi_99
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Check are u intializing each drawObjects before you use it

drawObjects[i] = new Shapes(); // Are u doing like this.........................
Majestics
Practically a Master Poster
696 posts since Jul 2007
Reputation Points: 209
Solved Threads: 66
Skill Endorsements: 5

Your array of Shapes starts out containing 20 nulls which get replaced by Shapes when you add a new Shape. It looks like the var count contains the number of Shapes currently in the array, so that it what you need for the upper limit of your for loop - ie stop the loop before it goes into the null part of the array.
I can't see the code where you add a new Shape - shouldn't that be in the ButtonListener ?
What's the point of all the parameters on the constructor on line 21? You don't use any of them!

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30
Question Answered as of 1 Year Ago by JamesCherrill and Majestics

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0640 seconds using 2.74MB