Below is the Java code for the 2D drawing application.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class DrawingApplication extends JFrame {
JPanel drawPanel;
JButton undoButton, clearButton;
JComboBox<String> shapeComboBox;
JCheckBox fillCheckbox, gradientCheckbox;
JTextField firstColorField, secondColorField;
JTextField strokeWidthField, strokeDashLengthField;
JCheckBox dashedLineCheckbox;
JLabel statusBar;
Shape lastShape;
ArrayList<Shape> shapes = new ArrayList<>();
public DrawingApplication() {
super("Drawing Application");
setLayout(new BorderLayout());
drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
add(drawPanel, BorderLayout.CENTER);
statusBar = new JLabel("Mouse position: (0, 0)");
add(statusBar, BorderLayout.SOUTH);
undoButton = new JButton("Undo");
undoButton.addActionListener(new ActionListener() {
atOverride
public
void
actionPerformed(ActionEvent e) {
if (shapes.size() > 0) {
drawPanel.repaint();
shapes.remove(shapes.size() - 1);
}
}
});
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
atOverride
public
void
actionPerformed(ActionEvent e) {
shapes.clear();
drawPanel.repaint();
}
});
shapeComboBox = new JComboBox<>(new String[]{"Line", "Oval", "Rectangle"});
fillCheckbox = new JCheckBox("Fill");
fillCheckbox.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
gradientCheckbox = new JCheckBox("Gradient");
gradientCheckbox.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
firstColorField = new JTextField();
firstColorField.setColumns(5);
firstColorField.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
secondColorField = new JTextField();
secondColorField.setColumns(5);
secondColorField.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
strokeWidthField = new JTextField();
strokeWidthField.setColumns(3);
strokeWidthField.setText("1");
strokeWidthField.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
strokeDashLengthField = new JTextField();
strokeDashLengthField.setColumns(3);
strokeDashLengthField.setText("0");
strokeDashLengthField.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
dashedLineCheckbox = new JCheckBox("Dashed Line");
dashedLineCheckbox.addActionListener(new ActionListener() {
atOverride
public void actionPerformed(ActionEvent e) {
if (lastShape != null) {
drawPanel.repaint();
}
}
});
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(10, 2));
Note that "at overide " is written in full because it is showing inappropriate word. when using it, change it to the short form.