Javadoc: PaintApp Class
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- java.awt.Window
-
- java.awt.Frame
-
- PaintApp
-
- All Implemented Interfaces:
- java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.WindowListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible
public class PaintApp extends java.awt.Frame implements java.awt.event.WindowListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener
This driver class controls the paint program's frame and graphical output. By implementing the WindowListener, MouseListener, and MouseMotionListener interfaces, the PaintApp class also manages how the mouse interacts with the application.
Submitted to AP Computer Science 12: 2015-01-27
Last modified: 2015-04-04
Author: Samantha Yu
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow, java.awt.Window.Type
-
-
Field Summary
Fields Modifier and Type Field and Description private int
appHeight
The paint program's dimensions that are used when creating the frame of the paint program.private int
appWidth
The paint program's dimensions that are used when creating the frame of the paint program.private int
canvasHeight
The paint program's dimensions that are used when creating the frame of the paint program.private java.awt.Rectangle
canvasRect
Canvas is not visible but is the click-able region of the window that does not include the menu bar.private boolean
continuePaint
Variables used as "switches" during program execution.private boolean
firstPaint
Variables used as "switches" during program execution.private java.awt.Graphics2D
g2Frame
g2Frame is a Graphics g typecasted to Graphics2D.private Layers
layersArray
A Layers object that controls the creation and deletion of layers, and what happens when the user wants to move to a different layer.private Menu
menuBar
The click-able and visible menu bar that allows the user to decide on the color, size, tool and layer.private boolean
menuChange
Variables used as "switches" during program execution.private int
menuHeight
Since the canvas does not contain the menu, Tools' useTool() subtracts the menuHeight from its y-coordinates.private int
newX
The coordinates from mouse events like Mouse_Released are stored in these variables.private int
newY
The coordinates from mouse events like Mouse_Released are stored in these variables.private int
oldX
The coordinates from mouse events like Mouse_Released are stored in these variables.private int
oldY
The coordinates from mouse events like Mouse_Released are stored in these variables.private static long
serialVersionUID
A generated serialVersionUID that ensures proper version control.private Tools
toolbar
A toolbar object that controls the style of the paint tool.-
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
-
-
Constructor Summary
Constructors Constructor and Description PaintApp()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
createApp()
The menu bar, canvas, layers array, and toolbar are created.private void
createCanvas()
This method creates the canvas, which is the entire application excluding the menu bar, that the user can draw on.int
getAppWidth()
int
getCanvasHeight()
int
getMenuHeight()
static void
main(java.lang.String[] args)
The main() handles the application framework by adding a window listener, mouse listener, and mouse motion listener, and making the application visible.void
mouseClicked(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener.void
mouseDragged(java.awt.event.MouseEvent e)
When a mouse button is held down and is dragged, mouseDragged() gets the current coordinates.void
mouseEntered(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener.void
mouseExited(java.awt.event.MouseEvent e)
void
mouseMoved(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener.void
mousePressed(java.awt.event.MouseEvent e)
When any of the mouse buttons are pressed down, mousePressed() gets the current coordinates.void
mouseReleased(java.awt.event.MouseEvent e)
When any of the buttons are released, mouseReleased() gets the current coordinates.void
paint(java.awt.Graphics g)
The paint() is called after paintProgram became visible and whenever the graphical output needs to be updated or repainted During its first call, paint() draws the menu onto the window from the menu's off-screen image.void
paintLayers(java.awt.Graphics2D g2Frame)
The paint() calls the paintLayers() to update the canvas' graphics.void
update(java.awt.Graphics g)
When repaint() is called, the update() then calls paint().void
windowActivated(java.awt.event.WindowEvent e)
void
windowClosed(java.awt.event.WindowEvent e)
void
windowClosing(java.awt.event.WindowEvent e)
When the window is closed, the Frame is destroyed and the Java virtual machine is terminated.void
windowDeactivated(java.awt.event.WindowEvent e)
void
windowDeiconified(java.awt.event.WindowEvent e)
void
windowIconified(java.awt.event.WindowEvent e)
void
windowOpened(java.awt.event.WindowEvent e)
These window events are required to be implemented, because this class implements the WindowListener.-
Methods inherited from class java.awt.Frame
addNotify, getAccessibleContext, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, paramString, remove, removeNotify, setBackground, setCursor, setExtendedState, setIconImage, setMaximizedBounds, setMenuBar, setOpacity, setResizable, setShape, setState, setTitle, setUndecorated
-
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, postEvent, processEvent, processWindowEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setType, setVisible, show, toBack, toFront
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
A generated serialVersionUID that ensures proper version control.
-
appWidth
private int appWidth
The paint program's dimensions that are used when creating the frame of the paint program.
-
appHeight
private int appHeight
The paint program's dimensions that are used when creating the frame of the paint program.
-
canvasHeight
private int canvasHeight
The paint program's dimensions that are used when creating the frame of the paint program.
-
menuBar
private Menu menuBar
The click-able and visible menu bar that allows the user to decide on the color, size, tool and layer.
-
toolbar
private Tools toolbar
A toolbar object that controls the style of the paint tool.
-
menuHeight
private int menuHeight
Since the canvas does not contain the menu, Tools' useTool() subtracts the menuHeight from its y-coordinates.
-
canvasRect
private java.awt.Rectangle canvasRect
Canvas is not visible but is the click-able region of the window that does not include the menu bar.
-
firstPaint
private boolean firstPaint
Variables used as "switches" during program execution.
-
menuChange
private boolean menuChange
Variables used as "switches" during program execution.
-
continuePaint
private boolean continuePaint
Variables used as "switches" during program execution.
-
oldX
private int oldX
The coordinates from mouse events like Mouse_Released are stored in these variables.
-
oldY
private int oldY
The coordinates from mouse events like Mouse_Released are stored in these variables.
-
newX
private int newX
The coordinates from mouse events like Mouse_Released are stored in these variables.
-
newY
private int newY
The coordinates from mouse events like Mouse_Released are stored in these variables.
-
g2Frame
private java.awt.Graphics2D g2Frame
g2Frame is a Graphics g typecasted to Graphics2D. g2Frame thus controls the graphics of the entire window. For example, g2Frame paints the current buffered image stored in imageArray at cLayerNum onto the window.
-
layersArray
private Layers layersArray
A Layers object that controls the creation and deletion of layers, and what happens when the user wants to move to a different layer.
-
-
Method Detail
-
main
public static void main(java.lang.String[] args)
The main() handles the application framework by adding a window listener, mouse listener, and mouse motion listener, and making the application visible.- Parameters:
args
- The command line arguments are not used in this paint program so it is unnecessary to declare them.
-
createApp
public void createApp()
The menu bar, canvas, layers array, and toolbar are created. The menu is "click-able" and can handle mouse events, but is not visible yet until paint() is called.
-
paint
public void paint(java.awt.Graphics g)
The paint() is called after paintProgram became visible and whenever the graphical output needs to be updated or repainted During its first call, paint() draws the menu onto the window from the menu's off-screen image.After the first call, if a user's mouse did not click inside the menu, the mouse paints with the user's selected tool, size, and color. But if a menu item is selected, then the area underneath the menu is not painted. Regardless of whether a menu item was selected, paintLayers() is called and the canvas is updated. When the user selects a large size for their tool, then the canvas will not be accidentally painted.
If the user has selected a menu item, the menu bar is updated and redrawn. In every paint() call, the menu bar is redrawn to prevent it from disappearing when the window has gone inactive or resized.
- Overrides:
paint
in classjava.awt.Window
-
paintLayers
public void paintLayers(java.awt.Graphics2D g2Frame)
The paint() calls the paintLayers() to update the canvas' graphics. If a layer is deleted, the deleted blank layer is first drawn to erase the deleted layer's graphics from the window. The rest of the layers are then drawn in ascending order and the deleted blank layer is removed from imageArray. Regardless of whether the user decided to delete a layer, the current layer is drawn last for visibility. If the user is not deleting a layer, the other layers are not redrawn to prevent these layers from flickering. Also, the layer status is updated when a layer is created, deleted, or switched to a different layer.- Parameters:
g2Frame
- Graphics g typecasted to Graphics2D
-
windowClosing
public void windowClosing(java.awt.event.WindowEvent e)
When the window is closed, the Frame is destroyed and the Java virtual machine is terminated.- Specified by:
windowClosing
in interfacejava.awt.event.WindowListener
-
windowOpened
public void windowOpened(java.awt.event.WindowEvent e)
These window events are required to be implemented, because this class implements the WindowListener. However, this paint program does not need these window events so these methods are left blank.- Specified by:
windowOpened
in interfacejava.awt.event.WindowListener
-
windowIconified
public void windowIconified(java.awt.event.WindowEvent e)
- Specified by:
windowIconified
in interfacejava.awt.event.WindowListener
-
windowClosed
public void windowClosed(java.awt.event.WindowEvent e)
- Specified by:
windowClosed
in interfacejava.awt.event.WindowListener
-
windowDeiconified
public void windowDeiconified(java.awt.event.WindowEvent e)
- Specified by:
windowDeiconified
in interfacejava.awt.event.WindowListener
-
windowActivated
public void windowActivated(java.awt.event.WindowEvent e)
- Specified by:
windowActivated
in interfacejava.awt.event.WindowListener
-
windowDeactivated
public void windowDeactivated(java.awt.event.WindowEvent e)
- Specified by:
windowDeactivated
in interfacejava.awt.event.WindowListener
-
createCanvas
private void createCanvas()
This method creates the canvas, which is the entire application excluding the menu bar, that the user can draw on.
-
getAppWidth
public int getAppWidth()
-
getCanvasHeight
public int getCanvasHeight()
-
getMenuHeight
public int getMenuHeight()
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener. However, this paint program does not need these mouse events so these methods are left blank. Unlike the mousePressed() which only occurs when the mouse is pressed, the mouseClicked() requires the user to press and release the mouse button.- Specified by:
mouseClicked
in interfacejava.awt.event.MouseListener
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener. However, this paint program does not need these mouse events so these methods are left blank.- Specified by:
mouseEntered
in interfacejava.awt.event.MouseListener
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)
- Specified by:
mouseExited
in interfacejava.awt.event.MouseListener
-
mouseMoved
public void mouseMoved(java.awt.event.MouseEvent e)
These mouse events are required to be implemented, because this class implements the MouseListener and MouseMotionListener. However, this paint program does not need these mouse events so these methods are left blank. Unlike the mouseDragged(), mouseMoved() is for when the mouse is moving but none of the buttons are held down.- Specified by:
mouseMoved
in interfacejava.awt.event.MouseMotionListener
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)
When any of the mouse buttons are pressed down, mousePressed() gets the current coordinates. mousePressed() repaints the application if the user has selected a menu item or clicked inside the canvas. mouseClicked() is called when the mouse presses down then releases.- Specified by:
mousePressed
in interfacejava.awt.event.MouseListener
-
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)
When a mouse button is held down and is dragged, mouseDragged() gets the current coordinates. mouseDragged() repaints the application if the mouse was dragged inside the canvas. mouseMoved() is called when the mouse is moving but none of the buttons are held down.- Specified by:
mouseDragged
in interfacejava.awt.event.MouseMotionListener
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)
When any of the buttons are released, mouseReleased() gets the current coordinates. mouseReleased() repaints the application if the mouse was released inside the canvas.- Specified by:
mouseReleased
in interfacejava.awt.event.MouseListener
-
update
public void update(java.awt.Graphics g)
When repaint() is called, the update() then calls paint(). Since "default implementation of update() clears the Component's background before calling paint()" (see http://journals.ecs.soton.ac.uk/java/tutorial/ui/drawing/update.html), I prevented the background layers from flickering by drawing the layers here.- Overrides:
update
in classjava.awt.Container
-
-