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

Java 2D Drawing Application. Implement the application 13.31 on pages 594 and 59

ID: 3863089 • Letter: J

Question

Java 2D Drawing Application. Implement the application 13.31 on pages 594 and 595 in the textbook as illustrated in Fig. 13.34 on page 595. 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 chose to draw with a gradient, then 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.

Information on how to program this application can be found in the following sections of the textbook: Exercises 10.1 and 10.2 in section 10.11 on pages 434 and 435, Exercise 12.17 on pages 552-553, and Exercise 13.31 on pages 594-595. I would advise reviewing those sections before beginning this assignment.

Note: Do not use the NetBeans GUI generator for this assignment.

Java 2D Drawings Undo Clear Shape: oval Filled Use Gradient 1st Color... 2nd Color... Line Width 10 Dash Length: 15 Dashed 547, 206)

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.TextField;
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.JCheckBox;
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;
static JCheckBox fillBox;
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, 5, 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";
fillBox = new JCheckBox("Fill");
panel.add(fillBox);
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("Rectangle")) {
if (Boolean.parseBoolean(temp[6])) {
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 {
g.drawRect(
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("Oval")) {
if (Boolean.parseBoolean(temp[6])) {
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 {
g.drawOval(
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")) {
if (fillBox.isSelected()) {
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 {
g.drawOval(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")) {
if (fillBox.isSelected()) {
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 {
g.drawRect(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 + " " + fillBox.isSelected());
}
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