-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRender.java
297 lines (287 loc) · 9.45 KB
/
Render.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* @(#)Render.java
*
*
* @author
* @version 1.00 2016/9/16
*/
import java.util.Scanner;
import java.io.*;
import java.awt.geom.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.*;
public class Render {
int imageSizeX;
int imageSizeY;
int length;
double[] x;
double[] y;
double[] xOld;
double[] yOld;
double[] m;
int focusOn = 1;
double centerX;
double centerY;
BufferedImage particleImage;
BufferedImage trailImage;
int blackInt = new Color(0,0,0).getRGB();
int whiteInt = new Color(255,255,255).getRGB();
Graphics2D trailGraphics;
Graphics2D particleGraphics;
String directoryTextString;
String directoryImageString;
String directoryImageNameString;
int particleCount;
int trailLength;
double minMass;
int frameStart;
int frameSkip;
int frameCount;
int picCount;
double saveOn;
Color[] colors;
double zoom;
BufferedReader input;
String dataString;
long startTime, elapsedTime;
File inputFile;
FileReader inputFW;
Thread ImageSaver;
double incrementer;
public Render(String name, int particles, int lengthMultiplier, double min, int frameStartConstruct, int frameSkipConstruct, int resolutionX, int resolutionY, double zoomFactor) {
directoryImageString = ".\\"+name+"\\image frames\\";
directoryImageNameString = name+"\\image frames";
particleCount = particles;
trailLength = lengthMultiplier;
minMass = min;
frameStart = frameStartConstruct;
frameSkip = 1;
incrementer = frameSkipConstruct;
imageSizeX = resolutionX - resolutionX%2;
imageSizeY = resolutionY - resolutionY%2;
zoom = zoomFactor;
createDirectory(name);
}
public void methodRunner() throws IOException{
length=particleCount;
trailImage = new BufferedImage(imageSizeX, imageSizeY, BufferedImage.TYPE_INT_ARGB );
trailGraphics = trailImage.createGraphics();
trailGraphics.setColor(Color.black);
trailGraphics.fillRect(0,0,imageSizeX,imageSizeY);
x = new double[particleCount];
xOld = new double[particleCount];
y = new double[particleCount];
yOld = new double[particleCount];
m = new double[particleCount];
colors = new Color[particleCount];
for(int i = 0;i<particleCount;i++){
colors[i] = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256),128);
}
x[0]=0;
frameCount = 0;
picCount = 0;
saveOn = 1;
for (int i=1; i<frameStart; i++){
input.readLine();
frameCount++;
}
while((dataString = input.readLine()) != null){
frameCount++;
startTime=System.nanoTime();
readTextFile(dataString);
focus();
drawParticles();
if (trailLength!=0){
fadeTrails();
drawTrails();
}
saveImage();
}
if (dataString == null){
System.out.println("Reached a null");
}
}
private void readTextFile(String dataString){
Scanner inputScanner = new Scanner(dataString);
int j = 0;
String temp = "";
while(inputScanner.hasNext()){
temp = inputScanner.next();
xOld[j]=x[j];
yOld[j]=y[j];
if (temp.equals("SKIP")){
x[j]=10001;
y[j]=10001;
m[j]=0;
} else {
x[j]=Double.parseDouble(temp);
y[j]=inputScanner.nextDouble();
m[j]=inputScanner.nextDouble();
}
j++;
}
if (picCount==0){
for(int i = 0; i < particleCount; i++){
xOld[i]=x[i];
yOld[i]=y[i];
}
}
inputScanner.close();
}
private void focus(){
if (focusOn==1){ //Full System Barycenter
double sumMass=0;
double sumX=0;
double sumY=0;
for(int i=0; i<length;i++){
sumMass+= m[i];
sumX += x[i]*m[i];
sumY += y[i]*m[i];
}
sumX/=sumMass;
sumY/=sumMass;
centerX=sumX;
centerY=sumY;
}else if(focusOn==2){ //Largest Particle
double max = Double.MIN_VALUE;
for(int i=0; i<length;i++){
if (m[i]>max){
max=m[i];
centerX=x[i];
centerY=y[i];
}
}
}else{ //Origin
centerX=0;
centerY=0;
}
}
private void drawParticles(){
particleImage = new BufferedImage(imageSizeX, imageSizeY, BufferedImage.TYPE_INT_ARGB );
particleGraphics = particleImage.createGraphics();
particleGraphics.setColor(new Color(0, true));
particleGraphics.fillRect(0,0,imageSizeX,imageSizeY);
for(int i=0; i<length; i++){
int dispX = (int)(((x[i]-centerX)*200*zoom)+imageSizeX/2);
int dispY = (int)(((y[i]-centerY)*200*zoom)+imageSizeY/2);
int radius = (int)((Math.sqrt(m[i]))/2*zoom);
for (int j = dispX-radius; j <= dispX+radius;j++){
for (int k = dispY-radius; k <= dispY+radius;k++){
if((j<imageSizeX)&&(k<imageSizeY)&&(j>0)&&(k>0)){
if (((dispX-j)*(dispX-j)+(dispY-k)*(dispY-k))<=(radius*radius)){
particleImage.setRGB(j,k, whiteInt);
}
}
}
}
}
}
private void fadeTrails(){
if (frameCount%trailLength==0){
int testCol = 0;
for(int i = 0; i<imageSizeX; i++){
for(int j = 0; j<imageSizeY; j++){
testCol = trailImage.getRGB(i,j);
if (testCol!= -16777216){
int R = (testCol & 255)-1, G = ((testCol >> 8) & 255)-1, B = ((testCol >> 16) & 255)-1, A = ((testCol >> 24) & 255);
R=Math.max(0,R);
G=Math.max(0,G);
B=Math.max(0,B);
Color c = new Color(B, G, R, A);
trailImage.setRGB(i, j, c.getRGB());
}
}
}
}
}
private void drawTrails(){
for(int i = 0; i<length; i++){
if (x[i]<10000){
if (m[i]>(minMass)){
trailGraphics.setColor(colors[i]);
trailGraphics.draw (new Line2D.Double((((x[i]-centerX)*200*zoom)+imageSizeX/2),(((y[i]-centerY)*200*zoom)+imageSizeY/2),(((xOld[i]-centerX)*200*zoom)+imageSizeX/2),(((yOld[i]-centerY)*200*zoom)+imageSizeY/2)));
if (m[i]>(2*minMass)){
trailGraphics.draw (new Line2D.Double((((x[i]-centerX)*200*zoom)+imageSizeX/2)+1,(((y[i]-centerY)*200*zoom)+imageSizeY/2),(((xOld[i]-centerX)*200*zoom)+imageSizeX/2)+1,(((yOld[i]-centerY)*200*zoom)+imageSizeY/2)));
trailGraphics.draw (new Line2D.Double((((x[i]-centerX)*200*zoom)+imageSizeX/2),(((y[i]-centerY)*200*zoom)+imageSizeY/2)-1,(((xOld[i]-centerX)*200*zoom)+imageSizeX/2),(((yOld[i]-centerY)*200*zoom)+imageSizeY/2)-1));
trailGraphics.draw (new Line2D.Double((((x[i]-centerX)*200*zoom)+imageSizeX/2)-1,(((y[i]-centerY)*200*zoom)+imageSizeY/2),(((xOld[i]-centerX)*200*zoom)+imageSizeX/2)-1,(((yOld[i]-centerY)*200*zoom)+imageSizeY/2)));
trailGraphics.draw (new Line2D.Double((((x[i]-centerX)*200*zoom)+imageSizeX/2),(((y[i]-centerY)*200*zoom)+imageSizeY/2)+1,(((xOld[i]-centerX)*200*zoom)+imageSizeX/2),(((yOld[i]-centerY)*200*zoom)+imageSizeY/2)+1));
}
}
}
}
}
private void saveImage(){
if ((int)saveOn==frameCount){
if (ImageSaver!=null){
try{
ImageSaver.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
BufferedImage outputImage = new BufferedImage(imageSizeX, imageSizeY, BufferedImage.TYPE_INT_ARGB );
Graphics2D outputGraphics = outputImage.createGraphics();
outputGraphics.drawImage(trailImage, 0, 0, null);
outputGraphics.drawImage(particleImage, 0, 0, null);
ImageSaver = new Thread(){
public void run(){
File f = null;
try{
f = new File(directoryImageString+String.format("%010d", picCount)+".PNG");
ImageIO.write(outputImage, "PNG", f);
}
catch(Exception e){
System.out.println(f.getAbsolutePath() + "\nFAILURE TO SAVE IMAGE");
e.printStackTrace();
}
}
};
ImageSaver.start();
picCount++;
saveOn+=incrementer;
incrementer+=0.0000;
elapsedTime=System.nanoTime()-startTime;
System.out.println("Image "+picCount+" (Frame " + frameCount + ") saved (took " + String.format("%014d", elapsedTime) + " nanoseconds)");
}
}
private void createDirectory(String name){
File directory = new File(directoryImageNameString);
if (!directory.exists()) {
boolean result = false;
try{
directory.mkdir();
result = true;
}
catch(SecurityException se){
System.out.println(directory.getAbsolutePath() + "\nFAILURE TO MAKE DIRECTORY");
se.printStackTrace();
}
if(result) {
System.out.println("Directory created!");
}
}else{
//if the directory exists, delete all existing images
System.out.println("Clearing directory");
for(File file: directory.listFiles())
if (!file.isDirectory())
file.delete();
System.out.println("Directory cleared!");
}
String inputFileName = ".\\" + name + "\\raw data.txt";
inputFile = new File(inputFileName);
input = null;
try {
inputFW = new FileReader (inputFile);
}
catch (IOException e){
System.out.println(inputFile.getAbsolutePath() + "\nFAILURE TO MAKE FileReader");
e.printStackTrace();
}
input = new BufferedReader(inputFW);
}
}