-
Notifications
You must be signed in to change notification settings - Fork 0
/
PravaFrame.java
177 lines (152 loc) · 6.66 KB
/
PravaFrame.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class PravaFrame extends JFrame {
private JTextArea textArea;
private Color backgroundColor;
Font EHS1 = FontLoader.loadFont("fonts\\ElectronicHighwaySign.TTF", 16f);
CoinCount coinCount = new CoinCount("coin.txt");
// Constructor
PravaFrame(Color backgroundColor, JFrame mainFrame) {
// Set frame properties
this.backgroundColor = backgroundColor;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(700, 700);
this.getContentPane().setBackground(backgroundColor);
this.getContentPane().setFocusable(false);
this.getContentPane().setLayout(null);
Font EHS = FontLoader.loadFont("fonts\\ElectronicHighwaySign.TTF", 20f);
Font EHS1 = FontLoader.loadFont("fonts\\ElectronicHighwaySign.TTF", 16f);
// Create and add BackButton
BackButton backButton = new BackButton(this, mainFrame);
backButton.setBounds(10, 10, 100, 50);
this.getContentPane().add(backButton);
// Create and configure JTextArea
textArea = new JTextArea();
textArea.setFont(EHS1);
textArea.setTabSize(2);
textArea.setBackground(backgroundColor);
textArea.setForeground(Color.WHITE);
textArea.setCaretColor(Color.WHITE);
textArea.setBorder(new ShadowBorder());
int centerX = (this.getWidth() - 400) / 2;
int centerY = (this.getHeight() - 400) / 2;
// Create and configure JScrollPane
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBackground(backgroundColor);
scrollPane.getViewport().setBackground(backgroundColor);
scrollPane.setBounds(centerX, centerY, 400, 400);
scrollPane.setBorder(null);
// Customize scroll bar appearance
scrollPane.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
@Override
protected void configureScrollBarColors() {
this.thumbColor = backgroundColor;
}
});
scrollPane.getHorizontalScrollBar().setUI(new BasicScrollBarUI() {
@Override
protected void configureScrollBarColors() {
this.thumbColor = backgroundColor;
}
});
this.getContentPane().add(scrollPane);
// Create and add JLabel
JLabel ideLabel = new JLabel("IDE");
ideLabel.setFont(EHS);
ideLabel.setForeground(Color.WHITE);
ideLabel.setBounds(centerX, centerY - 50, 100, 50);
this.getContentPane().add(ideLabel);
// Create and add CustomButton for run action
CustomButton runButton = new CustomButton("Run(-1)", 100, 50, centerX, centerY + 410);
runButton.addActionListener(new RunActionListener());
this.getContentPane().add(runButton);
// Create and add CustomButton for docs action
CustomButton docsButton = new CustomButton("Docs", 100, 50, centerX+300, centerY + 410);
docsButton.addActionListener(new DocsActionListener());
this.getContentPane().add(docsButton);
// Load file content into JTextArea
loadFile();
textArea.requestFocusInWindow();
// Create and add JLabel
JLabel titleLabel = new JLabel("Prava");
titleLabel.setFont(EHS);
titleLabel.setForeground(Color.WHITE);
titleLabel.setBounds(325, 10, 100, 50);
this.getContentPane().add(titleLabel);
}
// Load file content into JTextArea
private void loadFile() {
try {
String content = new String(Files.readAllBytes(Paths.get("prava/source.txt")));
textArea.setText(content);
} catch (IOException ex) {
ex.printStackTrace();
}
}
// ActionListener for run button
private class RunActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (coinCount.getCurrentCount() - 1 >= 0) {
// Write text area content to file
Files.write(Paths.get("prava/source.txt"), textArea.getText().getBytes());
// Execute Python script
ProcessBuilder pb = new ProcessBuilder("python", "prava/main.py");
Process p = pb.start();
p.waitFor();
// Read Java output from file
String javaOutput = new String(Files.readAllBytes(Paths.get("prava/java_output.txt")));
// Create and display OutputFrame
OutputFrame outputFrame = new OutputFrame(backgroundColor);
outputFrame.setOutput(javaOutput);
outputFrame.setVisible(true);
try {
// Update coin count
coinCount.changeCount(-1);
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Error updating the coin count");
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
}
// ActionListener for docs button
private class DocsActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Create and configure docs frame
JFrame docsFrame = new JFrame("Docs");
docsFrame.setSize(700, 700);
docsFrame.getContentPane().setBackground(backgroundColor);
// Create and configure docs text area
JTextArea docsTextArea = new JTextArea();
docsTextArea.setBackground(backgroundColor);
docsTextArea.setForeground(Color.WHITE);
docsTextArea.setFont(EHS1);
// Create and configure docs scroll pane
JScrollPane docsScrollPane = new JScrollPane(docsTextArea);
docsScrollPane.setBounds(400, 400, 400, 400);
docsScrollPane.setBorder(null);
docsFrame.getContentPane().add(docsScrollPane);
docsFrame.setVisible(true);
try {
// Load docs content into docs text area
String content = new String(Files.readAllBytes(Paths.get("prava\\docs.txt")));
docsTextArea.setText(content);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}