forked from AlexIIL/BetterLoadingScreen_1.7
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLoadingFrame.java
More file actions
150 lines (130 loc) · 4.47 KB
/
LoadingFrame.java
File metadata and controls
150 lines (130 loc) · 4.47 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
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
package alexiil.mods.load;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class LoadingFrame extends JFrame {
private class ThreadIncrementer extends Thread {
private final AtomicBoolean shouldIncrement = new AtomicBoolean(true);
private final float from, to, diff;
private final long time;
private long timeLeft;
public ThreadIncrementer(float from, float to, long timeLeft) {
this.from = from;
this.to = to;
diff = to - from;
this.timeLeft = timeLeft;
this.time = timeLeft;
}
public void stopIncrementing() {
shouldIncrement.set(false);
incrementer = null;
}
@Override
public void run() {
while (timeLeft > 0 && shouldIncrement.get()) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
timeLeft -= 250;
long timeDiff = time - timeLeft;
double percent = timeDiff / (double) time;
setProgress(from + percent * diff);
repaint();
}
if (incrementer == this) incrementer = null;
}
}
private JPanel contentPane;
private JLabel lblState;
private JProgressBar progressBar;
private JProgressBar GTprogressBar;
private ThreadIncrementer incrementer;
public static void setSystemLAF() {
if (GraphicsEnvironment.isHeadless()) {
return;
}
String clsName = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(clsName);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Launch the application.
*/
public static LoadingFrame openWindow() {
try {
LoadingFrame frame = new LoadingFrame();
frame.setBounds(getWindowBounds(frame));
frame.setAlwaysOnTop(true);
frame.setVisible(true);
return frame;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Rectangle getWindowBounds(LoadingFrame frame) {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle bounds = frame.getBounds();
return new Rectangle(
(size.width - bounds.width) / 2,
(size.height - bounds.height) / 2,
bounds.width,
bounds.height);
}
/**
* Create the frame.
*/
public LoadingFrame() {
setTitle("Minecraft Loading");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setBounds(100, 100, 450, 85);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
lblState = new JLabel("State");
panel.add(lblState, BorderLayout.NORTH);
JPanel panel_1 = new JPanel();
panel.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new BorderLayout(0, 0));
progressBar = new JProgressBar();
GTprogressBar = new JProgressBar();
progressBar.setStringPainted(true);
GTprogressBar.setStringPainted(true);
panel_1.add(progressBar, BorderLayout.NORTH);
panel_1.add(GTprogressBar, BorderLayout.NORTH);
}
public void setMessage(String message) {
lblState.setText(message);
}
public void setProgress(double percent) {
progressBar.setValue((int) percent);
GTprogressBar.setValue((int) percent);
}
public void setProgressIncrementing(float from, float to, long howLongFor) {
if (incrementer != null) {
incrementer.stopIncrementing();
}
incrementer = new ThreadIncrementer(from, to, howLongFor);
incrementer.start();
}
}