#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Random circles JPanel


    I am trying to create a GUI that has 2 panels in it. One for the buttons and one for the drawing. The buttons will take what number you put in and draw that many circles each with a random location/size. I am having trouble getting my circles to show up or I dont know how to aim my circles to my drawPanel:

    Heres my code:
    Code:
    public class Circles {
    	public static void main(String[]args){
    	JFrame frame = new JFrame("Cicles HW9");
    	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    	
    	frame.getContentPane().add(new CirclesPanel());
    
    	frame.pack();
    	frame.setVisible(true);
    
    		
    	}
    }
    Heres my panel:
    Code:
    public class CirclesPanel extends JPanel implements ActionListener{
    	private JButton draw, clear;
    	private JTextArea textArea;
    	private JPanel panel, drawPanel, buttonPanel;
    	private int count;
    	
    	public CirclesPanel(){
    		
    		JButton draw = new JButton("Draw");
    		JButton clear = new JButton("Clear");
    		JTextArea textArea = new JTextArea(1,10);
    		textArea.setBorder(BorderFactory.createTitledBorder("Circles"));
    		
    		
    		JPanel panel = new JPanel();
    		panel.setLayout(new BorderLayout());
    		setPreferredSize(new Dimension(620, 425));
    		
    		JPanel drawPanel = new JPanel();
    		drawPanel.setPreferredSize(new Dimension(450,400));
    		drawPanel.setBackground(Color.BLACK);
    		
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridLayout(3,1));
    		
    		add(panel);
    		add(buttonPanel, BorderLayout.WEST);
    		add(drawPanel, BorderLayout.EAST);
    		buttonPanel.add(textArea);
    		buttonPanel.add(draw);
    		buttonPanel.add(clear);
    		
    		draw.addActionListener(this);
    		clear.addActionListener(this);
    		
    	}
    	
    	
    	
    	public void actionPerformed(ActionEvent e) {
    
    		if(e.getSource() == draw){
    			String tempText = textArea.getText();
    			count = Integer.valueOf(tempText);
    			repaint();
    			
    
    			}
    		}
    	
    	public void paintComponent(Graphics g) {
    		Random generator = new Random();
    		int x, y, diameter;
    		for(int i = 0; i < count; i++){
    			x = generator.nextInt(90);
    			y = generator.nextInt(90);
    			diameter = generator.nextInt(30);
    			g.drawOval(x, y, diameter, diameter);
    			
    		
    	}
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    The import statements are missing. Code can't be compiled for testing.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    The import statements are missing. Code can't be compiled for testing.
    What are those?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What kind of IDE are you using that doesn't require import statements so the compiler can find classes that are in packages?

    Here are some samples:
    import java.awt.Color;
    import java.awt.image.BufferStrategy;
    import java.util.Scanner;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    What kind of IDE are you using that doesn't require import statements so the compiler can find classes that are in packages?

    Here are some samples:
    import java.awt.Color;
    import java.awt.image.BufferStrategy;
    import java.util.Scanner;
    Oh all my code is imported. I have
    Code:
     import java.awt.BorderLayout;
     import java.awt.Color;
     import java.awt.Dimension;
     import java.awt.FlowLayout;
     import java.awt.Graphics;
     import java.awt.GridLayout;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import java.util.Random;
     import javax.swing.BorderFactory;
     import javax.swing.JButton;
     import javax.swing.JPanel;
     import javax.swing.JTextArea;
    I just didnt add it because i thought people would know it was added
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Yes, import statements are required, but there are hundreds of possibilities and it helps if the code is posted with the imports it needs to save time and effort finding which ones are needed to do a compile.
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    There are a couple of problems I notice in your constructor. You're defining draw as a JButton and make it an instance variable. But then within the CirclesPanel constructor, you're redefining draw as a local variable. So in actionPerformed, the source will not be the instance variable called draw, so nothing is going to happen. You also have your other instance variables shadowed.
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Also I think there's a problem with the drawing. When you put the paintComponent method in CirclesPanel, the drawing surface that's used will the CirclesPanel and not drawPanel.

    One suggestion I would have it make DrawPanel a subclass of JPanel and override its paintComponent method with the code you have. Then in CirclesPanel, instead of making drawPanel a direct instance of a JPanel, you would make it an instance of DrawPanel.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    I see what you mean. How would I add my subpanel drawPanel to my main panel though?
  18. #10
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by choustonator
    I see what you mean. How would I add my subpanel drawPanel to my main panel though?
    You could create a seperate class called DrawPanel that extends JPanel. Then instead of creating an instance of JPanel, you would create an instance of DrawPanel.

IMN logo majestic logo threadwatch logo seochat tools logo