0

Got a small detail problem that I cannot get to understand myself.
The program, see attachments draws dots on the screen.

There is a dot class for the details of the dots to be stored, that is size, colour, shape (square or round)

Problem is with the colour...Cannot get other dots to have different colours...

Attachments
/**
 * @author Hindowah
 */


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

public class DoodleBoardFrame extends JFrame 
{
	public DoodleBoardFrame()
	{
		super();
		
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image img = kit.getImage("Lewis.jpg");
		this.setIconImage(img);		
		this.setResizable(true);
		this.setLocation(500,100);
		this.setVisible(true);
		this.setSize(650,650);
		
		this.setTitle("DoodleBoard");		
		
		Dpanel = new DoodleBoardPanel();
		this.add(Dpanel);		
	
		menuBar = new JMenuBar();
		menuBar.setVisible(true);
		//menuBar.setLocation(0,0);
		
		menuOptions = new JMenu("Options");
		menuOptions.setMnemonic('O');
		menuOptions.setToolTipText("Drawing tools");
		menuFile = new JMenu("File");
	   	menuFile.setMnemonic('F');
		menuHelp = new JMenu("Help");
	   	menuHelp.setMnemonic('H');

		menuBar.add(menuFile);
		menuBar.add(menuOptions);
		menuBar.add(menuHelp);
		this.setJMenuBar(menuBar);		
		
// now create and add the menu items...
		itemNew = new JMenuItem("New");
        itemNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
		itemNew.setEnabled(false);			// Disabled
		itemNew.setMnemonic('N');
		itemExit = new JMenuItem("Exit");
		itemExit.setToolTipText("Quit program");
		itemExit.setMnemonic('E'); // ADD shorcut key ???
		itemOpen = new JMenuItem("Open");  // Disabled
        itemOpen.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
		itemOpen.setEnabled(false);
		itemOpen.setMnemonic('O');
		//itemOpen.setAccelerator(KeyStroke.getKeyStroke("Ctrl O")); ????
		itemSave = new JMenuItem("Save");  // Disabled
        itemSave.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
		itemSave.setEnabled(false);
		itemSave.setMnemonic('S');
		itemSaveAs = new JMenuItem("Save As"); // Disabled
		itemSaveAs.setEnabled(false);
		menuFile.add(itemNew);
		menuFile.add(itemOpen);
		menuFile.addSeparator();
		menuFile.add(itemSave);
		menuFile.add(itemSaveAs);
		menuFile.addSeparator();
		menuFile.add(itemExit);
		
		itemPenColor = new JMenuItem("Pen Colour");
		itemPenColor.setMnemonic('C');
		itemPenSize = new JMenu("Pen Size");
		itemPenSize.setMnemonic('S');
		itemPenShape = new JMenu("Pen Shape"); 
		itemPenShape.setMnemonic('h');
		itemBGcolor = new JMenuItem("Background Colour");
		itemBGcolor.setToolTipText("You do not have to change the background colour to start drawing");
		itemBGcolor.setMnemonic('B');
		menuOptions.add(itemPenColor);
		
		// radio buttons for PENSIZE and PENSHAPE
		small = new JRadioButtonMenuItem("Small",true);  // "true" sets the default pen size to small
		medium = new JRadioButtonMenuItem("Medium");
		large = new JRadioButtonMenuItem("Large");
        ButtonGroup group1 = new ButtonGroup();		
		group1.add(small);
		group1.add(medium);
		group1.add(large);
		itemPenSize.add(small);
		itemPenSize.add(medium);
		itemPenSize.add(large);
		menuOptions.add(itemPenSize);

		round = new JRadioButtonMenuItem("Round",true);  // "true" sets the default pen shape to round
		square = new JRadioButtonMenuItem("Square");
        ButtonGroup group2 = new ButtonGroup();		
		group2.add(round);
		group2.add(square);
		itemPenShape.add(round);
		itemPenShape.add(square);
		menuOptions.add(itemPenShape);

		menuOptions.addSeparator();
		menuOptions.add(itemBGcolor);
		
		itemDrawHelp = new JMenuItem("Draw Help");
		//itemDrawHelp.setEnabled(false);
		itemAbout = new JMenuItem("About");
		itemAbout.setEnabled(false);   
		itemAbout.setMnemonic('A');
		menuHelp.add(itemAbout);
		menuHelp.add(itemDrawHelp);
		
		// ADD listeners...
		itemExit.addActionListener(new itemExitHandler()); 
		itemBGcolor.addActionListener(new itemBGcolorHandler());
		itemPenColor.addActionListener(new itemPenColorHandler());
		itemDrawHelp.addActionListener(new itemDrawHelpHandler());
		small.addActionListener(new smallHandler());
		medium.addActionListener(new smallHandler());
		large.addActionListener(new smallHandler());
		round.addActionListener(new roundHandler());
		square.addActionListener(new roundHandler());
		
		// create a ToolBAR
		
	     toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
	     eraseBUTTON = new JButton(new ImageIcon("eraser.jpg"));
	     eraseBUTTON.setToolTipText(" Erase ");
	     toolbar.add(eraseBUTTON);
	     toolbar.addSeparator();
	     saveBUTTON = new JButton(new ImageIcon("save.jpg"));
	     saveBUTTON.setToolTipText(" Save ");
	     toolbar.add(saveBUTTON);
	     toolbar.addSeparator();
	     openBUTTON = new JButton(new ImageIcon("open.jpg"));
	     openBUTTON.setToolTipText(" Open ");
	     toolbar.add(openBUTTON);
	     toolbar.addSeparator();
	     clearBUTTON = new JButton(new ImageIcon("clear.jpg"));
	     clearBUTTON.setToolTipText(" Clear all ");
	     toolbar.add(clearBUTTON);
	     
	     this.getContentPane().add(toolbar,BorderLayout.NORTH);	     
	}
	
	private DoodleBoardPanel Dpanel;	
	private JMenuBar menuBar;
	
	private JToolBar toolbar;
	private JButton eraseBUTTON;
	private JButton saveBUTTON;
	private JButton openBUTTON;
	private JButton clearBUTTON;
	
	private JMenu menuOptions;
	private JMenu menuFile;
	private JMenu menuHelp;
	
	private JMenuItem itemNew;
	private JMenuItem itemExit;
	private JMenuItem itemOpen;
	private JMenuItem itemSave;
	private JMenuItem itemSaveAs;
	private JMenuItem itemAbout;
	private JMenuItem itemDrawHelp;
	private JMenuItem itemPenColor;
	private JMenu itemPenSize;
	private JMenu itemPenShape;
	private JMenuItem itemBGcolor;
	
	private JRadioButtonMenuItem small;
	private JRadioButtonMenuItem medium;
	private JRadioButtonMenuItem large;	
	private JRadioButtonMenuItem round;
	private JRadioButtonMenuItem square;


	
	
	private class itemExitHandler implements ActionListener
	{
		public itemExitHandler()
		{}
		
		public void actionPerformed(ActionEvent e)
		{
			// make sure it stays within frame boundaries
			int returnval=JOptionPane.NO_OPTION;
			returnval=JOptionPane.showConfirmDialog(DoodleBoardFrame.this,"Are you ready to leave DoodleBoard application?","Close Application",JOptionPane.YES_NO_OPTION);
			if(returnval==JOptionPane.YES_OPTION)
				System.exit(0);
			//System.exit(0);
			
		}
	}
	
	private class itemBGcolorHandler implements ActionListener
	{
		public itemBGcolorHandler()
		{}
		
		public void actionPerformed(ActionEvent e)
		{
			Color defaultColour = ((DoodleBoardPanel) Dpanel).getBGColor();
			Color selected = JColorChooser.showDialog(itemBGcolor, "Set Background Colour", defaultColour);
			if(selected != null) ((DoodleBoardPanel) Dpanel).setBGColor(selected);
		}
	}
		
	private class itemPenColorHandler implements ActionListener
	{
		public itemPenColorHandler()
		{}
		public void actionPerformed(ActionEvent e)
		{
			Color defaultColour = ((DoodleBoardPanel)Dpanel).getAdotColor();
			Color selected = JColorChooser.showDialog(itemPenColor, "Choose Pen Colour", defaultColour);
			if(selected != null) ((DoodleBoardPanel)Dpanel).setAdotColor(selected); 
			// repaint(); // needed???
			// sets (updates) the current DOT COLOR after user selects color
		}
	}
	
	private class itemDrawHelpHandler implements ActionListener
	{
		public itemDrawHelpHandler()
		{}
		public void actionPerformed(ActionEvent e)
		{
			// make sure it stays within the frame????
			JOptionPane.showMessageDialog(DoodleBoardFrame.this, "Trust me, you do NOT need help with this program." +
					" It is very simple.\nClick(left) and hold mouse down, then drag slowly to get desired shape.", "Draw Help", JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	Integer size1;
	int size;int x; int y;
	String aShape = " ";
	//Shape aShape = null;
	private class smallHandler implements ActionListener
	{
		public smallHandler()
		{}
		public void actionPerformed(ActionEvent e)
		{
			 // default size is "small" ; sets the CURRENT dot size according to what radio button is chosen
			String str = e.getActionCommand();
		
			if(str.equalsIgnoreCase("small"))				
			{
				size1 = 3;
			}		
			else if(str.equalsIgnoreCase("medium"))			
			{
				size1 = 12;
			}
			else if(str.equalsIgnoreCase("large"))			
			{
				size1 = 24;
			}
			
			((DoodleBoardPanel)Dpanel).setAdotSize(size1);  //updates the DOT SIZE
		}
	}
	
	private class roundHandler implements ActionListener
	{
		public roundHandler()
		{}
		public void actionPerformed(ActionEvent e)
		{
			 // default shape is "round" ; sets the CURRENT dot shape according to what radio button is chosen
			String str=e.getActionCommand();
			
			if(str.equalsIgnoreCase("round"))				
			{
				aShape = "round"; 
				//aShape = new Ellipse2D.Double(x,y,size1 ,size1);
			}		
			else if(str.equalsIgnoreCase("square"))			
			{
				aShape = "square";
				 //aShape = new Rectangle2D.Double(x,y,size1 ,size1);
			}
			
			((DoodleBoardPanel)Dpanel).setAdotShape(aShape); // updates the DOT SHAPE
		}
	}


}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;


import java.awt.*; 
//import java.awt.Color;
//import java.awt.event.ActionListener;
//import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.awt.Shape;

import javax.swing.JPanel;

public class DoodleBoardPanel extends JPanel implements MouseMotionListener
{
	public DoodleBoardPanel()
	{
	    super();
	    this.setVisible(true);
	    Color def = new Color(204,204,204);
	    this.setBackground(def);
	    
	    this.addMouseMotionListener(this);
        addMouseMotionListener(this);
        
		DOTlist = new ArrayList<Shape>();
		
		//set the initial properties...
		Adotcolor = Color.black; Adotsize = 3; Adotshape = "round";		
		aDot = new Dot(Adotcolor,Adotsize,Adotshape);
	}
	
	private ArrayList<Shape> DOTlist;
	private Dot aDot;
	private Integer Adotsize;
	private Color Adotcolor;
	private String Adotshape;
	//private Shape Adotshape;
	
	/**
	 * Mouse event for drawing shapes, lines, etc...
	 * @return returns a dot object each time
	 * @param gets the X coordinate of the mouse o the screen
	 * @param gets the Y coordinate of the mouse on the screen
	 */
	public void mouseDragged(MouseEvent e)
    {
		// get the x and y coordinates where mouse is dragged
		int x = e.getX();
		int y = e.getY();
		
		//System.out.println("you are at "  + "(" + x + "," + y + ")");
		

		//Dot aDot = new Dot(Color.black,3,"round");
		//DOTlist.add(aDot);
		//repaint();  
		
		
		String ashape = getAdotShape();
		
		if (ashape.equalsIgnoreCase("round"))
		{
			Ellipse2D.Double Acircle = new Ellipse2D.Double(x,y,getAdotSize(),getAdotSize());
			/*g2.setColor(getAdotColor());   
			g2.setPaint(getAdotColor());
			g2.fill(Acircle);
			g2.draw(Acircle);	*/		
			DOTlist.add(Acircle);
			repaint();
		}
		else if (ashape.equalsIgnoreCase("square"))  
		{
			Rectangle2D.Double Asquare = new Rectangle2D.Double(x,y,getAdotSize(),getAdotSize());
			// Asquare.setColor(getAdotColor());   
			//g2.setPaint(getAdotColor());
			//g2.fill(Asquare);
			//g2.draw(Asquare);	
			DOTlist.add(Asquare);
			repaint();
		} 	
		//else if (ashape.equalsIgnoreCase(null))
		//{
			//Ellipse2D.Double Acircle = new Ellipse2D.Double(x,y,3,3);
			//DOTlist.add(Acircle);
			//repaint();
		//}
		//repaint();
		
    }
	
    public void mouseMoved(MouseEvent e)
    {this.setToolTipText("Welcome!!!  PRESS and DRAG mouse to start drawing. HAVE FUN!!!");}
	
	public Color getBGColor()
	{
		return this.getBackground();
	}
	
	public void setBGColor(Color chosen)
	{
		this.setBackground(chosen);
	}
	
	// methods used to SET dot properties
	
	public void setAdotSize(Integer Adotsize)
	{	
		aDot.setDotSize(Adotsize);		
	}
	
	public void setAdotShape(String Adotshape)
	{	
		aDot.setDotShape(Adotshape);		
	}
	
	public void setAdotColor(Color Adotcolor)
	{	
		aDot.setDotColor(Adotcolor);		
	}
	
	// methods to GET the current dot properties
	
	public String getAdotShape()
	{
		return aDot.getDotShape();
	}
	
	public Color getAdotColor()
	{
		return aDot.getDotColor();
	}
	
	public Integer getAdotSize()
	{
		return aDot.getDotSize();
	}
	
	
	// paint the dots on the panel
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
	//make sure the current color is set on call	
		
		for (Shape dot:DOTlist)
		{
			g2.fill(dot);
			g2.draw(dot);
			g2.setColor(getAdotColor());   
			g2.setPaint(getAdotColor());	
		}
		
	}

}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class DoodleBoardTester
{
	public static void main(String[] args)
	{
		DoodleBoardFrame gframe = new DoodleBoardFrame();
		gframe.setVisible(true);
		// Add a listener for the frame close event
		gframe.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent evt) 
        {
            // Exit the application
            System.exit(0);
        }});
	}

}
import java.awt.Color;
//import java.awt.Shape;
//import java.awt.Graphics2D;
//import java.awt.geom.Ellipse2D;
//import java.awt.geom.Rectangle2D;
//
//import javax.swing.JColorChooser;


/**
 * The constructor for all the dots that would be drawn on the screen
 * @param the Colour(Color) of the dot
 * @param the Size(Integer) of the dot
 * @param the Shape(String) of the dot
 */
public class Dot 
{
	public Dot(Color color,Integer size, String shape)
	{
	   dotColor = color;
		dotShape = shape;
		dotSize = size;		
	}
	
	private Color dotColor;
	private String dotShape;
	private Integer dotSize;
	
	// the get OR accessor methods...	
	
	/**
	 * Asks for the shape of the dot
	 * @return returns shape of the dot
	 */
	public String getDotShape()
	{
		return this.dotShape;
	}	
	
	/**
	 * Asks for the colour of the dot
	 * @return returns colour of the dot
	 */
	public Color getDotColor()
	{
		return this.dotColor;
	}	
	
	/**
	 * Asks for the size of the dot
	 * @return returns size of the dot
	 */
	public Integer getDotSize()
	{
		return this.dotSize;
	}
	
	
	//the set OR mutator methods...
	
	/**
	 * Sets the shape of the dot
	 * @param shape(string) of the dot
	 */
	public void setDotShape(String s)
	{
		this.dotShape = s;
	}	
	
	/**
	 * Sets the colour of the dot
	 * @param colour(color) of the dot
	 */
	public void setDotColor(Color c) 
	{
		this.dotColor = c;
	}	
	
	/**
	 * Sets the size of the dot
	 * @param size(integer) of the dot
	 */
	public void setDotSize(Integer Size)
	{
		this.dotSize = Size;		
	}	
}
2
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by momoh
0

In File DoodleBoardPanel.java, see method public void mouseDragged(MouseEvent e) {...} .

Here you are storing only the size and shape in ArrayList DOTlist . Hence, each dot is having different size and shape. However, you are not storing the color of each dot anywhere.

Now in the same file see the following method

public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		for (Shape dot:DOTlist)
		{
			g2.fill(dot);
			g2.draw(dot);
			g2.setColor(getAdotColor());   
			g2.setPaint(getAdotColor());	
		}
		
	}

Here you are setting the color depending on getAdotColor() which returns aDot.setDotColor(Adotcolor); Here aDot is a single object for all the dots, since you are not creating different Dot Objects for each dot created by the user.


Suggestion
--------------
In method public void mouseDragged(MouseEvent e) {...} , Create Dot Objects for each dot created by the user and add them in ArrayList DOTlist .

You might also have to modify your public void paintComponent(Graphics g) a bit.

Hope this was useful.

0

I GET WHAT YOU MEAN NOW...made much sense when I read it second time and with no haste (was at work the first time I read it, don't know was just excited to see what I was missing)

Thanx DUDE

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.