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

Java 2D Drawing Application. The application will contain the following elements

ID: 3554751 • Letter: J

Question

Java 2D Drawing Application. The application will contain the following elements:

a) an Undo button to undo the last shape drawn.
b) a Clear button to clear all shapes from the drawing.
c) a combo box for selecting the shape to draw, a line, oval, or rectangle.
d) a checkbox which specifies if the shape should be filled or unfilled.
e) a checkbox to specify whether to paint using a gradient.
f) two JButtons that each show a JColorChooser dialog to allow the user to choose the first and second color in the gradient.
g) a text field for entering the Stroke width.
h) a text field for entering the Stroke dash length.
I) a checkbox for specifying whether to draw a dashed or solid line.
j) a JPanel on which the shapes are drawn.
k) a status bar JLabel at the bottom of the frame that displays the current location of the mouse on the draw panel.

If the user selects to draw with a gradient, set the Paint on the shape to be a gradient of the two colors chosen by the user. If the user does not chosen to draw with a gradient, the Paint with a solid color of the 1st Color.

Note: When dragging the mouse to create a new shape, the shape should be drawn as the mouse is dragged.

Help with this would be amazing its due in 1 week.

Thanks

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPanel extends JPanel implements MouseListener,
       MouseMotionListener {

   static ArrayList<String> itemsDrawn;
   static String shape, color;

   public static void main(String[] args) {
       JFrame frame = new JFrame("Java 2D Drawing");

       frame.setSize(500, 500);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       BorderLayout borderLayout = new BorderLayout();
       frame.setLayout(borderLayout);

       final JPanel panel = new JPanel();

       panel.setLayout(new GridLayout(0, 4, 0, 0));

      
       JButton clear = new JButton("Clear");
       panel.add(clear);
       JButton undo = new JButton("Undo");
       panel.add(undo);
       String[] itemTypes = { "Oval", "Rectangle", "Line" };
       JComboBox<String> shapeChooser = new JComboBox<>(itemTypes);
       panel.add(shapeChooser);
       shape = "Oval";
       String[] colors = { "Red", "Green", "Blue", "Black" };
       JComboBox<String> colorChooser = new JComboBox<>(colors);
       panel.add(colorChooser);
       color = "Red";

       frame.add(panel,BorderLayout.PAGE_START);

       final MyPanel myPanel = new MyPanel();
       frame.add(myPanel,BorderLayout.CENTER);

       shapeChooser.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent e) {
               // TODO Auto-generated method stub
               JComboBox<String> cb = (JComboBox<String>) e.getSource();
               shape = (String) cb.getSelectedItem();
           }
       });

       colorChooser.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent e) {
               // TODO Auto-generated method stub
               JComboBox<String> cb = (JComboBox<String>) e.getSource();
               color = (String) cb.getSelectedItem();
           }
       });

       clear.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent arg0) {
               // TODO Auto-generated method stub
               itemsDrawn = new ArrayList<>();
               myPanel.repaint();
           }
       });
       undo.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent arg0) {
               // TODO Auto-generated method stub
               if (itemsDrawn.size() != 0) {
                   itemsDrawn.remove(itemsDrawn.size() - 1);
                   myPanel.repaint();
               }
           }
       });

       frame.setVisible(true);
   }

   /**
   *
   */
   private static final long serialVersionUID = 5509155261502497671L;

   Point start, end;

   public MyPanel() {
       start = end = null;
       addMouseListener(this);
       addMouseMotionListener(this);
       itemsDrawn = new ArrayList<>();
   }

   @Override
   public void paint(Graphics g) {
       // TODO Auto-generated method stub
       super.paint(g);
       int counter;
       String[] temp;

       for (counter = 0; counter < itemsDrawn.size(); counter++) {
           temp = itemsDrawn.get(counter).split(" ");
           if (temp[1].equals("Red")) {
               g.setColor(Color.RED);
           } else if (temp[1].equals("Green")) {
               g.setColor(Color.GREEN);
           } else if (temp[1].equals("Blue")) {
               g.setColor(Color.BLUE);
           } else if (temp[1].equals("Black")) {
               g.setColor(Color.BLACK);
           }

           if (temp[0].equals("Oval")) {

               g.fillOval(
                       Integer.parseInt(temp[2]) > Integer.parseInt(temp[4]) ? Integer
                               .parseInt(temp[4]) : Integer.parseInt(temp[2]),
                       Integer.parseInt(temp[3]) > Integer.parseInt(temp[5]) ? Integer
                               .parseInt(temp[5]) : Integer.parseInt(temp[3]),
                       Math.abs(Integer.parseInt(temp[4])
                               - Integer.parseInt(temp[2])), Math.abs(Integer
                               .parseInt(temp[5]) - Integer.parseInt(temp[3])));
           } else if (temp[0].equals("Rectangle")) {
               g.fillRect(
                       Integer.parseInt(temp[2]) > Integer.parseInt(temp[4]) ? Integer
                               .parseInt(temp[4]) : Integer.parseInt(temp[2]),
                       Integer.parseInt(temp[3]) > Integer.parseInt(temp[5]) ? Integer
                               .parseInt(temp[5]) : Integer.parseInt(temp[3]),
                       Math.abs(Integer.parseInt(temp[4])
                               - Integer.parseInt(temp[2])), Math.abs(Integer
                               .parseInt(temp[5]) - Integer.parseInt(temp[3])));
           } else if (temp[0].equals("Line")) {
               g.drawLine(Integer.parseInt(temp[2]),
                       Integer.parseInt(temp[3]), Integer.parseInt(temp[4]),
                       Integer.parseInt(temp[5]));
           }
       }
       if (start != null && end != null) {
           if (color.equals("Red")) {
               g.setColor(Color.RED);
           } else if (color.equals("Green")) {
               g.setColor(Color.GREEN);
           } else if (color.equals("Blue")) {
               g.setColor(Color.BLUE);
           } else if (color.equals("Black")) {
               g.setColor(Color.BLACK);
           }

           if (shape.equals("Oval")) {
               g.fillOval(start.x > end.x ? end.x : start.x,
                       start.y > end.y ? end.y : start.y,
                       Math.abs(end.x - start.x), Math.abs(end.y - start.y));
           } else if (shape.equals("Rectangle")) {
               g.fillRect(start.x > end.x ? end.x : start.x,
                       start.y > end.y ? end.y : start.y,
                       Math.abs(end.x - start.x), Math.abs(end.y - start.y));
           } else if (shape.equals("Line")) {
               g.drawLine(start.x, start.y, end.x, end.y);
           }
       }
   }

   @Override
   public void mouseDragged(MouseEvent arg0) {
       // TODO Auto-generated method stub
       end = arg0.getPoint();
       repaint();
   }

   @Override
   public void mouseMoved(MouseEvent arg0) {
       // TODO Auto-generated method stub

   }

   @Override
   public void mouseClicked(MouseEvent arg0) {
       // TODO Auto-generated method stub

   }

   @Override
   public void mouseEntered(MouseEvent arg0) {
       // TODO Auto-generated method stub

   }

   @Override
   public void mouseExited(MouseEvent arg0) {
       // TODO Auto-generated method stub

   }

   @Override
   public void mousePressed(MouseEvent arg0) {
       // TODO Auto-generated method stub
       start = arg0.getPoint();
   }

   @Override
   public void mouseReleased(MouseEvent arg0) {
       // TODO Auto-generated method stub

       if (start != null && end != null) {
           itemsDrawn.add(shape + " " + color + " " + start.x + " " + start.y
                   + " " + end.x + " " + end.y);

       }
       start = null;
       end = null;
   }

}

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