Repository navigation
Expand file tree
/
Copy pathMainContainer.java
More file actions
120 lines (102 loc) · 3.16 KB
/
Copy pathMainContainer.java
File metadata and controls
120 lines (102 loc) · 3.16 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package java_paint;
import javax.swing.*;
import java.awt.*;
import java.io.File;
/**
* MainContainer class is where we set a layout in our main window, for setting a menu in the top,
* a drawing area in the center, and a brush configuration tool in the bottom.
*
* Also, from this class we call the JFileChooser component which will allows us to select the path
* for opening and saving a file.
*
* @author
*
*/
public class MainContainer extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private MenuBar myMenuBar;
private ToolPalette myBrushes;
private DrawingPanel myCanvas;
private ToolsSubPanel myOptionTools;
public MainContainer() {
super("Drawing Panel");
setupWindow();
initializeComponents();
}
private void setupWindow() {
setSize(800, 600);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initializeComponents() {
myMenuBar = new MenuBar(this);
myCanvas = new DrawingPanel(this);
myOptionTools = new ToolsSubPanel();
setJMenuBar(myMenuBar);
myBrushes = new ToolPalette();
add(myBrushes, BorderLayout.NORTH);
add(myCanvas, BorderLayout.CENTER);
add(myOptionTools, BorderLayout.SOUTH);
}
public void clearDrawing() {
myCanvas.clearDrawing();
}
public void openFile() {
try {
String fileToBeOpen = getFileToBeOpened();
if (fileToBeOpen.length() > 0)
new ImageFrameViewer(fileToBeOpen);
} catch (Exception e) {
e.printStackTrace();
}
}
protected String getFileToBeOpened() {
String fileToOpen = "";
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// user selects a file
File selectedFile = fileChooser.getSelectedFile();
fileToOpen = selectedFile.getAbsolutePath();
}
return fileToOpen;
}
public void saveImage() {
String outputFile = "";
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
// user selects the destination file
File selectedFile = fileChooser.getSelectedFile();
outputFile = selectedFile.getAbsolutePath();
if (outputFile.length() > 0)
myCanvas.saveDrawingToFile(outputFile);
}
}
public int getBrushThickness() {
return myOptionTools.getThicknessSlider().getValue();
}
public Color getBrushColor() {
return myOptionTools.getColorButton().getBackground();
}
public boolean isFigureFilled() {
return myOptionTools.isCheckBoxSelected();
}
public String getBrushType() {
return myBrushes.getButtonSelected();
}
public void setBrushType(String opcion) {
myBrushes.setButtonSelected(opcion);
}
public void repaintDrawing() {
myCanvas.repaint();
}
public void undoLastAction() {
myCanvas.undo();
myCanvas.repaint();
}
}