Title: Mouse Interfaces : Drawing Shapes
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Mouse Interfaces: Drawing Shapes

- We are now going to go back to an example where we drew shapes (rectangles and ovals) on our screen based on user inputs.
Polymorphism Examples: Drawing Program
- We want to change this program to use the MouseListener and MouseMotionLister interfaces to work with the mouse. We did this in:
MouseInterfaces: Spots Painting
- Once we have put this all together, we will have a program that contains a collections of classes all derived from an abstract Shape class. Our program will also use the MouseListener and MouseMotionListener interfaces. So this one program will illustrate Abstract classes and Interfaces.
- This video is about the process of converting the starting program to the final version that accomplishes our goal. Down below, I have published our Final answer.

Shape.java


package drawing;

import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {
    private int x, y;
    private Color color;
    public abstract void draw(Graphics g);
    public abstract void subsequentPoint(int x, int y);

    public void setColor(Graphics g)
    {
           g.setColor(color);
    }

    public int getX()
    {
        return x;
    }
    public int getY()
    {
        return y;
    }

    public Shape(int x, int y, Color color)
    {
        this.x = x;
        this.y =y;
        this.color = color;
    }



}
class Rectangle extends Shape
{
    private int second_x, second_y;

    public Rectangle(int x, int y, Color color)
    {
        super(x,y,color);
        second_x = x;
        second_y =y;
    }
    public  void subsequentPoint(int x, int y)
    {
        second_x =x;
        second_y = y;
    }

    public void draw(Graphics g)
    {
        setColor(g);
        int width = Math.abs(second_x - getX());
        int height = Math.abs(second_y - getY());
        int x = Math.min(getX(), second_x);
        int y = Math.min(getY(), second_y);
        g.drawRect(x, y, width, height);
    }
}

class Oval extends Shape
{
    private int second_x, second_y;

    public Oval(int x, int y, Color color)
    {
        super(x,y,color);
        second_x = x;
        second_y =y;
    }

    public  void subsequentPoint(int x, int y)
    {
        second_x =x;
        second_y = y;
    }
    public void draw(Graphics g)
    {
         setColor(g);
         int width = Math.abs(second_x - getX());
         int height = Math.abs(second_y - getY());
         int x = Math.min(getX(), second_x);
         int y = Math.min(getY(), second_y);
         g.drawOval(x, y, width, height);
    }


}




MyDrawing.java


package drawing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JFrame;

enum DrawType {Rectangle, Oval};

public class MyDrawing extends JFrame implements MouseListener, MouseMotionListener{


    ArrayList<Shape> shapeArr = new ArrayList<Shape>();


    Color color = Color.red;
    DrawType drawType = DrawType.Rectangle;
    Shape inProgress = null;

    public void setDrawType(DrawType dt)
    {
        drawType =dt;
    }

    @Override
    public void mousePressed(MouseEvent evt) {
        int x= evt.getX();
        int y = evt.getY();
        switch(drawType)
        {
        case Rectangle:
            inProgress = new Rectangle(x,y, color);
            break;
        case Oval:
            inProgress = new Oval(x,y, color);
            break;
        }

    }
    @Override
    public void mouseDragged(MouseEvent evt) {

        int x = evt.getX();
        int y = evt.getY();
        if (inProgress != null)
        {
            inProgress.subsequentPoint(x, y);
            repaint();
        }
    }
    @Override
    public void mouseReleased(MouseEvent evt) {
        int x = evt.getX();
        int y = evt.getY();
        if (inProgress != null)
        {
            inProgress.subsequentPoint(x, y);
            addShape(inProgress);
            inProgress = null;
        }
    }

    public void addShape(Shape s)
    {
        //shapeArr[count++] = s;
        shapeArr.add(s);
        repaint();
    }
    MyDrawing()
    {
        super("My First GUI Example");
        setSize(800, 700);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    public void paint(Graphics g)
    {
        //super.paint(g);
        Dimension d =getSize();
        g.setColor(Color.white);
        g.fillRect(0, 0, d.width, d.height);

        for (int i=0; i < shapeArr.size(); i++)
        {
            //shapeArr[i].draw(g);
            Shape s = shapeArr.get(i);
            s.draw(g);
        }
        if (inProgress != null)
            inProgress.draw(g);
    }
    public static void main(String[] args) {
        MyDrawing md = new MyDrawing();

        Scanner keyboard = new Scanner(System.in);

        while (true)
        {
            System.out.println("Enter object to draw [r/o] ");
            String type = keyboard.next();
            if ("r".equals(type))
                md.setDrawType(DrawType.Rectangle);
            else if ("o".equals(type))
                md.setDrawType(DrawType.Oval);


        }


    }
    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }



    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


}



Last Updated: November 14, 2013 0:32 AM