Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify Exercise 14.17 to allow the user to save a drawing into a file or load a

ID: 3531152 • Letter: M

Question

Modify Exercise 14.17 to allow the user to save a drawing into a file or load a prior drawing from a file using object serialization. Add buttons Load (to read objects from a file) and Save (to write objects to a file). Use an ObjectOutputStream to write to the file and an ObjectInputStream to read from the file. Write the array of MyShape objects using method writeObject (class ObjectOutputStream), and read the array using method readObject (ObjectInputStream). The object-serialization mechanism can read or write entire arrays

Explanation / Answer

import java.awt.Color; 4 import java.awt.Graphics; 5 import java.awt.event.MouseAdapter; 6 import java.awt.event.MouseEvent; 7 import java.awt.event.MouseMotionListener; 8 import java.io.EOFException; 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileOutputStream; 12 import java.io.IOException; 13 import java.io.ObjectInputStream; 14 import java.io.ObjectOutputStream; 15 import javax.swing.JFileChooser; 16 import javax.swing.JLabel; 17 import javax.swing.JOptionPane; 18 import javax.swing.JPanel; 19 20 public class DrawPanel extends JPanel 21 { 22 private MyShape shapes[]; // array containing all the shapes 23 private int shapeCount; // statistic on the number of each shape 24 25 private int shapeType; // the type of shape to draw 26 private MyShape currentShape; // the current shape being drawn 27 private Color currentColor; // the color of the shape 28 private boolean filledShape; // whether this shape is filled 29 30 private JLabel statusLabel; // label displaying mouse coordinates 31 32 // constructor 33 public DrawPanel( JLabel status ) 34 { 35 shapes = new MyShape[ 100 ]; // create the array 36 shapeCount = 0 ; // initially we have no shapes 37 38 setShapeType( 0 ); // initially draw lines 39 setDrawingColor( Color.BLACK ); // start drawing with black 24 Chapter 14 Files and Streams 40 setFilledShape( false ); // not filled by default 41 currentShape = null ; // not drawing anything initially 42 43 setBackground( Color.WHITE ); // set a white background 44 45 // add the mouse listeners 46 MouseHandler mouseHandler = new MouseHandler(); 47 addMouseListener( mouseHandler ); 48 addMouseMotionListener( mouseHandler ); 49 50 // set the status label for displaying mouse coordinates 51 statusLabel = status; 52 } // end DrawPanel constructor 53 54 // draw shapes using polymorphism 55 public void paintComponent( Graphics g ) 56 { 57 super .paintComponent( g ); 58 59 for ( int i = 0 ; i 2 ) 70 shapeType = 0 ; 71 72 this .shapeType = shapeType; 73 } // end method setShapeType 74 75 // sets the drawing color 76 public void setDrawingColor( Color c ) 77 { 78 currentColor = c; 79 } // end method setDrawingColor 80 81 // clears the last shape drawn 82 public void clearLastShape() 83 { 84 if ( shapeCount > 0 ) 85 { 86 shapeCount--; 87 repaint(); 88 } // end if 89 } // end method clearLastShape 90 91 // clears all drawings on this panel 92 public void clearDrawing() 93 { 94 shapeCount = 0 ; Student Solution Exercises 25 95 repaint(); 96 } // end method clearDrawing 97 98 // sets whether to draw a filled shape 99 public void setFilledShape( boolean isFilled ) 100 { 101 filledShape = isFilled; 102 } // end method setFilledShape 103 104 // load saved drawing 105 public void loadDrawing() 106 { 107 ObjectInputStream input = null ; 108 109 try // user selects file, shapes are input 110 { 111 // use JFileChooser to select file 112 JFileChooser fileChooser = new JFileChooser(); 113 fileChooser.setFileSelectionMode( 114 JFileChooser.FILES_ONLY ); 115 116 int result = fileChooser.showOpenDialog( 117 DrawPanel. this ); 118 119 // if user clicked Cancel button on dialog, return 120 if ( result == JFileChooser.CANCEL_OPTION ) 121 return ; 122 123 // get selected file 124 File fileName = fileChooser.getSelectedFile(); 125 126 // display error if invalid 127 if ( ( fileName == null ) || 128 ( fileName.getName().equals( "" ) ) ) 129 { 130 JOptionPane.showMessageDialog( DrawPanel. this , 131 "Invalid File Name" , "Invalid File Name" , 132 JOptionPane.ERROR_MESSAGE ); 133 return ; 134 } // end if 135 136 // open file for input 137 input = new ObjectInputStream( 138 new FileInputStream( fileName ) ); 139 140 // read in number of shapes using deserialization 141 shapeCount = ( Integer ) input.readObject(); 142 143 // read in shapes using deserialization 144 // set shapes to be displayed on drawPanel 145 shapes = ( MyShape [] ) input.readObject(); 146 147 repaint(); // redraw shapes 148 } // end try 149 catch ( EOFException eofException ) 26 Chapter 14 Files and Streams 150 { 151 JOptionPane.showMessageDialog( DrawPanel. this , 152 "No more records in file." , "End of File" , 153 JOptionPane.ERROR_MESSAGE ); 154 } // end catch 155 catch ( ClassNotFoundException classNotFoundException ) 156 { 157 JOptionPane.showMessageDialog( DrawPanel. this , 158 "Unable to create object." , "Class Not Found" , 159 JOptionPane.ERROR_MESSAGE ); 160 } // end catch 161 catch ( IOException ioException ) 162 { 163 JOptionPane.showMessageDialog( DrawPanel. this , 164 "Error opening file." , "Error" , 165 JOptionPane.ERROR_MESSAGE ); 166 } // end catch 167 finally 168 { 169 try 170 { 171 if ( input != null ) 172 input.close(); // close file and stream 173 } // end try 174 catch ( IOException ioException ) 175 { 176 JOptionPane.showMessageDialog( DrawPanel. this , 177 "Error closing file." , "Error" , 178 JOptionPane.ERROR_MESSAGE ); 179 } // end catch 180 } // end finally 181 } // end method loadDrawing 182 183 // save drawing as serialized objects 184 public void saveDrawing() 185 { 186 ObjectOutputStream output = null ; 187 188 try 189 { 190 // use JFileChooser to select file 191 JFileChooser fileChooser = new JFileChooser(); 192 fileChooser.setFileSelectionMode( 193 JFileChooser.FILES_ONLY ); 194 195 int result = fileChooser.showSaveDialog( 196 DrawPanel. this ); 197 198 // if user clicked Cancel button on dialog, return 199 if ( result == JFileChooser.CANCEL_OPTION ) 200 return ; 201 202 // get selected file 203 File fileName = fileChooser.getSelectedFile(); 204 Student Solution Exercises 27 205 // display error if invalid 206 if ( ( fileName == null ) || 207 ( fileName.getName().equals( "" ) ) ) 208 { 209 JOptionPane.showMessageDialog( DrawPanel. this , 210 "Invalid File Name" , "Invalid File Name" , 211 JOptionPane.ERROR_MESSAGE ); 212 return ; 213 } // end if 214 215 // open file for output 216 output = new ObjectOutputStream( 217 new FileOutputStream( fileName ) ); 218 219 // write number of shapes to file 220 output.writeObject( shapeCount ); 221 222 // write shapes to file using serialization 223 output.writeObject( shapes ); 224 } // end try 225 catch ( IOException ioException ) 226 { 227 JOptionPane.showMessageDialog( DrawPanel. this , 228 "Error Opening File" , "Error." , 229 JOptionPane.ERROR_MESSAGE ); 230 } // end catch 231 finally 232 { 233 try 234 { 235 if ( output != null ) 236 output.close(); // close file and stream 237 } // end try 238 catch ( IOException ioException ) 239 { 240 JOptionPane.showMessageDialog( DrawPanel. this , 241 "Error closing file." , "Error" , 242 JOptionPane.ERROR_MESSAGE ); 243 } // end catch 244 } // end finally 245 } // end method saveDrawing 246 247 // Handles mouse events for this JPanel 248 private class MouseHandler extends MouseAdapter 249 implements MouseMotionListener 250 { 251 // creates and sets the initial position for the new shape 252 public void mousePressed( MouseEvent e ) 253 { 254 if ( currentShape != null ) 255 return ; 256 257 // create the appropriate shape based on shapeType 258 switch ( shapeType ) 259 { 28 Chapter 14 Files and Streams 260 case 0: 261 currentShape = new MyLine( e.getX(), e.getY(), 262 e.getX(), e.getY(), currentColor ); 263 break ; 264 case 1: 265 currentShape = new MyOval( e.getX(), e.getY(), 266 e.getX(), e.getY(), currentColor, filledShape ); 267 break ; 268 case 2: 269 currentShape = new MyRect( e.getX(), e.getY(), 270 e.getX(), e.getY(), currentColor, filledShape ); 271 break ; 272 } // end switch 273 } // end method mousePressed 274 275 // fixes the current shape onto the panel 276 public void mouseReleased( MouseEvent e ) 277 { 278 if ( currentShape == null ) 279 return ; 280 281 // set the second point on the shape 282 currentShape.setX2( e.getX() ); 283 currentShape.setY2( e.getY() ); 284 285 // only set the shape if there is room in the array 286 if ( shapeCount
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote