Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Adding graphic objects to ArrayList

$
0
0
Hello, I am trying to make a program that allows the user to click anywhere on a JPanel to create a "bubble". I want there to be multiple bubbles, in essence I want the number of bubbles the user can create to be infinite. So far, I can create a bubble, place it anywhere, and also change the color. The problem is, I can only create one bubble. I made an ArrayList and a addCircle method, but I am unsure how to implement it. Any help is appreciated!

The framework class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Soap extends JFrame implements ActionListener {
	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);
	private MakeBubbles bb = new MakeBubbles();
	
	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);
		add(bb, BorderLayout.CENTER);
		setSize.addActionListener(this);
		green.addActionListener(this);
		red.addActionListener(this);
		
	}
	public void actionPerformed(ActionEvent e){
		int num = Integer.parseInt(newR.getText());
		bb.setRadius(num);
		if (e.getSource() == red){
			bb.getGraphics();
			bb.setColor(Color.RED);
			
		}if (e.getSource() == green){
			bb.getGraphics();
			bb.setColor(Color.GREEN);
			
		}
	}
	
	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);
	}
	
}


The MakeBubbles class to handle the graphic objects
public class MakeBubbles extends JPanel implements MouseListener {
	private int X, Y;
	private int radius = 24;
	private Color c = Color.CYAN;
	public ArrayList<MakeBubbles> lBub = new ArrayList<MakeBubbles>();
	
	MakeBubbles(){
		addMouseListener(this);
	}
	
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(c);
		g2.fillOval(X,Y,radius,radius);
		//repaint();
	}
	public void addCircle(MakeBubbles newBubble){
		lBub.add(newBubble);
	}
	
	public void setRadius(int newR){
		this.radius = newR;
	}
	
	public void setColor(Color newC){
		this.c = newC;
	}
	public void setX(int newXC){
		this.X = newXC;
	}
	public void setY(int newYC){
		this.Y = newYC;
	}

	public void mouseClicked(MouseEvent e) {
		int X = e.getX();
		int Y = e.getY();
		setX(X);
		setY(Y);
		repaint();
	}
	public void mouseEntered(MouseEvent arg0) {
		
	}
	public void mouseExited(MouseEvent arg0) {
		
	}
	public void mousePressed(MouseEvent e) {
	
	}
	public void mouseReleased(MouseEvent arg0) {
		
	}
	
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>