Drawing Pad 4 - Enhanced


Time Sheet

Task

Time (hours.)

1.       Read textbook chapter 9, and course presentation for topics relevant to exercise 9.1.

2.00

2.       Create the planning document

0.25

3.       Review sample code in the textbook and other sources from the Internet

2.00

4.       Code the Drawing Pad 4 application by enhancing the existing code from the text  

3.00

5.       Test the Drawing Pad 4 application/Applet

2.00

6.       Create the html to call the Drawing Pad 4 application Applet

0:25

7.       Test the html

0.25

8.       Upload files to NJIT web server and create link from home page

0.25

9.       Test the site

0.25

TOTAL

10.25

 

Planning Document

User Case: Enhanced Drawing Pad 4

Primary Actor: user who wants to use a drawing pad to draw some basic shapes or write text.  

Precondition: The user has access to the server in which the drawing pad application is stored.

Main Success Scenario:

1.      The user start the drawing pad Applet by clicking on the link on Exercise9.1 . The system responds by displaying a Drawing Pad 4 Applet showing a menu at the top and a tool bar on the left.

 

2.      The user can execute the following operations from the main menu at the top:

·         File – This option allows the user to save the current drawing or to load an drawing previously saved

·         Tools – This option shows the shapes that the user can draw on the drawing pad. These options are the same as the options shown on the tool bar on the left.

·         Option – This option allow the user to select the color and font characteristics to be used in the drawing pad.

 

3.      The user can select the following drawing options from the tool bar:

·         Scribble

·         Line

·         Oval

·         Rectangle

·         Filled Oval

·         Filled rectangle

·         Text

·         Triangle

·         Filled Triangle

·         Diamond

·         Filled Diamond

·         Eraser – The user can erase part or all the drawing pad

 

4.      When the user select one shape to draw, the system sets the drawing shape to the selected shape and remains until the user select another shape or close the applet. For example, if the user select triangle, the drawing pad remains in “triangle mode” until the user selects another shape. Therefore, multiple triangles can be drawn on the drawing pad.

 

5.      The user can select a text function in which the system will allow entering text using the keyboard; however, the system does not allow edition functions; therefore, the text cannot be changed or corrected.

 

6.      The user stop the applet by closing the applet

 

Source Code

Drawing Pad 4 Applet

01 import java.awt.BorderLayout;
02 import java.awt.Dimension;
03 import javax.swing.JApplet;
04  
05  /**
06   * Provides an applet (wrapper) for the DrawingPad4 application.
07   @author Ciro Casimiro
08   */
09  public class DrawingPad4Applet extends JApplet
10  {
11   protected Dimension d;
12   public void init() {
13    d = getSize();
14    resize(d.width, d.height);
15    resize(800600);
16   }
17      /**
18       * Creates a new Applet.
19       */
20      public DrawingPad4Applet()
21      {
22          // create the drawing pad frame
23          DrawingPad4 dp = new DrawingPad4("unused");
24 
25         // set the layout
26         this.setLayout(new BorderLayout());
27 
28         // add the content pane of the frame to the applet
29         this.add(dp.getContentPane(), BorderLayout.CENTER);
30     }
31  }

Drawing Pad 4 Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Enhance Drawing Pad program
03  *                    
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * Description: Enhance Drawing Pad program to do the following:
07  * (1)add new drawing tools
08  * (2)Selecting different background colors
09  * (3)Support selecting image files as the background
10  * (4)Support a zoom-in and zoom-out function
11  * (5)Support moving and deleting shapes 
12  * (program derived from code in the textbook and Web site) 
13  * **************************************************************/
14 
15 import java.awt.*;
16 import javax.swing.*;
17 
18 public class DrawingPad4 extends DrawingPad3 {
19 
20  public DrawingPad4(String title) {
21   super(title);
22  }
23 
24  //Add new shapes: triangle and diamond, and Eraser function 
25  protected void initTools() { 
26      super.initTools();
27   
28   toolkit.addTool(new TwoEndsShapeTool(canvas, "Triangle",
29     new TriangleShape()));
30   toolkit.addTool(new TwoEndsShapeTool(canvas, "Filled Triangle",
31     new FilledTriangleShape()));
32   toolkit.addTool(new TwoEndsShapeTool(canvas, "Diamond",
33     new DiamondShape()));
34   toolkit.addTool(new TwoEndsShapeTool(canvas, "Filled Diamond",
35     new FilledDiamondShape()));
36   toolkit.addTool(new TwoEndsShapeTool(canvas, "Eraser"new Eraser()));
37 
38   drawingCanvas.setTool(toolkit.getTool(0));
39 
40  }
41 
42  public static void main(String[] args) {
43   JFrame frame = new DrawingPad4("Drawing Pad");
44   frame.setSize(width, height);
45   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
46   frame.setLocation(screenSize.width / - width / 2, screenSize.height
47     - height / 2);
48   frame.setVisible(true);
49  }
50 }

Triangle Shape Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Class to add a triangle shape
03  *                     
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * (program derived from code in the textbook and Web site) 
07  * **************************************************************/
08 import java.awt.*;
09  
10 public class TriangleShape extends TwoEndsShape {
11  
12     public void draw(Graphics g) {
13         int x = Math.min(x1, x2);
14         int y = Math.min(y1, y2);
15         int w = Math.abs(x1 - x21;
16         int h = Math.abs(y1 - y21;
17         if (color != null) {
18             g.setColor(color);
19         }
20         Polygon pg = new Polygon();
21         pg.addPoint(x, y + h);
22         pg.addPoint(x + w, y + h);
23         pg.addPoint(x + w / 2, y);
24  
25         g.drawPolygon(pg);
26     }
27  
28     public void drawOutline(Graphics g, int x1, int y1, int x2, int y2) {
29         int x = Math.min(x1, x2);
30         int y = Math.min(y1, y2);
31         int w = Math.abs(x1 - x21;
32         int h = Math.abs(y1 - y21;
33  
34         Polygon pg = new Polygon();
35         pg.addPoint(x, y + h);
36         pg.addPoint(x + w, y + h);
37         pg.addPoint(x + w / 2, y);
38  
39         g.drawPolygon(pg);
40     }
41 }
42  
Java2html

Filled Triangle Shape Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Class to add a filled triangle shape
03  *                     
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * (program derived from code in the textbook and Web site) 
07  * **************************************************************/ 
08 import java.awt.*;
09  
10 public class FilledTriangleShape extends TriangleShape {
11  
12     @Override
13     public void draw(Graphics g) {
14         int x = Math.min(x1, x2);
15         int y = Math.min(y1, y2);
16         int w = Math.abs(x1 - x21;
17         int h = Math.abs(y1 - y21;
18         if (color != null) {
19             g.setColor(color);
20         }
21         Polygon pg = new Polygon();
22         pg.addPoint(x, y + h);
23         pg.addPoint(x + w, y + h);
24         pg.addPoint(x + w / 2, y);
25         g.fillPolygon(pg);
26     }
27 
28  

Diamond Shape Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Class to add a diamond shape
03  *                     
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * (program derived from code in the textbook and Web site) 
07  * **************************************************************/
08 
09 import java.awt.*;
10  
11 public class DiamondShape extends TwoEndsShape {
12  
13     public void draw(Graphics g) {
14         int x = Math.min(x1, x2);
15         int y = Math.min(y1, y2);
16         int w = Math.abs(x1 - x21;
17         int h = Math.abs(y1 - y21;
18         if (color != null) {
19             g.setColor(color);
20         }
21         Polygon pg = new Polygon();
22         pg.addPoint(x, y + h / 2);
23         pg.addPoint(x + w / 2, y);
24         pg.addPoint(x + w, y + h / 2);
25         pg.addPoint(x + w / 2, y + h);
26         g.drawPolygon(pg);
27  
28     }
29  
30     public void drawOutline(Graphics g, int x1, int y1, int x2, int y2) {
31         int x = Math.min(x1, x2);
32         int y = Math.min(y1, y2);
33         int w = Math.abs(x1 - x21;
34         int h = Math.abs(y1 - y21;
35  
36         Polygon pg = new Polygon();
37         pg.addPoint(x, y + h / 2);
38         pg.addPoint(x + w / 2, y);
39         pg.addPoint(x + w, y + h / 2);
40         pg.addPoint(x + w / 2, y + h);
41         g.drawPolygon(pg);
42     }
43 }
44  

Filled Diamond Shape Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Class to add a filled diamond shape
03  *                     
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * (program derived from code in the textbook and Web site) 
07  * **************************************************************/ 
08 
09 import java.awt.*;
10  
11 public class FilledDiamondShape extends DiamondShape {
12  
13     @Override
14     public void draw(Graphics g) {
15         int x = Math.min(x1, x2);
16         int y = Math.min(y1, y2);
17         int w = Math.abs(x1 - x21;
18         int h = Math.abs(y1 - y21;
19         if (color != null) {
20             g.setColor(color);
21         }
22         Polygon pg = new Polygon();
23         pg.addPoint(x, y + h / 2);
24         pg.addPoint(x + w / 2, y);
25         pg.addPoint(x + w, y + h / 2);
26         pg.addPoint(x + w / 2, y + h);
27         g.fillPolygon(pg);
28     }
29 
30  

Eraser Class

01 /* *************************************************************
02  * Chapter 9 - Exercise 9.1: Class to erase contents
03  *                     
04  * NJIT CS-602: Java programming and the Web
05  * Author: Ciro Casimiro
06  * (program derived from code in the textbook and Web site) 
07  * **************************************************************/ 
08 import java.awt.*;
09 
10 import java.awt.*;
11  
12 public class Eraser extends TwoEndsShape {
13  
14   public void draw(Graphics g) {
15       int x = Math.min(x1, x2);
16       int y = Math.min(y1, y2);
17       int w = Math.abs(x1 - x21;
18       int h = Math.abs(y1 - y21;    
19       g.clearRect(x, y, w, h);
20     }
21  
22   public void drawOutline(Graphics g, int x1, int y1, int x2, int y2) {
23       int x = Math.min(x1, x2);
24       int y = Math.min(y1, y2);
25       int w = Math.abs(x1 - x21;
26       int h = Math.abs(y1 - y21;    
27       g.drawRect(x, y, w, h);
28   }
29 

Execution of Drawing Pad 4 Applet

Selecting Triangle and Filled Triangle Shapes

Selecting Diamond and Filled Diamond Shapes

Selecting Eraser - To erase all or part of the drawing pad

JUnit for triangle Shape Class