-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawing.java
More file actions
48 lines (35 loc) · 1.32 KB
/
Copy pathDrawing.java
File metadata and controls
48 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Drawing extends JPanel{
private Shape shape;
//Overridden method that draws the current shape on the Graphics
@Override
public void paint(Graphics g) {
super.paint(g);
shape.setColor(g);
shape.draw(g);
shape.getNoOfShapes();
}
//Overridden method that specifies the dimensions of the drawing panel
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
//checks whether the shape provided will completely fit within the panel
public void drawShape(Shape shape){
try {
if (contains(shape.x, shape.y) && contains(shape.x, shape.y + shape.height)
&& contains(shape.x + shape.width, shape.y) && contains(shape.x + shape.width, shape.y + shape.height)) {
this.shape = shape;
}throw new OutsideBounds("Shape cannot fit into panel!");
}catch(OutsideBounds o) {
JOptionPane.showMessageDialog(null, o.getMessage());
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Please enter valid dimension and coordinates!");
}
repaint();
}
}