├── .gitattributes ├── .gitignore ├── DrawFrame.java ├── DrawPanel.java ├── LinkedList.java ├── ListNode.java ├── MyBoundedShape.java ├── MyLine.java ├── MyOval.java ├── MyRectangle.java ├── MyShape.java ├── README.md └── TestDraw.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /DrawFrame.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | import javax.swing.JPanel; 3 | import javax.swing.JLabel; 4 | import javax.swing.JButton; 5 | import javax.swing.JComboBox; 6 | import javax.swing.JCheckBox; 7 | import java.awt.event.ItemListener; 8 | import java.awt.event.ItemEvent; 9 | import java.awt.event.ActionListener; 10 | import java.awt.event.ActionEvent; 11 | import java.awt.BorderLayout; 12 | import java.awt.GridLayout; 13 | import java.awt.FlowLayout; 14 | import java.awt.Color; 15 | 16 | /** 17 | * Provides the GUI and encapsulates the DrawPanel 18 | * It creates 3 buttons undo, redo and clear. 19 | * It creates 2 combobox colors and shapes. 20 | * It creates 1 checkbox filled to select whether shape is filled or not. 21 | * Has two private inner classes 22 | * One for handling button events 23 | * Another for handling both combo box events and checkbox events 24 | */ 25 | public class DrawFrame extends JFrame 26 | { 27 | private JLabel stausLabel; //label display mouse coordinates 28 | private DrawPanel panel; //draw panel for the shapes 29 | 30 | private JButton undo; // button to undo last drawn shape 31 | private JButton redo; // button to redo an undo 32 | private JButton clear; // button to clear panel 33 | 34 | private JComboBox colors; //combobox with color options 35 | 36 | //array of strings containing color options for JComboBox colors 37 | private String colorOptions[]= 38 | {"Black","Blue","Cyan","Dark Gray","Gray","Green","Light Gray", 39 | "Magenta","Orange","Pink","Red","White","Yellow"}; 40 | 41 | //aray of Color objects contating Color constants 42 | private Color colorArray[]= 43 | {Color.BLACK , Color.BLUE , Color.CYAN , Color.darkGray , Color.GRAY , 44 | Color.GREEN, Color.lightGray , Color.MAGENTA , Color.ORANGE , 45 | Color.PINK , Color.RED , Color.WHITE , Color.YELLOW}; 46 | 47 | private JComboBox shapes; //combobox with shape options 48 | 49 | //array of strings containing shape options for JComboBox shapes 50 | private String shapeOptions[]= 51 | {"Line","Rectangle","Oval"}; 52 | 53 | private JCheckBox filled; //checkbox to select whether shape is filled or not 54 | 55 | private JPanel widgetJPanel; //holds the widgets: buttons, comboboxes and checkbox 56 | private JPanel widgetPadder; //encapsulates widgetJPanel and adds padding around the edges 57 | 58 | /** 59 | * This constructor sets the name of the JFrame. 60 | * It also creates a DrawPanel object that extends JPanel for drawing the shapes and contains 61 | * a statuslabel for mouse position. 62 | * Initializes widgets for buttons, comboboxes and checkbox 63 | * It also adds event handlers for the widgets 64 | */ 65 | public DrawFrame() 66 | { 67 | super("SuperPaint Application v2.0!"); //sets the name of DrawFrame 68 | 69 | JLabel statusLabel = new JLabel( "" ); //create JLabel object to pass into DrawPanel 70 | 71 | panel = new DrawPanel(statusLabel); //create draw panel and pass in JLabel 72 | 73 | //create buttons 74 | undo = new JButton( "Undo" ); 75 | redo = new JButton( "Redo" ); 76 | clear = new JButton( "Clear" ); 77 | 78 | //create comboboxes 79 | colors = new JComboBox( colorOptions ); 80 | shapes = new JComboBox( shapeOptions ); 81 | 82 | //create checkbox 83 | filled = new JCheckBox( "Filled" ); 84 | 85 | //JPanel object, widgetJPanel, with grid layout for widgets 86 | widgetJPanel = new JPanel(); 87 | widgetJPanel.setLayout( new GridLayout( 1, 6, 10, 10 ) ); //sets padding between widgets in gridlayout 88 | 89 | //JPanel object, widgetPadder, with flowlayout to encapsulate and pad the widgetJPanel 90 | widgetPadder = new JPanel(); 91 | widgetPadder.setLayout(new FlowLayout(FlowLayout.LEADING, 20, 5)); //sets padding around the edges 92 | 93 | // add widgets to widgetJPanel 94 | widgetJPanel.add( undo ); 95 | widgetJPanel.add( redo ); 96 | widgetJPanel.add( clear ); 97 | widgetJPanel.add( colors ); 98 | widgetJPanel.add( shapes ); 99 | widgetJPanel.add( filled ); 100 | // add widgetJPanel to widgetPadder 101 | widgetPadder.add( widgetJPanel ); 102 | 103 | //add widgetPadder and panel to JFrame 104 | add( widgetPadder, BorderLayout.NORTH); 105 | add( panel, BorderLayout.CENTER); 106 | 107 | // create new ButtonHandler for button event handling 108 | ButtonHandler buttonHandler = new ButtonHandler(); 109 | undo.addActionListener( buttonHandler ); 110 | redo.addActionListener( buttonHandler ); 111 | clear.addActionListener( buttonHandler ); 112 | 113 | //create handlers for combobox and checkbox 114 | ItemListenerHandler handler = new ItemListenerHandler(); 115 | colors.addItemListener( handler ); 116 | shapes.addItemListener( handler ); 117 | filled.addItemListener( handler ); 118 | 119 | setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 120 | setSize( 500, 500 ); 121 | setVisible( true ); 122 | 123 | } // end DrawFrame constructor 124 | 125 | /** 126 | * private inner class for button event handling 127 | */ 128 | private class ButtonHandler implements ActionListener 129 | { 130 | // handles button events 131 | public void actionPerformed( ActionEvent event ) 132 | { 133 | if (event.getActionCommand().equals("Undo")){ 134 | panel.clearLastShape(); 135 | } 136 | else if (event.getActionCommand().equals("Redo")){ 137 | panel.redoLastShape(); 138 | } 139 | else if (event.getActionCommand().equals("Clear")){ 140 | panel.clearDrawing(); 141 | } 142 | 143 | } // end method actionPerformed 144 | } // end private inner class ButtonHandler 145 | 146 | /** 147 | * private inner class for checkbox and combobox event handling 148 | */ 149 | private class ItemListenerHandler implements ItemListener 150 | { 151 | public void itemStateChanged( ItemEvent event ) 152 | { 153 | // process filled checkbox events 154 | if ( event.getSource() == filled ) 155 | { 156 | boolean checkFill=filled.isSelected() ? true : false; // 157 | panel.setCurrentShapeFilled(checkFill); 158 | } 159 | 160 | // determine whether combo box selected 161 | if ( event.getStateChange() == ItemEvent.SELECTED ) 162 | { 163 | //if event source is combo box colors pass in colorArray at index selected. 164 | if ( event.getSource() == colors) 165 | { 166 | panel.setCurrentShapeColor 167 | (colorArray[colors.getSelectedIndex()]); 168 | } 169 | 170 | //else if event source is combo box shapes pass in index selected 171 | else if ( event.getSource() == shapes) 172 | { 173 | panel.setCurrentShapeType(shapes.getSelectedIndex()); 174 | } 175 | } 176 | 177 | } // end method itemStateChanged 178 | } 179 | 180 | } // end class DrawFrame -------------------------------------------------------------------------------- /DrawPanel.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | import javax.swing.JPanel; 4 | import javax.swing.JLabel; 5 | import java.awt.BorderLayout; 6 | import java.awt.event.MouseEvent; 7 | import java.awt.event.MouseAdapter; 8 | import java.util.ArrayList; 9 | 10 | /** 11 | * This class handles mouse events and uses them to draw shapes. 12 | * It contains a dynamic stack myShapes which is the shapes drawn on the panel. 13 | * It contains a dynamic stack clearedShape which is the shapes cleared from the panel. 14 | * It has many variables for the current shape [type, variable to store shape object, color, fill]. 15 | * It contains a JLabel called statusLabel for the mouse coordinates 16 | * It has mutator methods for currentShapeType, currentShapeColor and currentShapeFilled. 17 | * It has methods for undoing, redoing and clearing shapes. 18 | * It has a private inner class MouseHandler which extends MouseAdapter and 19 | * handles mouse and mouse motion events used for drawing the current shape. 20 | */ 21 | 22 | public class DrawPanel extends JPanel 23 | { 24 | private LinkedList myShapes; //dynamic stack of shapes 25 | private LinkedList clearedShapes; //dynamic stack of cleared shapes from undo 26 | 27 | //current Shape variables 28 | private int currentShapeType; //0 for line, 1 for rect, 2 for oval 29 | private MyShape currentShapeObject; //stores the current shape object 30 | private Color currentShapeColor; //current shape color 31 | private boolean currentShapeFilled; //determine whether shape is filled or not 32 | 33 | JLabel statusLabel; //status label for mouse coordinates 34 | 35 | /** 36 | * This constructor initializes the dynamic stack for myShapes and clearedShapes. 37 | * It sets the current shape variables to default values. 38 | * It initalizes statusLabel from JLabel passed in. 39 | * Sets up the panel and adds event handling for mouse events. 40 | */ 41 | public DrawPanel(JLabel statusLabel){ 42 | 43 | myShapes = new LinkedList(); //initialize myShapes dynamic stack 44 | clearedShapes = new LinkedList(); //initialize clearedShapes dynamic stack 45 | 46 | //Initialize current Shape variables 47 | currentShapeType=0; 48 | currentShapeObject=null; 49 | currentShapeColor=Color.BLACK; 50 | currentShapeFilled=false; 51 | 52 | this.statusLabel = statusLabel; //Initialize statusLabel 53 | 54 | setLayout(new BorderLayout()); //sets layout to border layout; default is flow layout 55 | setBackground( Color.WHITE ); //sets background color of panel to white 56 | add( statusLabel, BorderLayout.SOUTH ); //adds a statuslabel to the south border 57 | 58 | // event handling for mouse and mouse motion events 59 | MouseHandler handler = new MouseHandler(); 60 | addMouseListener( handler ); 61 | addMouseMotionListener( handler ); 62 | } 63 | 64 | /** 65 | * Calls the draw method for the existing shapes. 66 | */ 67 | public void paintComponent( Graphics g ) 68 | { 69 | super.paintComponent( g ); 70 | 71 | // draw the shapes 72 | ArrayList shapeArray=myShapes.getArray(); 73 | for ( int counter=shapeArray.size()-1; counter>=0; counter-- ) 74 | shapeArray.get(counter).draw(g); 75 | 76 | //draws the current Shape Object if it is not null 77 | if (currentShapeObject!=null) 78 | currentShapeObject.draw(g); 79 | } 80 | 81 | //Mutator methods for currentShapeType, currentShapeColor and currentShapeFilled 82 | 83 | /** 84 | * Sets the currentShapeType to type (0 for line, 1 for rect, 2 for oval) passed in. 85 | */ 86 | public void setCurrentShapeType(int type) 87 | { 88 | currentShapeType=type; 89 | } 90 | 91 | /** 92 | * Sets the currentShapeColor to the Color object passed in. 93 | * The Color object contains the color for the current shape. 94 | */ 95 | public void setCurrentShapeColor(Color color) 96 | { 97 | currentShapeColor=color; 98 | } 99 | 100 | /** 101 | * Sets the boolean currentShapeFilled to boolean filled passed in. 102 | * If filled=true, current shape is filled. 103 | * If filled=false, current shape is not filled. 104 | */ 105 | public void setCurrentShapeFilled(boolean filled) 106 | { 107 | currentShapeFilled=filled; 108 | } 109 | 110 | 111 | /** 112 | * Clear the last shape drawn and calls repaint() to redraw the panel if clearedShapes is not empty 113 | */ 114 | public void clearLastShape() 115 | { 116 | if (! myShapes.isEmpty()) 117 | { 118 | clearedShapes.addFront(myShapes.removeFront()); 119 | repaint(); 120 | } 121 | } 122 | 123 | /** 124 | * Redo the last shape cleared if clearedShapes is not empty 125 | * It calls repaint() to redraw the panel. 126 | */ 127 | public void redoLastShape() 128 | { 129 | if (! clearedShapes.isEmpty()) 130 | { 131 | myShapes.addFront(clearedShapes.removeFront()); 132 | repaint(); 133 | } 134 | } 135 | 136 | /** 137 | * Remove all shapes in current drawing. Also makes clearedShapes empty since you cannot redo after clear. 138 | * It called repaint() to redraw the panel. 139 | */ 140 | public void clearDrawing() 141 | { 142 | myShapes.makeEmpty(); 143 | clearedShapes.makeEmpty(); 144 | repaint(); 145 | } 146 | 147 | /** 148 | * Private inner class that implements MouseAdapter and does event handling for mouse events. 149 | */ 150 | private class MouseHandler extends MouseAdapter 151 | { 152 | /** 153 | * When mouse is pressed draw a shape object based on type, color and filled. 154 | * X1,Y1 & X2,Y2 coordinate for the drawn shape are both set to the same X & Y mouse position. 155 | */ 156 | public void mousePressed( MouseEvent event ) 157 | { 158 | switch (currentShapeType) //0 for line, 1 for rect, 2 for oval 159 | { 160 | case 0: 161 | currentShapeObject= new MyLine( event.getX(), event.getY(), 162 | event.getX(), event.getY(), currentShapeColor); 163 | break; 164 | case 1: 165 | currentShapeObject= new MyRectangle( event.getX(), event.getY(), 166 | event.getX(), event.getY(), currentShapeColor, currentShapeFilled); 167 | break; 168 | case 2: 169 | currentShapeObject= new MyOval( event.getX(), event.getY(), 170 | event.getX(), event.getY(), currentShapeColor, currentShapeFilled); 171 | break; 172 | 173 | }// end switch case 174 | } // end method mousePressed 175 | 176 | /** 177 | * When mouse is released set currentShapeObject's x2 & y2 to mouse pos. 178 | * Then addFront currentShapeObject onto the myShapes dynamic Stack 179 | * and set currentShapeObject to null [clearing current shape object since it has been drawn]. 180 | * Lastly, it clears all shape objects in clearedShapes [because you cannot redo after a new drawing] 181 | * and calls repaint() to redraw panel. 182 | */ 183 | public void mouseReleased( MouseEvent event ) 184 | { 185 | //sets currentShapeObject x2 & Y2 186 | currentShapeObject.setX2(event.getX()); 187 | currentShapeObject.setY2(event.getY()); 188 | 189 | myShapes.addFront(currentShapeObject); //addFront currentShapeObject onto myShapes 190 | 191 | currentShapeObject=null; //sets currentShapeObject to null 192 | clearedShapes.makeEmpty(); //clears clearedShapes 193 | repaint(); 194 | 195 | } // end method mouseReleased 196 | 197 | /** 198 | * This method gets the mouse pos when it is moving and sets it to statusLabel. 199 | */ 200 | public void mouseMoved( MouseEvent event ) 201 | { 202 | statusLabel.setText(String.format("Mouse Coordinates X: %d Y: %d",event.getX(),event.getY())); 203 | } // end method mouseMoved 204 | 205 | /** 206 | * This method gets the mouse position when it is dragging and sets x2 & y2 of current shape to the mouse pos 207 | * It also gets the mouse position when it is dragging and sets it to statusLabel 208 | * Then it calls repaint() to redraw the panel 209 | */ 210 | public void mouseDragged( MouseEvent event ) 211 | { 212 | //sets currentShapeObject x2 & Y2 213 | currentShapeObject.setX2(event.getX()); 214 | currentShapeObject.setY2(event.getY()); 215 | 216 | //sets statusLabel to current mouse position 217 | statusLabel.setText(String.format("Mouse Coordinates X: %d Y: %d",event.getX(),event.getY())); 218 | 219 | repaint(); 220 | 221 | } // end method mouseDragged 222 | 223 | }// end MouseHandler 224 | 225 | } // end class DrawPanel -------------------------------------------------------------------------------- /LinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | class LinkedList { 4 | private int numberOfNodes = 0; 5 | private ListNode front = null; 6 | 7 | // Returns true if the linked list has no nodes, or false otherwise. 8 | public boolean isEmpty() { 9 | return (front == null); 10 | } 11 | 12 | // Deletes all of the nodes in the linked list. 13 | // Note: ListNode objects will be automatically garbage collected by JVM. 14 | public void makeEmpty() { 15 | front = null; 16 | numberOfNodes = 0; 17 | } 18 | 19 | // Returns the number of nodes in the linked list 20 | public int size() { 21 | return numberOfNodes; 22 | } 23 | 24 | // Adds a node to the front of the linked list. 25 | public void addFront( T element ) { 26 | front = new ListNode( element, front ); 27 | numberOfNodes++; 28 | } 29 | 30 | // Returns a reference to the data in the first node, or null if the list is empty. 31 | public T peek() { 32 | if (isEmpty()) 33 | return null; 34 | 35 | return front.getData(); 36 | } 37 | 38 | // Removes a node from the front of the linked list (if there is one). 39 | // Returns a reference to the data in the first node, or null if the list is empty. 40 | @SuppressWarnings("unchecked") 41 | public T removeFront() { 42 | T tempData; 43 | 44 | if (isEmpty()) 45 | return null; 46 | 47 | tempData = front.getData(); 48 | front = front.getNext(); 49 | numberOfNodes--; 50 | return tempData; 51 | } 52 | 53 | @SuppressWarnings("unchecked") 54 | public void removeEnd(T element) { 55 | ListNode node=front; 56 | while(node.getNext() != null) 57 | { 58 | node = node.getNext(); 59 | } 60 | node.setNext(new ListNode((T)element, null)); 61 | } 62 | 63 | // Return array filled with T objects 64 | @SuppressWarnings("unchecked") 65 | public ArrayList getArray() { 66 | 67 | ArrayList shapeArray=new ArrayList(); 68 | 69 | ListNode node=front; 70 | while (node!=null) 71 | { 72 | shapeArray.add(node.getData()); 73 | node = node.getNext(); 74 | } 75 | 76 | return shapeArray; 77 | } 78 | } -------------------------------------------------------------------------------- /ListNode.java: -------------------------------------------------------------------------------- 1 | public class ListNode { 2 | private T data; 3 | private ListNode next; 4 | 5 | // Constructor: No reference to next node provided so make it null 6 | public ListNode( T nodeData ) { 7 | this( nodeData, null); 8 | } 9 | 10 | // Constructor: Set data and reference to next node. 11 | public ListNode( T nodeData, ListNode nodeNext ) { 12 | data = nodeData; 13 | next = nodeNext; 14 | } 15 | 16 | // Accessor: Return the data for current ListNode object 17 | public T getData() { 18 | return data; 19 | } 20 | 21 | // Accessor: Return reference to next ListNode object 22 | public ListNode getNext() { 23 | return next; 24 | } 25 | 26 | // Mutator: Set new data for current ListNode object 27 | public void setData( T newData ) { 28 | data = newData; 29 | } 30 | 31 | 32 | // Mutator: Set new reference to the next node object 33 | public void setNext( ListNode newNext ) { 34 | next = newNext; 35 | } 36 | } -------------------------------------------------------------------------------- /MyBoundedShape.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | /** 5 | * This is an abstract class with an abstract draw method. It inherits from MyShape 6 | * and contains methods needed for drawing ovals and rectangles. It also contains an instance variable called fill. 7 | */ 8 | abstract class MyBoundedShape extends MyShape 9 | { 10 | private boolean fill; //boolean variable that determines whether the shape is filled or not 11 | 12 | /** 13 | * public constructor that takes no parameters and calls the no parameter constructor for Myshape. 14 | * It sets fill to false. 15 | */ 16 | public MyBoundedShape() 17 | { 18 | super(); 19 | fill=false; 20 | } 21 | 22 | /** 23 | * public overloaded constructor that takes int coordinates and a boolean for fill. 24 | * It passes the coordinates and color into the constructor for Myshape and assigns 25 | * the fill to an instance variable fill. 26 | */ 27 | public MyBoundedShape(int x1, int y1, int x2, int y2, Color color, boolean fill) 28 | { 29 | super(x1, y1, x2, y2, color); 30 | this.fill=fill; 31 | } 32 | 33 | //Mutator methods 34 | 35 | /** 36 | * set fill 37 | */ 38 | public void setFill(boolean fill) 39 | { 40 | this.fill=fill; 41 | } 42 | 43 | /** 44 | * gets the upper left x 45 | */ 46 | public int getUpperLeftX() 47 | { 48 | return Math.min(getX1(),getX2()); 49 | } 50 | 51 | /** 52 | * gets the upper left y 53 | */ 54 | public int getUpperLeftY() 55 | { 56 | return Math.min(getY1(),getY2()); 57 | } 58 | 59 | /** 60 | * gets width 61 | */ 62 | public int getWidth() 63 | { 64 | return Math.abs(getX1()-getX2()); 65 | } 66 | 67 | //Accessor methods 68 | 69 | /** 70 | * gets height 71 | */ 72 | public int getHeight() 73 | { 74 | return Math.abs(getY1()-getY2()); 75 | } 76 | 77 | /** 78 | * return fill 79 | */ 80 | public boolean getFill() 81 | { 82 | return fill; 83 | } 84 | 85 | /** 86 | * Abstract method for drawing the shape that must be overriden 87 | */ 88 | abstract public void draw( Graphics g ); 89 | } // end class MyBoundedShape -------------------------------------------------------------------------------- /MyLine.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | /** 5 | * This class inherits from MyShape and is responsible for drawing a line. 6 | */ 7 | public class MyLine extends MyShape 8 | { 9 | /** 10 | * No parameter constructor which calls the no parameter constructor in MyShape 11 | */ 12 | public MyLine() 13 | { 14 | super(); 15 | } 16 | 17 | /** 18 | * Overloaded constructor that takes coordinates and color. It passes them to the constructor in MyShape 19 | */ 20 | public MyLine( int x1, int y1, int x2, int y2, Color color ) 21 | { 22 | super(x1, y1, x2, y2, color); 23 | } 24 | 25 | /** 26 | * Overrides the draw method in Myshape. It sets the gets the color from Myshape 27 | * and the coordinates it needs to draw from MyShape as well. 28 | */ 29 | @Override 30 | public void draw( Graphics g ) 31 | { 32 | g.setColor( getColor() ); //sets the color 33 | g.drawLine( getX1(), getY1(), getX2(), getY2() ); //draws the line 34 | } 35 | } // end class MyLine -------------------------------------------------------------------------------- /MyOval.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | /** 5 | * This class inherits from MyBoundedShape and is responsible for drawing a oval. 6 | */ 7 | public class MyOval extends MyBoundedShape 8 | { 9 | /** 10 | * No parameter constructor which calls the no parameter constructor in MyBoundedShape. 11 | */ 12 | public MyOval() 13 | { 14 | super(); 15 | } 16 | 17 | /** 18 | * Overloaded constructor that takes coordinates, color and fill. 19 | * It passes them into MyBoundedShape's constructor. 20 | */ 21 | public MyOval( int x1, int y1, int x2, int y2, Color color, boolean fill ) 22 | { 23 | super(x1, y1, x2, y2, color,fill); 24 | } 25 | 26 | /** 27 | * Overrides the draw method in MyBoundedShape. It sets the gets the color from MyBoundedShape 28 | * to set the color and the values it needs to draw from MyBoundedShape as well. 29 | */ 30 | @Override 31 | public void draw( Graphics g ) 32 | { 33 | g.setColor( getColor() ); //sets the color 34 | if (getFill()) //determines whether fill is true or false 35 | g.fillOval( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); //draws a filled oval 36 | else 37 | g.drawOval( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); //draws a regular oval 38 | 39 | } 40 | 41 | } // end class MyOval -------------------------------------------------------------------------------- /MyRectangle.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | /** 5 | * This class inherits from MyBoundedShape and is responsible for drawing a rectangle 6 | */ 7 | public class MyRectangle extends MyBoundedShape 8 | { 9 | /** 10 | * No parameter constructor which calls the no parameter constructor in MyBoundedShape 11 | */ 12 | public MyRectangle() 13 | { 14 | super(); 15 | } 16 | 17 | /** 18 | * Overloaded constructor that takes coordinates, color and fill. 19 | * It passes them into MyBoundedShape's constructor 20 | */ 21 | public MyRectangle( int x1, int y1, int x2, int y2, Color color, boolean fill ) 22 | { 23 | super(x1, y1, x2, y2, color,fill); 24 | } 25 | 26 | /** 27 | * Overrides the draw method in MyBoundedShape. It sets the gets the color from MyBoundedShape 28 | * to set the color and the values it needs to draw from MyBoundedShape as well. 29 | */ 30 | @Override 31 | public void draw( Graphics g ) 32 | { 33 | g.setColor( getColor() ); //sets the color 34 | if (getFill()) //determines whether fill is true or false 35 | g.fillRect( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); //draws a filled rectangle 36 | else 37 | g.drawRect( getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() ); //draws a regular rectangle 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /MyShape.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Graphics; 3 | 4 | /** 5 | * This class contains int coordinates and a Color color. It has accessor and mutator methods for them. 6 | */ 7 | abstract class MyShape 8 | { 9 | private int x1,y1,x2,y2; //coordinates of shape 10 | private Color color; // color of shape 11 | 12 | /** 13 | * public constructor which takes no variables and 14 | * sets coordinates to zero and color to black 15 | */ 16 | public MyShape() 17 | { 18 | x1=0; 19 | y1=0; 20 | x2=0; 21 | y2=0; 22 | color=Color.BLACK; 23 | } 24 | 25 | /** 26 | * overloaded constructor which takes variables for coordinates and 27 | * color assigning them to the instance variables in the class 28 | */ 29 | public MyShape(int x1, int y1, int x2, int y2, Color color) 30 | { 31 | this.x1=x1; 32 | this.y1=y1; 33 | this.x2=x2; 34 | this.y2=y2; 35 | this.color=color; 36 | } 37 | 38 | //Mutator methods 39 | 40 | /** 41 | * Mutator method for x1 42 | */ 43 | public void setX1(int x1) 44 | { 45 | this.x1=x1; 46 | } 47 | 48 | /** 49 | * Mutator method for y1 50 | */ 51 | public void setY1(int y1) 52 | { 53 | this.y1=y1; 54 | } 55 | 56 | /** 57 | * Mutator method for x2 58 | */ 59 | public void setX2(int x2) 60 | { 61 | this.x2=x2; 62 | } 63 | 64 | /** 65 | * Mutator method for y2 66 | */ 67 | public void setY2(int y2) 68 | { 69 | this.y2=y2; 70 | } 71 | 72 | /** 73 | * Mutator method for color 74 | */ 75 | public void setColor(Color color) 76 | { 77 | this.color=color; 78 | } 79 | 80 | 81 | //Accessor methods 82 | 83 | /** 84 | * Accessor method for x1 85 | */ 86 | public int getX1() 87 | { 88 | return x1; 89 | } 90 | 91 | /** 92 | * Accessor method for y1 93 | */ 94 | public int getY1() 95 | { 96 | return y1; 97 | } 98 | 99 | /** 100 | * Accessor method for x2 101 | */ 102 | public int getX2() 103 | { 104 | return x2; 105 | } 106 | 107 | /** 108 | * Accessor method for y2 109 | */ 110 | public int getY2() 111 | { 112 | return y2; 113 | } 114 | 115 | /** 116 | * Accessor method for color 117 | */ 118 | public Color getColor() 119 | { 120 | return color; 121 | } 122 | 123 | /** 124 | * Abstract method for drawing the shape that must be overriden 125 | */ 126 | abstract public void draw( Graphics g ); 127 | 128 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Java-SuperPaint-Application 2 | =========================== 3 | Java paint application 4 | 5 | Required 6 | ---------- 7 | [Java JDK 6](http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.htm) 8 | 9 | Description 10 | ------------ 11 | * This program use inheritance and implementation to create a paint program. 12 | * The paint program lets you select a color, a shape (line, rectangle,oval) and whether or not the shape is filled. 13 | * You use the mouse to drag and draw a shape, the shape's coordinate re-adjust automatically with mouse's position as you are dragging. 14 | * There is an undo button and you can undo as many shapes as you like and a redo button to redo them as well. 15 | * However, if you draw a new shape you can no longer redo. 16 | * There is also a clear button to clear the screen and you cannot undo that. 17 | 18 | ![alt text](http://i.imgur.com/6gzUfxo.png "SuperPaint Application") 19 | -------------------------------------------------------------------------------- /TestDraw.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Paint Gui Assignment 3 | * Name: Jimmy Wang 4 | * Date: March 18 2013 5 | * 6 | * Description: 7 | * This program use inheritance and implementation to create a paint program. 8 | * The paint program lets you select a color, a shape (line, rectangle,oval) and whether or not the shape is filled. 9 | * You use the mouse to drag and draw a shape, the shape's coordinate re-adjust automatically with mouse's position 10 | * as your dragging. 11 | * 12 | * There is an undo button and you can undo as many shapes as you like and a redo button to redo them as well. 13 | * However, if you draw a new shape you can no longer redo. 14 | * There is also a clear button to clear the screen and you cannot undo that. 15 | */ 16 | public class TestDraw 17 | { 18 | public static void main( String args[] ) 19 | { 20 | DrawFrame paintGui = new DrawFrame(); //initalize DrawFrame object called paintGui 21 | } // end main 22 | } // end class TestDraw --------------------------------------------------------------------------------