Paint OOP version 1

This assignment has the same specs as the Chapter13 lab assignment. You need to demonstrate your knowledge of OOP design. In Lab 13 you had to change multiple methods (init, paint, mouseClicked). Each new color and each new tool made the code more and more difficult to navigate. In this Lab we will write a Class for each thing in the Pallette. In the Starter File you will see that each color selecting "button" is a different instance of the ColorButton Class. Each Tool is an instance of its own class.

Write an applet that demonstrates your ability to take advantage of the code already written.

As an example here is one that has a lot of features.

PaintOOP1.java

// PaintOOP1.java
// The Paint Brush Program
// 

// This paint program has terrible OOP Design--but more on that later
// We hope that you can learn that as you add tools, the complexity
// will grow, and you will appretiate good OOP Design later.
// 
// Right now there are only 2 colors and 2 draw tools to get you started, 
// Leon Schram's Starter uses many depricated methods to this 
// is my version which will compile without deprecations warnings.
// 
// In Chapter 21 will will learn more about Interfaces, but the 
// Current way to get graphic input from an Applet is by Implementing
// the MouseMotionListener and the MouseListener. 
// Basically, they are all set up for us, and we can override events
// When we want to with our implemention

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*; 

public class PaintOOP1 extends Applet implements MouseMotionListener, MouseListener 
{
    Rectangle paintArea,  clear;
    ColorButton red, blue, white;
    PencilTool pencil;
    BrushTool brush;
    LineEffectTool lineEffect;
    
    Color currentColor;
    boolean firstClick, lineEffectOn;
    public int drawTool, tempDrawTool,xClick, yClick, startX, startY, 
          width, height, cc;
    
    public void init()
    {
        cc = 0;
        drawTool = 1;
        tempDrawTool = 1;
        xClick = 97;
        yClick = 0;
        startX = 105;
        startY = 10;
        
        currentColor = Color.red;
        firstClick = true;
        width = getWidth();
        height = getHeight();
      
        
        paintArea = new Rectangle(115,15,869,569);
        // Pallette Button Rectengles
        red = new ColorButton(5,5,42,42, Color.red);
        blue = new ColorButton(53,5,42,42, Color.blue);
        white = new ColorButton(5,53,42,42, Color.white, "Erase");
        
        clear = new Rectangle(4,340,92,55);
        pencil = new PencilTool(5,400,42,42, "pencil");
        brush = new BrushTool(53,400,42,42, "brush");
        lineEffect = new LineEffectTool(5,448,42,42);
        
        // Finally, we alter the init method to add our MouseListener and 
        // MouseMotionListener to this Applet:
        addMouseMotionListener(this); // new
        addMouseListener(this); //new
    }
    
    public void paint(Graphics g)
    {
        if (cc <= 1)// Single click
        {
            g.setColor(Color.white);
            g.fillRect(0,0,width,height);
        }    
    
        //Draw Color and Tool Pallette
        red.drawButton(g);
        blue.drawButton(g);  
        white.drawButton(g); 
        pencil.drawButton(g, currentColor);
        brush.drawButton(g, currentColor);
        lineEffect.drawButton(g, currentColor);
        
        g.drawString("Clear Screen",13,363);                
        g.drawString("(Double-Click)",9,383);               
        g.drawRect(clear.x, clear.y, clear.width, clear.height);  
       
        // Now draw if in Paint area
        g.setColor(currentColor);
        switch (drawTool)
        {
            case 1 : //pencil
                     if (paintArea.contains(xClick,yClick))
                        pencil.draw(g,xClick,yClick);
                     break;
                    
            case 2 : //brush
                     if (paintArea.contains(xClick,yClick))
                        brush.draw(g, xClick,yClick);                        
                     break;
            
                     
            case 5 :  //clearScreen
                     g.setColor(Color.white);
                     g.fillRect(paintArea.x-15,paintArea.y-5, paintArea.width+30, paintArea.height+10 );
                     g.setColor(currentColor);
                     drawTool = tempDrawTool;
                     
                     break;
                     
            case 9 : //LineEffectTool
                     if (lineEffectOn && paintArea.contains(xClick,yClick))
                        lineEffect.draw(g, startX,startY,xClick,yClick);
                     break;  
                      
           
                     
                                                                           
        }   
    }

    public void drawPixel(Graphics g, int x, int y)
    {
        g.drawLine(x,y,x,y);
    }
   

/*
 * New Mouse methods
 *
 * Since we are implementing the MouseListener and MouseMotionListener with our Applet
 * There are some required methods we need to decalre here, even
 * if there is nothing to do if that mouse event occurs:
 */

//  MOUSE MOTION LISTENER STUFF:
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}

// Now we put the code in the last required method
// that used to be in the old mouseDown method 
// in our new mouseClicked method:
// The textbook like the Rectangle's inside() method
// which has been depricated... simply uses the current
// contains() method instead

    public void mouseClicked(MouseEvent e) 
    {
      int x = e.getX();//new
      int y = e.getY();//new
      
        
      cc++;  // click count
            
        if(red.contains(x,y))
        {
            currentColor = red.getColor();
        }   
       
        else if(blue.contains(x,y))
        {
            currentColor = blue.getColor();    
        }  
        
        else if(white.contains(x,y))
        {   
            currentColor = white.getColor();
        }   
       
        else if(pencil.contains(x,y))
        {
            drawTool = 1;   
            lineEffectOn = false;
        }           
        else if(brush.contains(x,y))
        {
            drawTool = 2;
            lineEffectOn = false;
        }           
       
        else if(clear.contains(x,y) && e.getClickCount() == 2)//double clicked on the clear button
        {
            if (drawTool != 5)
                tempDrawTool = drawTool;
            drawTool = 5;   
            lineEffectOn = false;
        }           
        
        else if(lineEffect.contains(x,y))
        {
            drawTool = 9;                   
            lineEffectOn = true;
            
        }   
        else if(paintArea.contains(x,y) && lineEffectOn)
        {
            startX = x;
            startY = y;
        }   
        
        xClick = x;
        yClick = y; 
        repaint();
    }
    
//  MOUSE MOTION LISTENER STUFF:
// this method needed of all MouseMotionListeners,
// even if we don't do anything:
    public void mouseMoved(MouseEvent e) {}  
                                            
// Here we put the put the code that used to be in the mouseDrag method:
    public void mouseDragged(MouseEvent e) 
    {
        int x = e.getX();//new
        int y = e.getY();//new
        cc+=2;  // click count  
        xClick = x;
        yClick = y; 
        repaint();
        
        
    }
   
/*
 * End of New Mouse Methods
 */
    public void update(Graphics g)
    {
        paint(g);
    }

}
        
        
    


    

ColorButton.java


/**
 *  class ColorButton.
 * 
 */
import java.awt.*;
public class ColorButton
{
	// instance variables 
	private Rectangle btn;
	private Color c;
	private String lab;

	/**
	 * Constructor for objects of class ColorButton
	 */
	public ColorButton( int x, int y, int width, int length, Color c)
	{
	   btn = new Rectangle (x, y, width, length);
	   this.c=c;
       lab="";
	   
	}
	public ColorButton( int x, int y, int width, int length, Color c, String lab)
	{
	   btn = new Rectangle (x, y, width, length);
	   this.c=c;
       this.lab=lab;
	   
	}

	
	public void drawButton(Graphics g)
	{
		g.setColor(c);       
        g.fillRect(btn.x, btn.y, btn.width, btn.height);
        g.setColor(Color.black);
        g.drawRect(btn.x, btn.y, btn.width, btn.height); 
        g.drawString(lab,btn.x+5, btn.y+25);
	}
	
	public boolean contains(int x, int y)
	{
	    return btn.contains(x,y);
	}
	
	
	public Color getColor()
	{
	    return c;
	}
}

PencilTool.java


/**
 *  class PencilTool.
 * 
 */
import java.awt.*;
public class PencilTool
{
	// instance variables - 
	private Rectangle btn;
	private String lab;

	/**
	 * Constructor for objects of class 
	 */
	public PencilTool( int x, int y, int width, int length)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       lab="";
	   
	}
	public PencilTool( int x, int y, int width, int length,  String lab)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       this.lab=lab;
	   
	}

	
	public void drawButton(Graphics g, Color currentColor)
	{
		g.setColor(Color.white);//erase the old color version
	    g.fillRect(btn.x+20,btn.y+20,2,2);
	    
	    if (!currentColor.equals(Color.white)) 
        {
            g.setColor(currentColor);
            g.fillRect(btn.x+20,btn.y+20,2,2);
        } else {
            g.setColor(Color.black);
            g.drawRect(btn.x+20,btn.y+20,2,2);
        }
	   
	    //Next draw outline and the label
	    g.setColor(Color.black);
        g.drawRect(btn.x, btn.y, btn.width, btn.height); 
        g.drawString(lab,btn.x+5, btn.y+35);
	}
	
	public boolean contains(int x, int y)
	{
	    return btn.contains(x,y);
	}
	public void draw(Graphics g, int x, int y)
	{
	    g.fillRect(x-1,y-1,3,3);
	}
}

BrushTool.java


/**
 *  class BrushTool.
 * 
 */
import java.awt.*;
public class BrushTool
{
	// instance variables 
	private Rectangle btn;
	private String lab;

	/**
	 * Constructor for objects of class 
	 */
	public BrushTool( int x, int y, int width, int length)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       lab="";
	   
	}
	public BrushTool( int x, int y, int width, int length,  String lab)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       this.lab=lab;
	   
	}

	
	public void drawButton(Graphics g, Color currentColor)
	{
		g.setColor(Color.white);//erase the old color version 
	    g.fillRect(btn.x+17,btn.y+14,8,8);
	    
	    if (!currentColor.equals(Color.white)) 
        {
            g.setColor(currentColor);
            g.fillRect(btn.x+17,btn.y+14,8,8);
        } else {
            g.setColor(Color.black);
            g.drawRect(btn.x+17,btn.y+14,8,8);
        }
	   
	    //Next draw outline and the label
	    g.setColor(Color.black);
        g.drawRect(btn.x, btn.y, btn.width, btn.height); 
        g.drawString(lab,btn.x+5, btn.y+35);
	}
	
	public boolean contains(int x, int y)
	{
	    return btn.contains(x,y);
	}
	public void draw(Graphics g, int x, int y)
	{
	    g.fillRect(x-4,y-4,9,9);
	}
}

LineEffectTool.java


/**
 *  class LineEffectTool.
 * 
 */
import java.awt.*;
public class LineEffectTool
{
	// instance variables 
	private Rectangle btn;
	private String lab;

	/**
	 * Constructor for objects of class 
	 */
	public LineEffectTool( int x, int y, int width, int length)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       lab="";
	   
	}
	public LineEffectTool( int x, int y, int width, int length,  String lab)
	{
	   btn = new Rectangle (x, y, width, length);
	   
       this.lab=lab;
	   
	}

	
	public void drawButton(Graphics g, Color currentColor)
	{
		g.setColor(Color.white);//erase the old color version 
	    g.fillRect(btn.x,btn.y,btn.width,btn.height);
	    
	    if (!currentColor.equals(Color.white)) 
        {
            g.setColor(currentColor);
            
        } else {
            g.setColor(Color.black);
            
        }
	    for (double rad = 0.5*Math.PI; rad <= 1.5*Math.PI; rad += 0.2)
        {
            int x =  btn.x+17 + (int) Math.round(Math.cos(rad) * 10);
            int y = btn.y+24 + (int) Math.round(Math.sin(rad) * 15);
            g.drawLine(btn.x+37,btn.y,x,y);
        }  
	    //Next draw outline and the label
	    g.setColor(Color.black);
        g.drawRect(btn.x, btn.y, btn.width, btn.height); 
        g.drawString(lab,btn.x+5, btn.y+35);
	}
	
	public boolean contains(int x, int y)
	{
	    return btn.contains(x,y);
	}
	public void draw(Graphics g, int x0, int y0, int x1, int y1)
	{
	    g.drawLine(x0, y0, x1, y1);
	}
}

Required Output

It requires the following capabilities:

9-Point and 10 Point Versions

The program allows the selection of three different paint thicknesses. You could call these draw tools "pen", "brush", and "roller".

Implement one or more of the features like the ones listed below. You will more for adding each one of these features for a total possible score of 11 points.

11-Point Version

The Pencil Tool, the Brush Tool and many Tool Classes you have written have a lot in common. There is a lot of code that really only needs to be written once. Make a super class called Tool with the code that seems to be in common to all these Tool Classes. Next, adapt the PencilTool, BrushTool, etc, classes so they are subclasses of the Tool Class and inherit and/or overload the methods of the Tool super class.