Ryan Bury

RB Paint

RBPaint Picture

Download RBPaint Jar


Drawable Resource Files

These are some classes to define the different shapes used by the program. Some separation is achieved by placing these items in different files. It makes errors easier to source.

Shape.java Button.java Line.java Oval.java Rectangle.java

Main.java

A simple main class that starts the application. At one point this was supposed to be an Applet! However, Applets now are universally unsupported, so that didn't work.

import javax.swing.JApplet;
import javax.swing.JFrame;

public class Main /*extends JApplet*/{
	/*public void init()
	{
		this.setLayout(null);
		Canvas myCanvas = new Canvas();
		myCanvas.setBounds(0, 0, 800, 600);
		
		this.add(myCanvas);
		
		this.setVisible(true);
	}*/
	
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("RB - Paint");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0,0,790, 640);
		
		Canvas myCanvas = new Canvas();
		myCanvas.setBounds(0, 0, 770, 600);
		
		frame.add(myCanvas);
		frame.setVisible(true);
	}
}

Canvas.java

Here is where all the magic happens! All the events are handled here, including saving and loading.

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Canvas extends JPanel implements MouseListener, MouseMotionListener{
	
	private final int ButtonAreaX = 0, ButtonAreaY = 0, ButtonAreaWidth = 170, ButtonAreaHeight = 600;
	private final int DrawingAreaX = 170, DrawingAreaY = 0, DrawingAreaWidth = 600, DrawingAreaHeight = 600;
	
	Choice colorChoice;
	
	Shape activeShape;
	MouseEvent initialClick;
	ArrayList<Shape> shapes = new ArrayList<Shape>();
	ArrayList<Button> drawButtons = new ArrayList<Button>();
	ArrayList<Button> colorButtons = new ArrayList<Button>();
	Button ovalButton, rectangleButton, lineButton, brushButton, eraseButton, blueButton, purpleButton, redButton, orangeButton, yellowButton, greenButton, whiteButton, blackButton;
	Button clearButton, filledToggle, saveButton, loadButton;
	Oval cursor;
	JSlider sizeSlider;
	
	JFileChooser chooser;
	public Canvas()
	{
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.setSize(500,500);
		this.setLayout(null);
		ovalButton = new Button(10, 110, 150, 30, "Draw Oval");
		ovalButton.isPressed = true;
		rectangleButton = new Button(10, 150, 150, 30, "Draw Rectangle");
		lineButton = new Button(10, 190, 150, 30, "Draw Line");
		filledToggle = new Button(10, 230, 75, 30, "Filled");
		
		brushButton = new Button(10, 310, 150, 30, "Brush");
		eraseButton = new Button(10, 350, 150, 30, "Erase");
		sizeSlider = new JSlider(JSlider.HORIZONTAL, 2, 102, 2);
		sizeSlider.setBackground(new Color(200, 200, 200));
		sizeSlider.setFont(new Font("Garamond", Font.PLAIN, 12));
		sizeSlider.setBounds(10, 390, 150, 45);
		sizeSlider.setMajorTickSpacing(20);
		sizeSlider.setMinorTickSpacing(5);
		sizeSlider.setPaintTicks(true);
		sizeSlider.setPaintLabels(true);
		this.add(sizeSlider);
		
		whiteButton = new Button(10, 10, 30, 30, "White");
		whiteButton.isPressed = true;
		blackButton = new Button(50, 10, 30, 30, "Black");
		blueButton = new Button(90, 10, 30, 30, "Blue");
		purpleButton = new Button(130, 10, 30, 30, "Purple");
		redButton = new Button(10, 50, 30, 30, "Red");
		orangeButton = new Button(50, 50, 30, 30, "Orange");
		yellowButton = new Button(90, 50, 30, 30, "Yellow");
		greenButton = new Button(130, 50, 30, 30, "Green");
		
		clearButton = new Button(10, 505, 150, 30, "Clear");	
		saveButton = new Button(10, 560, 70, 30, "Save");
		loadButton = new Button(90, 560, 70, 30, "Load");
		
		drawButtons.add(ovalButton);
		drawButtons.add(rectangleButton);
		drawButtons.add(lineButton);
		drawButtons.add(brushButton);
		drawButtons.add(eraseButton);
		
		colorButtons.add(blueButton);
		colorButtons.add(purpleButton);
		colorButtons.add(redButton);
		colorButtons.add(orangeButton);
		colorButtons.add(yellowButton);
		colorButtons.add(greenButton);
		colorButtons.add(whiteButton);
		colorButtons.add(blackButton);
		repaint();
		
		cursor = new Oval(0, 0, 0, 0, new Color(100, 100, 100, 125));
		
		chooser = new JFileChooser();
		chooser.setFileFilter(new FileNameExtensionFilter("RB Paint (*.rbpa)", "rbpa"));
		chooser.addChoosableFileFilter(new FileNameExtensionFilter("RB Paint (*.rbpa)", "rbpa"));
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		g.setColor(new Color(180, 180, 180));
		g.fillRect(DrawingAreaX, DrawingAreaY, DrawingAreaWidth, DrawingAreaHeight);
			
				
		for(int i = 0; i < shapes.size(); i ++){ shapes.get(i).draw(g); }
		
		g.setColor(new Color(200, 200, 200));
		g.fillRect(ButtonAreaX, ButtonAreaY, ButtonAreaWidth, ButtonAreaHeight);
		g.setColor(new Color(180, 180, 180));
		g.fillRect(0, 545, ButtonAreaWidth, 5);	
		for(int i = 0; i < drawButtons.size(); i ++){ drawButtons.get(i).draw(g); }
		for(int i = 0; i < colorButtons.size(); i ++){ colorButtons.get(i).draw(g); }
		clearButton.draw(g);
		saveButton.draw(g);
		loadButton.draw(g);
		
		filledToggle.draw(g);
		
		if(brushButton.isPressed || eraseButton.isPressed)
		{
			cursor.draw(g);
		}
	}


	@Override
	public void mousePressed(MouseEvent e) 
	{
		
		if(inDrawingArea(e))
		{
			if(ovalButton.isPressed)
			{
				activeShape = new Oval(e.getX(), e.getY(), 0, 0, getColor());
				((Oval) activeShape).setFilled(filledToggle.isPressed);
			}
			if(rectangleButton.isPressed)
			{
				activeShape = new Rectangle(e.getX(), e.getY(), 0, 0, getColor());
				((Rectangle) activeShape).setFilled(filledToggle.isPressed);
			}
			if(lineButton.isPressed)
				activeShape = new Line(e.getX(), e.getY(), e.getX(), e.getY(), getColor());
			if(brushButton.isPressed)
			{
				activeShape = new Oval(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), getColor());
				((Oval) activeShape).setFilled(true);
			}
			if(eraseButton.isPressed)
			{
				activeShape = new Oval(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), new Color(180, 180, 180));
				((Oval) activeShape).setFilled(true);
			}
				
			shapes.add(activeShape);
			initialClick = e;
		}
		else if(inButtonArea(e))
		{
			if(clearButton.collides(e.getX(), e.getY()))
			{
				clearButton.isPressed = true;
			}
			if(saveButton.collides(e.getX(), e.getY()))
			{
				saveButton.isPressed = true;
			}
			if(loadButton.collides(e.getX(), e.getY()))
			{
				loadButton.isPressed = true;
			}
		}
		repaint();
		
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
		activeShape = null;
		if(inButtonArea(e))
		{
			if(clearButton.collides(e.getX(), e.getY()))
			{
				shapes.clear();
				clearButton.isPressed = false;
				repaint();
				return;
			}
			if(filledToggle.collides(e.getX(), e.getY()))
			{
				filledToggle.isPressed = !filledToggle.isPressed;
				repaint();
				return;
			}
			
			if(saveButton.collides(e.getX(), e.getY()))
			{
				int val = chooser.showSaveDialog(this);
				
				if(val == JFileChooser.APPROVE_OPTION)
				{
					try
					{
						File file = chooser.getSelectedFile();
						PrintWriter outputStream = new PrintWriter(new FileWriter(file));

						for(int i = 0; i < shapes.size(); i ++)
						{
							outputStream.println(shapes.get(i).toString());
						}

						if(outputStream != null)
							outputStream.close();
					}
					catch(Exception ex)
					{
						ex.printStackTrace();
					}
				}
				saveButton.isPressed = false;
				saveButton.isHovered = false;
			}
			
			if(loadButton.collides(e.getX(), e.getY()))
			{
				int val = chooser.showOpenDialog(this);
				
				if(val == JFileChooser.APPROVE_OPTION)
				{
					try 
					{
						File file = chooser.getSelectedFile();
						
						BufferedReader inputStream = new BufferedReader(new FileReader(file));
						
						String s;
						while((s = inputStream.readLine()) != null)
						{
							String[] str = s.split(" ", 2);
							if(str[0].equals("Line"))
							{
								shapes.add(new Line(str[1]));
							}
							if(str[0].equals("Oval"))
							{
								shapes.add(new Oval(str[1]));
							}
							if(str[0].equals("Rectangle"))
							{
								shapes.add(new Rectangle(str[1]));
							}
						}
						
						inputStream.close();
						
					}catch(Exception ex)
					{
						ex.printStackTrace();
					}
				}
				
				loadButton.isPressed = false;
				loadButton.isHovered = false;
			}
			
			for(int i = 0; i < drawButtons.size(); i ++)
			{
				if(drawButtons.get(i).collides(e.getX(), e.getY()))
				{
					unpressDrawButtons();
					drawButtons.get(i).isPressed = true;
					repaint();
					return;
				}
			}
			for(int i = 0; i < colorButtons.size(); i++)
			{
				if(colorButtons.get(i).collides(e.getX(), e.getY()))
				{
					unpressColorButtons();
					colorButtons.get(i).isPressed = true;
					repaint();
					return;
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) 
	{
		
	}

	@Override
	public void mouseExited(MouseEvent e) 
	{
		
	}

	@Override
	public void mouseDragged(MouseEvent e) 
	{
		cursor.reInit(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), new Color(100, 100, 100, 125));
		
		if(clearButton.isPressed && !clearButton.collides(e.getX(), e.getY())) clearButton.isPressed = false;
		if(saveButton.isPressed && !saveButton.collides(e.getX(), e.getY())) saveButton.isPressed = false;
		if(loadButton.isPressed && !loadButton.collides(e.getX(), e.getY())) loadButton.isPressed = false;
		
		if(inDrawingArea(e) && activeShape != null)
		{
			if(lineButton.isPressed)
			{
				activeShape.reInit(initialClick.getX(), initialClick.getY(), e.getX(), e.getY(), getColor());
				repaint();
				return;
			}
			if(brushButton.isPressed)
			{
				activeShape = new Oval(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), getColor());
				((Oval) activeShape).setFilled(true);
				shapes.add(activeShape);
				repaint();
				return;
			}
			if(eraseButton.isPressed)
			{
				activeShape = new Oval(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), new Color(180, 180, 180));
				((Oval) activeShape).setFilled(true);
				shapes.add(activeShape);
				repaint();
				return;
			}
			int X, Y, W, H;
			if(e.getX() > initialClick.getX())
			{
				X = initialClick.getX();
				W = e.getX() - initialClick.getX();
			}
			else
			{
				X = e.getX();
				W = initialClick.getX() - e.getX();
			}
		
			if(e.getY() > initialClick.getY())
			{
				Y = initialClick.getY();
				H = e.getY() - initialClick.getY();
			}
			else
			{
				Y = e.getY();
				H = initialClick.getY() - e.getY();
			}
			activeShape.reInit(X, Y, W, H, getColor());
		}
		else if(inButtonArea(e))
		{
			hoverCheck(e.getX(), e.getY());
		}
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) 
	{
		cursor.reInit(e.getX() - sizeSlider.getValue()/2, e.getY() - sizeSlider.getValue()/2, sizeSlider.getValue(), sizeSlider.getValue(), new Color(100, 100, 100, 125));
		
		if(inButtonArea(e))
		{
			hoverCheck(e.getX(), e.getY());
		}
		repaint();
	}
	
	public boolean inDrawingArea(MouseEvent e)
	{
		return e.getX() >= DrawingAreaX && e.getX() <= DrawingAreaWidth + DrawingAreaX && e.getY() >= DrawingAreaY && e.getY() <= DrawingAreaHeight + DrawingAreaY;
	}
	public boolean inButtonArea(MouseEvent e)
	{
		return e.getX() >= ButtonAreaX && e.getX() <= ButtonAreaWidth + ButtonAreaX && e.getY() >= ButtonAreaY && e.getY() <= ButtonAreaHeight + ButtonAreaY;
	}
	public void unpressDrawButtons()
	{
		for(int i = 0; i < drawButtons.size(); i ++)
		{
			drawButtons.get(i).isPressed = false;
		}
	}
	public void unpressColorButtons()
	{
		for(int i = 0; i < colorButtons.size(); i++)
		{
			colorButtons.get(i).isPressed = false;
		}
	}
	public Color getColor()
	{		
		for(int i = 0; i < colorButtons.size(); i ++)
		{
			if(colorButtons.get(i).isPressed)
			{
				return colorButtons.get(i).getColor();
			}
		}
		
		return null;
	}

	public void hoverCheck(int x, int y)
	{
		if(clearButton.collides(x, y))
		{
			clearButton.isHovered = true;
		}
		else
		{
			clearButton.isHovered = false;
		}
		
		if(filledToggle.collides(x, y))
		{
			filledToggle.isHovered = true;
		}
		else
		{
			filledToggle.isHovered = false;
		}
		
		if(saveButton.collides(x, y))
		{
			saveButton.isHovered = true;
		}
		else
		{
			saveButton.isHovered = false;
		}
		
		if(loadButton.collides(x, y))
		{
			loadButton.isHovered = true;
		}
		else
		{
			loadButton.isHovered = false;
		}
		
		for(int i = 0; i < drawButtons.size(); i ++)
		{
			if(drawButtons.get(i).collides(x, y))
			{
				drawButtons.get(i).isHovered = true;
			}
			else
			{
				drawButtons.get(i).isHovered = false;
			}
		}
		for(int i = 0; i < colorButtons.size(); i ++)
		{
			if(colorButtons.get(i).collides(x, y))
			{
				colorButtons.get(i).isHovered = true;
			}
			else
			{
				colorButtons.get(i).isHovered = false;
			}
		}	
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}