SubToolBox.java 8.14 KB
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * SubToolBox contains special features for certain differnet tools. Such as 
 * setting the line width, setting the radius of the spray, choosing a brush,
 * selecting whether you want to draw a normal, outline or filled shape.
 */
public class SubToolBox extends JPanel
{
	private JButton current;
	
	/** Creates a default SubToolBox. **/
	public SubToolBox()
	{
		// sets the default stuff and then starts off the choose panel method
		setSize(40,66);
		setLayout(null);
		setVisible(true);
		choosePanel();
	}
	
	/** Handles a switch in the toolbox panel with the corresponding tool. **/
	public void choosePanel()
	{
		// removes the current panel and adds the one corresponding to the selected tool.
		removeAll();
		int tbs = ToolBox.toolSelected;
		if(tbs == ToolBox.RECTANGLE || tbs == ToolBox.OVAL || tbs == ToolBox.ROUNDRECTANGLE)
			shapePanel();
		else if(tbs == ToolBox.LINE)
			linePanel();
		else if(tbs == ToolBox.SPRAY)
			sprayPanel();
		else if(tbs == ToolBox.BRUSH)
			brushPanel();
		else if(tbs == ToolBox.DRAG)
			dragPanel();
		else if(tbs == ToolBox.RANDOMDRAW)
			randomPanel();
		repaint();
	}
	
	/** Allows user to select settings for drawing random lines. **/
	public void randomPanel()
	{
		JTextField field = new JTextField();
		JButton submit = new JButton();
		field.setBounds(25,0,40,22);
		submit.setBounds(25,22,40,22);
		add(field);
		add(submit);
		submit.addActionListener(new JRandom(field));
	}
	
	/** Allows the user to set the thickness of a line. **/
	public void linePanel()
	{
		// creates a textbox and a button to submit the size of the line
		JTextField field = new JTextField();
		JButton submit = new JButton();
		field.setBounds(25,0,40,22);
		submit.setBounds(25,22,40,22);
		add(field);
		add(submit);
		submit.addActionListener(new JTextListener(field));
	}
	
	/** Allows the user to set the size of the spray. **/
	public void sprayPanel()
	{
		// creates a field to set the size of the spray
		JTextField field = new JTextField();
		JButton submit = new JButton();
		field.setBounds(25,0,40,22);
		submit.setBounds(25,22,40,22);
		add(field);
		add(submit);
		submit.addActionListener(new JSprayListener(field));
	}
	
	/** Allows the user to select the item to drag. **/	
	public void dragPanel()
	{
		JButton rect = new JButton(new ImageIcon("Images//box.gif"));
		JButton oval = new JButton(new ImageIcon("Images//oval.gif"));
		rect.setSelectedIcon(new ImageIcon("Images//boxPressed.gif"));
		oval.setSelectedIcon(new ImageIcon("Images//ovalPressed.gif"));
		rect.setBounds(25,0,40,22);
		oval.setBounds(25,22,40,22);
		add(rect);
		add(oval);
		rect.addActionListener(new RectListener(rect));
		oval.addActionListener(new OvalListener(oval));
		JButton line = new JButton(new ImageIcon("Images//line.gif"));
		line.setSelectedIcon(new ImageIcon("Images//linePressed.gif"));
		line.setBounds(25,44,40,22);
		add(line);
		line.addActionListener(new LineListener(line));
		setCurrent(line);
		DrawingCanvas.extraOperations = DrawingCanvas.LINE;
	}
	
	/** Allows the user to select a style of brush. **/
	public void brushPanel()
	{
		// loads up the images when its pressed and when its not and sets 
		// its location and adds it to the screen.
		JButton rect = new JButton(new ImageIcon("Images//box.gif"));
		JButton oval = new JButton(new ImageIcon("Images//oval.gif"));
		rect.setSelectedIcon(new ImageIcon("Images//boxPressed.gif"));
		oval.setSelectedIcon(new ImageIcon("Images//ovalPressed.gif"));
		rect.setBounds(25,0,40,22);
		oval.setBounds(25,22,40,22);
		add(rect);
		add(oval);
		rect.addActionListener(new RectListener(rect));
		oval.addActionListener(new OvalListener(oval));
		setCurrent(rect);
		DrawingCanvas.extraOperations = DrawingCanvas.RECT;
	}
	
	/** Creates a shapes panel and allows the user to choose the type. **/
	public void shapePanel()
	{
		// loads up the images when its pressed and when its not and sets
		// its location and adds it to the screen.
		JButton normal = new JButton(new ImageIcon("Images//normal.gif"));
		JButton fill = new JButton(new ImageIcon("Images//fill.gif"));
		JButton outline = new JButton(new ImageIcon("Images//outline.gif"));
		normal.setSelectedIcon(new ImageIcon("Images//normalPressed.gif"));
		fill.setSelectedIcon(new ImageIcon("Images//fillPressed.gif"));
		outline.setSelectedIcon(new ImageIcon("Images//outlinePressed.gif"));
		normal.setBounds(25,0,40,22);
		fill.setBounds(25,22,40,22);
		outline.setBounds(25,44,40,22);
		add(normal);
		add(fill);
		add(outline);
		normal.addActionListener(new NormalListener(normal));
		fill.addActionListener(new FillListener(fill));
		outline.addActionListener(new OutlineListener(outline));
		setCurrent(normal);
		DrawingCanvas.extraOperations = DrawingCanvas.NORMAL;
	}
	
	/** Sets the current special selection to the field. **/
	public void setCurrent(JButton b)
	{
		current = b;
		current.setSelected(true);
	}
	/** Rectangle Listener, if button is pressed change field. **/
	public class RectListener implements ActionListener
	{
		private JButton b;
		
		public RectListener(JButton b){this.b=b;}
		
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.RECT;
		}
	}
	/** Oval Listener, if button is pressed change field. **/
	public class OvalListener implements ActionListener
	{
		private JButton b;
		
		public OvalListener(JButton b){this.b=b;}
		
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.OVAL;
		}
	}
	/** Line Listener, if button is pressed change field. **/
	public class LineListener implements ActionListener
	{
		private JButton b;
		
		public LineListener(JButton b){this.b=b;}
		
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.LINE;
		}
	}
	/** Spray Listener, changes size of spray and brush. **/
	public class JSprayListener implements ActionListener
	{
		private JTextField f;
		public JSprayListener(JTextField f){this.f=f;}
		public void actionPerformed(ActionEvent e)
		{
			DrawingCanvas.radius = Math.abs(Integer.parseInt(f.getText()));
		}
	}
	/** Random Listener, changes amount of lines. **/
	public class JRandom implements ActionListener
	{
		private JTextField f;
		public JRandom (JTextField f){this.f=f;}
		public void actionPerformed(ActionEvent e)
		{
			DrawingCanvas.amtLines = Integer.parseInt(f.getText());
		}
	}
	/** Text Listener, changes size of line. **/
	public class JTextListener implements ActionListener
	{
		private JTextField f;
		public JTextListener(JTextField f){this.f=f;}
		public void actionPerformed(ActionEvent e)
		{
			DrawingCanvas.lineWidth = Integer.parseInt(f.getText());
		}
	}
	/** Normal Listener, extra operations is set to normal. **/
	public class NormalListener implements ActionListener
	{
		private JButton b;
		public NormalListener(JButton b){this.b=b;}
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.NORMAL;
		}
	}
	/** Fill Listener, extra operations is set to fill. **/
	public class FillListener implements ActionListener
	{
		private JButton b;
		
		public FillListener(JButton b){this.b=b;}
		
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.FILL;
		}
	}
	/** Outline Listener, extra operations is set to outline. **/
	public class OutlineListener implements ActionListener
	{
		private JButton b;
		
		public OutlineListener(JButton b){this.b=b;}
		
		public void actionPerformed(ActionEvent e)
		{
			current.setSelected(false);
			setCurrent(b);
			DrawingCanvas.extraOperations = DrawingCanvas.OUTLINE;
		}
	}
}