Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

InheritanceExercise


DrawingPanel.java

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A class that stores a list of objects to draw on itself
 * Use the following keys:
 *     Arrow keys    move the currently selected item (highlighted yellow)
 *     n             toggle to the next item
 *     t             move the selected item to the top
 *     b             move the selected item to the bottom
 * 
 * @author Chuck Cusack
 * @version 1.0, September, 2006
 */
public class DrawingPanel extends JPanel implements KeyListener, MouseListener  {
	private ArrayList<Object> theObjects;    // The list of GraphicalObjects on the panel
	private Object selectedObject;   // The item on the panel that is currently selected.

	/**
	 * Constructor for objects of class DrawingPanel
	 */
	public DrawingPanel()
	{
		theObjects=new ArrayList<Object>();
		selectedObject=null;
		
		setFocusable(true);
        addMouseListener(this);
        addKeyListener(this);
		repaint();
	}
	
	/**
	 * Method overridden so that the JPanel canvas will be painted
	 * how we want it.
	 * For now, we just paint the various objects onto it.
	 */
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    for(int i=0;i<theObjects.size();i++) {
	    	   Object currentObject = theObjects.get(i);
	    	   if(currentObject instanceof Oval) {
		           Oval oval = (Oval) currentObject;
		           if(oval==selectedObject) { // highlight if selected
		               Color oldColor=g.getColor();
		               g.setColor(Color.yellow);
		               int x=oval.getX()-2;
		               int y=oval.getY()-2;
		               int width=oval.getWidth()+4;
		               int height=oval.getHeight()+4;
		               g.fillRect(x,y,width,height);  
		               // Reset the color back to the original.
		               g.setColor(oldColor);
		           }
		           oval.drawObject(g);
	    	   } else if(currentObject instanceof Rectangle) {
	    		   Rectangle rect = (Rectangle) currentObject;
		           if(rect==selectedObject) { // highlight if selected
		               Color oldColor=g.getColor();
		               g.setColor(Color.yellow);
		               int x=rect.getX()-2;
		               int y=rect.getY()-2;
		               int width=rect.getWidth()+4;
		               int height=rect.getHeight()+4;
		               g.fillRect(x,y,width,height);  
		               // Reset the color back to the original.
		               g.setColor(oldColor);
		           }
	    		   rect.drawObject(g);
	    	   } else if(currentObject instanceof BasicString) {
	    		   BasicString string = (BasicString) currentObject;
		           if(string==selectedObject) { // highlight if selected
		               Color oldColor=g.getColor();
		               g.setColor(Color.yellow);
		               int x=string.getX()-2;
		               int y=string.getY()-2;
		               int width=10;  // BasicStrings do not have width and height
		               int height=10; // I think 10 is about right....
		               g.fillRect(x,y,width,height);  
		               // Reset the color back to the original.
		               g.setColor(oldColor);
		           }
	    		   string.drawObject(g);
	    	   }
	    }
	}

    /**
     * Add the given object to the panel.
     * 
     * @param go The object to add to the panel.
     */
	public void addObject(Object go) {
	        theObjects.add(go);
	        repaint();
	}
	
   /**
     * Remove the given object from the panel.
     * 
     * @param go The object to remove from the panel.
     * @returns true if the object was on the list and 
     *          it was removed, and false otherwise.
     */
	public boolean removeObject(Object go) {
	        if(theObjects.remove(go)) {
	             repaint();
	             return true;
	        } else {
	             return false;
	        }
	}
	
	/** 
	 * @returns the GraphicalObject that is currently selected
	 */
	public Object getSelectedObject() {
	    return selectedObject;
	}
	/**
	 * Sets the selected item to be the given object, if that
	 * object is on the list.  Otherwise, does nothing.
	 * @param object the item to make selected
	 * @returns true if object is on the list, and false otherwise
	 */
	public boolean setSelectedObject(Object object) {
	    if(theObjects.contains(object)) {
	        selectedObject=object;
	        repaint();
	        return true;
	    } else {
	           return false;
	    }
	   }
   /**
	 * Sets the selected item to be the object with the
	 * given index, if the index is within the proper range.
	 * 
	 * @param index the index of the item to set as selected.
	 * @returns true if object is on the list, and false otherwise
	 */
	public boolean setSelectedObject(int index) {
	    if(index<0 || index >= theObjects.size()) {
	        return false;
	    } else {
	        selectedObject=theObjects.get(index);
	        repaint();
	        return true;
	    }
	}
	
	/**
	 * @returns how many objects are on the panel
	 */
	public int getNumberObjects() {
	    return theObjects.size();
	}
	   
    /*
     * The following are from the KeyListener interface
     * 
     */
    public void keyPressed(KeyEvent e) {
        // This is implemented here and not in keyTyped because keyTyped
        // does not listen to non-printing keys (like arrow keys)
        // Very irritating, and this was tough to figure out, which is
        // why I am documenting it.
        if(selectedObject!=null) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                	if(selectedObject instanceof Oval) {
     		           Oval oval = (Oval) selectedObject;
                       oval.setY(oval.getY()-10);
     	    	   } else if(selectedObject instanceof Rectangle) {
     	    		   Rectangle rect = (Rectangle) selectedObject;
                       rect.setY(rect.getY()-10);
     	    	   } else if(selectedObject instanceof BasicString) {
     	    		   BasicString string = (BasicString) selectedObject;
                       string.setY(string.getY()-10);
     	    	   }
                    break;
                case KeyEvent.VK_DOWN:
                	if(selectedObject instanceof Oval) {
      		           Oval oval = (Oval) selectedObject;
                        oval.setY(oval.getY()+10);
      	    	   } else if(selectedObject instanceof Rectangle) {
      	    		   Rectangle rect = (Rectangle) selectedObject;
                        rect.setY(rect.getY()+10);
      	    	   } else if(selectedObject instanceof BasicString) {
      	    		   BasicString string = (BasicString) selectedObject;
                        string.setY(string.getY()+10);
      	    	   }
                    break;
                case KeyEvent.VK_RIGHT:
                	if(selectedObject instanceof Oval) {
       		           Oval oval = (Oval) selectedObject;
                         oval.setX(oval.getX()+10);
       	    	   } else if(selectedObject instanceof Rectangle) {
       	    		   Rectangle rect = (Rectangle) selectedObject;
                         rect.setX(rect.getX()+10);
       	    	   } else if(selectedObject instanceof BasicString) {
       	    		   BasicString string = (BasicString) selectedObject;
                         string.setX(string.getX()+10);
       	    	   }
                    break;
                case KeyEvent.VK_LEFT:
                	if(selectedObject instanceof Oval) {
      		           Oval oval = (Oval) selectedObject;
                        oval.setX(oval.getX()-10);
      	    	   } else if(selectedObject instanceof Rectangle) {
      	    		   Rectangle rect = (Rectangle) selectedObject;
                        rect.setX(rect.getX()-10);
      	    	   } else if(selectedObject instanceof BasicString) {
      	    		   BasicString string = (BasicString) selectedObject;
                        string.setX(string.getX()-10);
      	    	   }
                    break;
                default:
            }
            repaint();
        }
    }
    public void keyTyped(KeyEvent e) {
        if(selectedObject!=null) {
            switch (e.getKeyChar()) { 
                case 't':
                    theObjects.remove(selectedObject);
                    theObjects.add(selectedObject);
                    break;
                case 'b':
                    theObjects.remove(selectedObject);
                    theObjects.add(0,selectedObject);
                    break;
                case 'n':
                    int index=theObjects.indexOf(selectedObject);
                    index=(index+1)%theObjects.size();
                    selectedObject = theObjects.get(index);
                    break;
                default:
            }
            repaint();
        }
    }
    public void keyReleased(KeyEvent e) {
    }
	
    /*
     * Methods from the MouseListener interface
     * We don't need them all, but we have to implement them all
     * anyway.
     */
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) { 
        // Need to do this so the panel gets focus so it can 
        // handle key events.  Without this, it doesn't get key events.
        requestFocusInWindow();
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    } 
}