import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.Stack;
public class DrawingPanel extends JPanel {
private Shape currentShape;
private MyMenu menu;
private Point startingPoint;
private Point endingPoint;
private Point startPoint;//pointClicked
private Point endPoint;
private boolean loadSelected;
private boolean deleteSelected;
private Stack<Shape> shapeStack = new Stack<>();
private Color borderColor;
public DrawingPanel() {
}
public DrawingPanel(MyMenu menu) {
this();
this.menu = menu;
addMouseListener(new DrawingListener());
addMouseMotionListener(new DrawingListener());
}
class DrawingListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
startingPoint = new Point(e.getX(), e.getY());
startPoint = new Point(e.getX(), e.getY());
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
//startPoint = new Point(e.getX(), e.getY());
//deleteSelected = menu.isDeletePressed();
}
@Override
public void mouseDragged(MouseEvent e) {
endingPoint = new Point(e.getX(), e.getY());
String shapeSelected = menu.getShapeSelected();
if (shapeSelected.equals("Line"))
currentShape = new Line(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y, borderColor);
if (shapeSelected.equals("Oval"))
currentShape = new Oval(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y, borderColor);
if (shapeSelected.equals("Rectangle"))
currentShape = new Rectangle(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y,borderColor);
if (shapeSelected.equals("Square"))
currentShape = new Square(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y, borderColor);
if (shapeSelected.equals("Circle"))
currentShape = new Circle(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y,borderColor);
// if (shapeSelected.equals("Rectangle"))
// currentShape = new Rectangle(startingPoint.x, startingPoint.y, endingPoint.x, endingPoint.y);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if(currentShape != null) {
MyMenu.getShapeList().add(currentShape);
repaint();
}
//currentShape = null;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g1 = (Graphics2D) g;
//deleteSelected = menu.isDeletePressed();
borderColor = menu.getBorderColor();
deleteSelected = menu.isDeletePressed();
if (currentShape != null) {//toDO:solve null pointer exception
// g1.setColor(Color.green);
// g1.fillRect(currentShape.startingPoint.x , currentShape.startingPoint.y, currentShape.endingPoint.x, currentShape.endingPoint.y);
// g1.setColor(Color.RED);
// g1.drawRect(currentShape.startingPoint.x, currentShape.startingPoint.y, currentShape.endingPoint.x, currentShape.endingPoint.y);
currentShape.drawShape(g1, currentShape.startingPoint, currentShape.endingPoint);//draw the current then the rest
for (Shape s : MyMenu.getShapeList()) {
//g1.setStroke(new BasicStroke(2));
drawingShape(g1, s);
}
}
//System.out.println(currentShape);
loadSelected = menu.isLoadFile();
if(loadSelected) {
load(g);
}
if(deleteSelected) {
delete(g);
for (Shape s : MyMenu.getShapeList()) {
//g1.setStroke(new BasicStroke(2));
drawingShape(g1, s);
// }
}}
}
public void drawingShape(Graphics g, Shape s) {
s.drawShape(g, s.startingPoint, s.endingPoint);
}
public void load(Graphics g) {
ObjectInputStream in;
Shape s;
try {
in = new ObjectInputStream(new FileInputStream("Shapes.txt"));
while ((s = (Shape) in.readObject()) != null) {
drawingShape(g, s);
MyMenu.getShapeList().add(s);
repaint();
}
loadSelected = false;
} catch (EOFException eof) {
return;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public void delete(Graphics g) {
Shape s = selectedShape(startPoint);
if(currentShape == s)
remove(currentShape);
remove(s);
currentShape = null;
repaint();
//
// }
}
public void remove(Shape s) {
MyMenu.getShapeList().remove(s);
}
public Shape selectedShape(Point pointClicked) {
Shape q = null;
for (Shape s : MyMenu.getShapeList()) {
if (s.contains(pointClicked)) {
q = s;
shapeStack.push(s);
break;
}
}
return q;
}
}