Java Programing - Write a program that draws two types of shapes: ovals and rectangles.

Please see attached document for detail information - I used Swing Gui Builder which is unacceptable so it has to be create the Components from scratch.


See code below 


File name - Project3.java


package project3;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.awt.Dimension;

import javax.swing.*;

import java.lang.reflect.Field;

public class Project3 extends javax.swing.JFrame {

 public String Error = "";

 private javax.swing.JButton jButton1;

 private javax.swing.JButton jButton2;

 private javax.swing.JButton jButton3;


 // <editor-fold defaultstate="collapsed" desc="Generated Code">

 private javax.swing.JComboBox<String> jComboBox1;

 private javax.swing.JComboBox<String> jComboBox2;

 private javax.swing.JComboBox<String> jComboBox3;

 private javax.swing.JLabel jLabel1;

 private javax.swing.JLabel jLabel2;

 private javax.swing.JLabel jLabel3;

 private javax.swing.JLabel jLabel4;

 private javax.swing.JLabel jLabel5;

 private javax.swing.JLabel jLabel6;

 private javax.swing.JLabel jLabel7;

 private javax.swing.JPanel jPanel1;

 private javax.swing.JTextField jTextField1;

 private javax.swing.JTextField jTextField2;

 private javax.swing.JTextField jTextField3;

 private javax.swing.JTextField jTextField4;

 public Project3() {

   initComponents();

   this.setTitle("Shapes");

 }

 public static void main(String[] args) {

   try {

     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

       if ("Nimbus".equals(info.getName())) {

         javax.swing.UIManager.setLookAndFeel(info.getClassName());

         break;

       }

     }

   } catch (ClassNotFoundException ex) {

     java.util.logging.Logger.getLogger(Project3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

   } catch (InstantiationException ex) {

     java.util.logging.Logger.getLogger(Project3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

   } catch (IllegalAccessException ex) {

     java.util.logging.Logger.getLogger(Project3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

   } catch (javax.swing.UnsupportedLookAndFeelException ex) {

     java.util.logging.Logger.getLogger(Project3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

   }

   java.awt.EventQueue.invokeLater(new Runnable() {

     public void run() {

       new Project3().setVisible(true);

     }

   });

 }

 private void initComponents() {

   jComboBox1 = new javax.swing.JComboBox<>();

   jComboBox2 = new javax.swing.JComboBox<>();

   jLabel1 = new javax.swing.JLabel();

   jLabel2 = new javax.swing.JLabel();

   jLabel3 = new javax.swing.JLabel();

   jComboBox3 = new javax.swing.JComboBox<>();

   jTextField1 = new javax.swing.JTextField();

   jTextField2 = new javax.swing.JTextField();

   jLabel4 = new javax.swing.JLabel();

   jLabel5 = new javax.swing.JLabel();

   jButton1 = new javax.swing.JButton();

   jButton2 = new javax.swing.JButton();

   jButton3 = new javax.swing.JButton();

   jPanel1 = new javax.swing.JPanel();

   jTextField3 = new javax.swing.JTextField();

   jTextField4 = new javax.swing.JTextField();

   jLabel6 = new javax.swing.JLabel();

   jLabel7 = new javax.swing.JLabel();

   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

   jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Oval", "Rectangle"}));

   jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Solid", "Hollow"}));

   jLabel1.setText("Shape type");

   jLabel2.setText("Fill type");

   jLabel3.setText("Color");

   jComboBox3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Red", "Black", "Orange", "Yellow", "Green", "Blue", "Magenta"}));

   jTextField1.setText("30");

   jTextField2.setText("40");

   jLabel4.setText("x coordinate");

   jLabel5.setText("y coordinate");

   jButton1.setText("Draw");

   jButton2.setText("Clear");

   jButton3.setText("Exit");

 

   jButton1.addActionListener(this::jButton1ActionPerformed);

   jButton2.addActionListener(this::jButton2ActionPerformed);

   jButton3.addActionListener(this::jButton3ActionPerformed);


   jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Shape Drawing", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 13), new java.awt.Color(51, 51, 51))); // NOI18N

   jPanel1.setMinimumSize(new java.awt.Dimension(340, 340));

   jPanel1.setPreferredSize(new java.awt.Dimension(220, 220));

   javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

   jPanel1.setLayout(jPanel1Layout);

   jPanel1Layout.setHorizontalGroup(

       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

           .addGap(0, 0, Short.MAX_VALUE)

   );

   jPanel1Layout.setVerticalGroup(

       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

           .addGap(0, 0, Short.MAX_VALUE)

   );

   jTextField3.setText("100");

   jTextField4.setText("125");

   jLabel6.setText("Width");

   jLabel7.setText("Height");

   javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

   getContentPane().setLayout(layout);

   layout.setHorizontalGroup(

       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

           .addGroup(layout.createSequentialGroup()

               .addContainerGap()

               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                   .addComponent(jLabel1)

                   

                   .addComponent(jLabel2)

                   .addComponent(jLabel3)

                   .addComponent(jLabel6)

                   .addComponent(jLabel7)

                   .addComponent(jLabel4)

                   .addComponent(jLabel5))

               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)

               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                   .addComponent(jTextField2)

                   .addComponent(jTextField1)

                   .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                   .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 93, Short.MAX_VALUE)

                   .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                   .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.TRAILING)

                   .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.TRAILING))

               .addGap(18, 18, 18)

               .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)

               .addGap(45, 45, 45))

           .addGroup(layout.createSequentialGroup()

               .addGap(179, 179, 179)

               .addGap(179, 179, 179)

               .addGap(179, 179, 179)

               .addComponent(jButton1)

               .addComponent(jButton2)

               .addComponent(jButton3)

              

               .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

       

       

       

   );

   layout.setVerticalGroup(

       layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

           .addGroup(layout.createSequentialGroup()

               .addContainerGap()

               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                   .addGroup(layout.createSequentialGroup()

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                           .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                           .addComponent(jLabel1))

                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

                           .addComponent(jLabel2)

                           .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                           .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                           .addComponent(jLabel3))

                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                           .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                           .addComponent(jLabel6))

                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

                           .addComponent(jLabel7)

                           .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

                       .addGap(10, 10, 10)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                           .addComponent(jLabel4)

                           .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

                           .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                           .addComponent(jLabel5)))

                   .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))

               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

               .addComponent(jButton1)

               .addComponent(jButton2)

               .addComponent(jButton3)

               .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

       

       

       

   );

   pack();

 }// </editor-fold>

 private boolean ParseInteger(String s, String Error) {

   try {

     Integer.parseInt(s);

   } catch (NumberFormatException e) {

     this.Error += Error + "\n";

     return false;

   }

   return true;

 }

 

 

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

      

    System.exit(0);


  }

   

   

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

   int x = 0, y = 0, w = 0, h = 0;

   this.Error = "";

   if (ParseInteger(jTextField1.getText(), "X Coordinate must be integer")) {

     x = Integer.parseInt(jTextField1.getText());

   }


   if (ParseInteger(jTextField2.getText(), "Y Coordiante must be integer")) {

     y = Integer.parseInt(jTextField2.getText());

   }

   if (ParseInteger(jTextField3.getText(), "Width Must be integer")) {

     w = Integer.parseInt(jTextField3.getText());

   }


   if (ParseInteger(jTextField4.getText(), "Height Must be integer")) {

     h = Integer.parseInt(jTextField4.getText());

   }


   if (!this.Error.equals("")) {

     JOptionPane.showMessageDialog(null, this.Error, "Error", JOptionPane.WARNING_MESSAGE);

     return;

   }


   Rectangle rec = new Rectangle(x, y, w, h);//create rectangle for only dim and pos

   String strco = jComboBox3.getSelectedItem().toString().toLowerCase();//get the color

   Color color;

   try {

     Field field = Color.class.getField(strco);

     color = (Color) field.get(null);

   } catch (Exception e) {

     color = null;

   }

//solid or hollow

   boolean fill = true;

   if (jComboBox2.getSelectedItem().toString().equals("Hollow")) {

     fill = false;

   }

   //rectangle or oval

   if (jComboBox1.getSelectedItem().toString().equals("Rectangle")) {

     Rectangular re = new Rectangular(rec, color, fill);

     //create the rectangle with all the atributtes

     painting(re);

   } else {

     Oval ov = new Oval(rec, color, fill);

     //create the oval with all the atributtes

     painting(ov);

   }

 }

 

  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

   jTextField1.setText("0");

   jTextField2.setText("0");

   jTextField3.setText("0");

   jTextField4.setText("0");

    }


 public void painting(Shape shp) {

   try {

     Drawing d = new Drawing();

     d.setSize(d.getPreferredSize());

     d.drawShape(shp);

     //reset the pane

     jPanel1.removeAll();

 

     //add the shape

     jPanel1.add(d);


     //repaint

     jPanel1.repaint();


     // Increment count_shapes by calling method of shape class

     shp.incrementCountShapes();

   } catch (Exception ex) {

     JOptionPane.showMessageDialog(null, "Out Of Bound: 200x200", "Error", JOptionPane.WARNING_MESSAGE);

   }

 }

 // End of variables declaration

}

abstract class Shape extends Rectangle {

 private static int count_shapes;

 private final Color color;

 private final boolean solid;

 public Shape(Rectangle rctngl, Color color, boolean solid) {

   this.color = color;

   this.solid = solid;

   this.setRect(rctngl.x, rctngl.y, rctngl.width, rctngl.height);

 }

 public void setColor(Graphics g) {

   g.setColor(this.color);

 }

 public boolean getSolid() {

   return this.solid;

 }


 // To increment count_shapes

 public void incrementCountShapes() {

   count_shapes++;

 }

 public int getNoOfShapes() {

   return count_shapes;

 }

 abstract void Draw(Graphics g);

}

class Oval extends Shape {

 public Oval(Rectangle rctngl, Color color, boolean solid) {

   super(rctngl, color, solid);

 }


 @Override

 void Draw(Graphics g) {

   setColor(g);

   if (getSolid()) {

     g.fillOval(this.x, this.y, this.width, this.height);

   } else {

     g.drawOval(this.x, this.y, this.width, this.height);

   }

 }

}

class Rectangular extends Shape {

 public Rectangular(Rectangle rctngl, Color color, boolean solid) {

   super(rctngl, color, solid);

 }


 @Override

 void Draw(Graphics g) {

   setColor(g);

   if (getSolid()) {

     g.fillRect(this.x, this.y, this.width, this.height);

   } else {

     g.drawRect(this.x, this.y, this.width, this.height);

   }

 }

}

class Drawing extends JPanel {

 private Shape shape;


 @Override

 protected void paintComponent(Graphics grphcs) {

   shape.Draw(grphcs);

   grphcs.drawString("" + shape.getNoOfShapes(), 5, 35);

 }


 @Override

 public Dimension getPreferredSize() {

   Dimension dmnsn = new Dimension(201, 201);


   return dmnsn;


   //To change body of generated methods, choose Tools | Templates.

 }

 public void drawShape(Shape shape) throws Exception {

   if (contains(shape.width, shape.height)) {

     this.shape = shape;

     repaint();

   } else {

     throw new OutsideBounds(111);

   }

 }

}


class OutsideBounds extends Exception {

 private final int codigoError;

 public OutsideBounds(int codigoError) {

   super();

   this.codigoError = codigoError;

 }

 @Override


 public String getMessage() {

   String Message = "";

   switch (codigoError) {

     case 111:

       Message = "OutsideBounds";

   }

   return Message;


 }

}

Get Help With a similar task to - Java Programing - Write a program that draws two types of shapes: ovals and rectangles.

Login to view and/or buy answers.. or post an answer
Additional Instructions:

Programming Project 3 Assignment Requirements: Two documents are expected: (1) One Java program containing all the code for the assignment. I do not accept multiple Java files in a single assignment folder and (2) a Test Plan document containing a table and screen captures. [Please review the information in the Project 3 Deliverables thread. All the attached files are there to aid in your skill development and enhanced knowledge.] Caution: I often send feedback to you after reviewing your assignment. I always send it to your email account that UMGC has on record. It seems to happen often that the feedback email goes to SPAM. Please check your SPAM folder regularly to ensure you are not missing my communications. I expect a response to those emails that deal with feedback The third programming project involves writing one program that draws two types of shapes: ovals and rectangles. This program consists of five classes and a Program Core. The first class is the Shape class, which is an abstract class that extends the predefined Java class Rectangle. It should contain two instance variables, the color of the shape and the density of the shape [solid or hollow]. It should also contain a class (static) variable that keeps track of how many shapes have been created. It should have three instance methods, one class method, and one abstract method: 1. A constructor that accepts three parameters for the purpose of initializing the characteristics of the shape: a. Rectangle object that defines the dimensions and position of the shape b. Color of the shape c. Density of the shape: solid or hollow [It should also update the number of shapes created from the beginning of the program.] 2. An instance method named setColor that accepts the Graphics object as a parameter and sets the color for the next draw operation to the color of the current shape. 3. An instance method named getSolid that returns whether the shape is solid or hollow. 4. A class method named getNoOfShapes that returns the number of shapes created so far excluding processing errors, like an entry that is not an integer. 5. An abstract method named draw that accepts a Graphics object as a parameter. The Shape class has two subclasses: Oval and Rectangular. The first subclass is Oval. It should have the following two methods: 1. A constructor that accepts three parameters for the purpose of initializing the characteristics of the shape, a Rectangle object that defines a. Dimensions and Position of the shape b. Color of the shape a. Density of the shape: solid or hollow 2. An overridden method draw that draws the Oval object on the Graphics object passed as a parameter. The second subclass is Rectangular. It should have the following two methods: 1. A constructor that accepts three parameters for the purpose of initializing the characteristics of the shape, a Rectangle object that defines a. Dimensions and Position of the shape b. Color of the shape c. Density of the shape: solid or hollow 2. An overridden method draw that draws the Rectangular object on the Graphics object passed as a parameter. The fourth class is named Drawing, which should extend the predefined Java class JPanel. It has one instance variable that contains the shape that is currently drawn. It should have three methods: 1. An overridden paintComponent method that draws the current shape on the Graphics object that is passed to it as a parameter. It should also draw the number of shapes that have been created thus far in the upper left corner. 2. An overridden getPreferredSize method that specifies the dimensions of the drawing panel as 200 pixels wide and 200 pixels high. 3. An instance method named drawShape that is passed the current shape to be drawn. It first checks whether the shape provided is no greater than 200 pixels x 200 pixels in width and height. This check has no relation to the coordinate positioning of the shape. If the parameters are greater than the shape, it throws an OutsideBounds exception. Otherwise, it saves the shape in the corresponding instance variable. It then calls repaint to cause that shape to be drawn. For this program, the effect of positioning of the shape that might have part of the shape go outside one or more boundaries of the Drawing Panel should not be considered. [Example: The width of the shape is 190 and the x coordinate is 50. Those values combined will place the shape 40 pixels outside the right boundary of the Drawing Panel. Do not consider these scenarios for this project.] No additional public methods should be included in any of the above classes. The fifth class named OutsideBounds should define a checked exception. The Program Core should contain the main method. It should generate the GUI shown below: The combo-box for the Shape Type should allow two choices, either Rectangle or Oval. The combo-box for the Fill Type should also allow two choices, either Hollow or Solid. The combo-box for the Color should allow seven choices, Black, Red, Orange, Yellow, Green, Blue, or Magenta. Clicking the Draw button should first check whether any non-integer values have been entered in any of the fields that require integers. If so, an error message should be displayed in a JOptionPane window for the specific TextField without the integer. A generic message is not acceptable. Otherwise an appropriate Shape object should be created and passed to the drawShape method of the Drawing class. If that call results in an OutsideBounds exception being thrown, an appropriate error message should be displayed in a JOptionPane window. Note: only successful shapes that are displayed add to the count for Shapes. It might be more user-friendly to add two additional buttons to the Geometric Drawing Window: (1) a Clear Field button for the width, height, and two coordinates TextFields and (2) an Exit button to end the program in a user-friendly manner. Be sure to follow good programming style, which means making all instance variables private, naming all constants, and avoiding the duplication of code. Furthermore, you must select enough different kinds of shapes for a comprehensive Test Plan for the program. CMIS242 Project 3 Page 1 of 2 Fall 2020

Test Plan Matrix - CMIS 242 6380 Intermediate Programming – Date: October 2020 CMIS 242 Project 3 Test Case Shape Type Fill Type Color Width Height X Coordinate Y Coordinate Screen shot # Shape # Message 1 Oval Solid Red 102 130 50 40 1 1   2 Rectangle Hollow Magenta 105.4       2   Width must be integer 3 Rectangle Hollow Magenta 400 140 60 30 3   Out of the bounds 4 Oval Hollow Orange 100 320.23     4   Height must be integer 5 Rectangle Solid Blue 100 160 30.66   5   x coordinate must be integer 6 Oval Hollow Blue 120 140 30 45 6 2   7 Oval Hollow Green 100 150 45 60.78 7   y coordinate must be integer 8 Rectangle Solid Yellow 180 189 102 102 8 3   9 Oval Solid Black 175 102 50 60 9 4   Screen shot 1 Screen shot 2 Screen shot 3 Screen shot 4 Screen shot 5 Screen shot 6 Screen shot 7 Screen shot 8 Screen shot 9 Bedria Demissie CMIS242 PRJ3 Test Plan Page 1 of 15 Summer 2020

Related Questions

Similar orders to Java Programing - Write a program that draws two types of shapes: ovals and rectangles.
21
Views
0
Answers
Nested imbalanced design of expriment using Box-Adjusted wald-type test
I need to provide statistical analysis of a nested non-balanced design of an experiment. I am would like to have the implementation R. I will need the answers to be provided as shown in the attached file (Project.pdf), and also would like to have access to...
34
Views
0
Answers
CMPT 200 Coding Homework
Write a class called Fraction that can store a rational number (reminder: those numbers that can be expressed in the form a/b, where a and b are integers are rational numbers). For example, a variable with a value of ½ would be created using oneHalf ...
15
Views
0
Answers
Artificial Inteligence System Technique
This is a Master Degree course and I have attached example questions, there are 5 questions and only 3 need to be answered. We will get the actual questions on the day of the exam and they need to be completed within 2 hours, which means the expert has to ...