6 Replies - 508 Views - Last Post: 06 January 2013 - 11:37 AM Rate Topic: -----

#1 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Multiple Circle objects on JPanel

Posted 06 January 2013 - 10:18 AM

Okay so I am creating a program where the user can click anywhere on a JPanel and a circle will appear. I can run the program with no errors however no circles are appearing when I click on the JPanel. I believe my paint components and draw methods are a little messy.

Main class:
public class Soap extends JFrame  {
	private JButton setSize = new JButton("SET RADIUS");
	private JButton green = new JButton("GREEN");
	private JButton red = new JButton("RED");
	private JLabel info = new JLabel("New Radius: ");
	private JTextField newR = new JTextField(8);
	public ArrayList<MakeBubbles> bub = new ArrayList<MakeBubbles>();
	private JPanel canvas = new JPanel();
	
	Soap(){
		setLayout(new BorderLayout());
		JPanel main = new JPanel(new FlowLayout(FlowLayout.CENTER));
		main.add(green);
		main.add(red);
		main.add(info);
		main.add(newR);
		main.add(setSize);
		add(main, BorderLayout.SOUTH);
		canvas.setBackground(Color.WHITE);
		add(canvas, BorderLayout.CENTER);
		canvas.addMouseListener(clicker);
	}
	
	protected void paintComponent(Graphics g){
		super.paintComponents(g);
		for (MakeBubbles mb : bub){
			mb.draw(g);
		}
	}
	
	MouseListener clicker = new MouseAdapter(){
		public void mouseClicked(MouseEvent e){
			MakeBubbles b = new MakeBubbles((int)e.getX(), (int)e.getY(), 79, Color.CYAN);
			bub.add(B)/>;
			repaint();
		}
	};
	
	public static void main(String[] args){
		Soap s1 = new Soap();
		s1.setVisible(true);
		s1.setSize(500,300);
		s1.setResizable(true);
		s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}


Bubble Class:

public class MakeBubbles  {
	int x = 0, y = 0, radius = 30;
	Color c = Color.CYAN;
	
	public MakeBubbles(int x, int y, int radius, Color c){
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.c = c;
	}
	public int getX(){
		return this.x;
	}
	public int getY(){
		return this.y;
	}
	
	public void setRadius(int newR){
		this.radius = newR;
	}
	public int getRadius(){
		return this.radius;
	}
	public Color getColor(){
		return this.c;
	}
	public void draw(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(c);
		g2.fillOval(x,y,radius,radius);
	}
}


Is This A Good Question/Topic? 1
  • +

Replies To: Multiple Circle objects on JPanel

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 10:51 AM

You're almost there. Your Bubble class is fine. You are overwriting the wrong paint method there.
paintComponent should be overridden the JPanel, I presume the one you are sticking in the middle of that jFrame.

When I create an application like this, my JPanel handles painting through paintComponent(), as well as the KeyListener. My JFrame is a separate class and it only has one job - to hold my JPanel.
Was This Post Helpful? 0
  • +
  • -

#3 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 10:59 AM

Ahh I think I know what you are saying.. should I create a seperate class that extends JPanel and have that as my "canvas" class?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 11:03 AM

Yes.

JFrame - one job is to hold canvas sunblass of JPanel.

Canvas subclass of JPanel implements KeyListener and you paint in paintComponent(). This is where all your app logic is, your arraylist<Bubble> etc.

Bubble class - You are good. :)
Was This Post Helpful? 2
  • +
  • -

#5 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 11:26 AM

Thanks man! got it working.. now I just need to create actionListeners for the JButtons. Two questions though.. 1) when I first create a bubble, a copy of my set radius button appears in the top left. and 2) its not letting me change the background color to White when i do setBackground(Color.WHITE) in the canvas constructor. Thanks so much for your help!
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 11:32 AM

Just handle it in your Jpanel's paintComponent().

public void paintComponent(Graphics g) {
    //super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(0,0,getWidth(),getHeight());



calling super.paintComponent(g) does the same thing as the code I posted, so you might as well do it there.
Was This Post Helpful? 0
  • +
  • -

#7 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Multiple Circle objects on JPanel

Posted 06 January 2013 - 11:37 AM

wow.. you're awesome! thanks for the help :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1