161k views
3 votes
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.

2 Answers

4 votes

Answer:

sadness and depression is my answer

Step-by-step explanation:

User Heiko Hatzfeld
by
4.3k points
2 votes

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.

User Brian Schlenker
by
4.2k points