I am trying to write a program that kind of resembles and etch-a-sketch. I have the basis for it. I am having two problems:

1. When I draw a shape in the default black color, when I switch the color it changes the color of what I had previously drawn. I would like what I drew before to stay that color and the new color to be only on the new shape that I draw.

2. I am also having trouble with my radio buttons. They work, however, when I draw a filled shaped and then draw an unfilled shape, it changes the previous to unfilled. I would like the previous to stay as is, and only change the new shape.

Any suggestions?
Thanks.

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

public class DrawPanel extends JFrame implements ActionListener {
	Container content;

	Color color = Color.BLACK;
	JButton colorButton = new JButton("Color Chooser");

	LinkedList<Shape> shapeList = new LinkedList<Shape>();
	Shape shape;
	Point start, end;
	final String[] type = new String[] { "Line", "Rectangle", "Oval" };
	JComboBox comboBox = new JComboBox(type);

	JRadioButton fillRadio = new JRadioButton("Fill w/ color");
	JRadioButton noFillRadio = new JRadioButton("Do Not Fill w/ Color");
	ButtonGroup group = new ButtonGroup();

	boolean filledOrNot = false;

	public DrawPanel() {
		super("DrawPanel");

		colorButton.addActionListener(this);
		fillRadio.addActionListener(this);
		noFillRadio.addActionListener(this);
		add(fillRadio, BorderLayout.CENTER);
		add(noFillRadio, BorderLayout.EAST);
		group.add(fillRadio);
		group.add(noFillRadio);

		JPanel panel = new JPanel() {
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2 = (Graphics2D) g;
				g2.setColor(Color.white);
				g2.fillRect(0, 0, getWidth(), getHeight());
				g2.setColor(color);
				for (Shape s : shapeList){
					g2.draw(s);
				}

				g2.draw(shape);
				if(filledOrNot){
					g2.setColor(color);
					g2.fill(shape);
				}
			}

		};

		panel.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				start = e.getPoint();
			}

			public void mouseReleased(MouseEvent e) {
				shapeList.add(shape);
			}
		});

		panel.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {
				end = e.getPoint();
				Object select = comboBox.getSelectedItem();
				if (select.equals(type[0]))
					shape = new Line2D.Float(start, end);
				else {
					if (select.equals(type[1]))
						shape = new Rectangle();
					else
						shape = new Ellipse2D.Float();
					((RectangularShape) shape).setFrameFromDiagonal(start, end);
				}
				repaint();
			}
		});

		panel.setPreferredSize(new Dimension(320, 440));
		add(panel, BorderLayout.NORTH);
		shape = new Rectangle();

		add(comboBox, BorderLayout.SOUTH);
		add(colorButton, BorderLayout.WEST);

		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);

	}

	public void changeColor() {
		color = JColorChooser.showDialog(this, "Choose Background Color",
				Color.BLACK);
	}

	public void actionPerformed(ActionEvent e) {
   if (e.getSource() == colorButton) {
           this.changeColor();
   } else if (e.getSource() == fillRadio){
           if (fillRadio.isSelected())
                   filledOrNot = true;
   } else if (e.getSource() == noFillRadio){
           if(noFillRadio.isSelected())
                   filledOrNot = false;
   }

	}

	public static void main(String[] args) {
		new DrawPanel();
	}
}

You could make your own PaintShape class that contains a Shape reference and those two properties. You can pass all of the info through the constructor and access it as you loop through your shapes in the paintComponent() method.

Member Avatar for hfx642

Keep track of your colours for EACH "shape" being drawn.

Yes, that was the intention for the small custom class I mentioned above, in case I wasn't very clear.

class PaintShape{
   Shape shape;
   Color color;
... // etc
}

and a list of those

LinkedList<PaintShape> shapeList = new LinkedList<PaintShape>();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.