-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
212 lines (158 loc) · 4.53 KB
/
Main.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
/**********************************************************
* E/11/269 *
**********************************************************/
import java.awt.*; /* java abstract window toolkit */
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.geom.Line2D;
public class Main extends JPanel {
/* title for the panel */
public static final String TITLE = "TEST IMAGE";
/* how big should my panel be */
public static final int IMG_WIDTH = 600;
public static final int IMG_HEIGHT = 600;
/* where to map the panel to complex plane
* Basically min and max for real and complex axis
* You can change these numbers and see what you get
*/
public static final double minRe = -1.50;
public static final double maxRe = 1;
public static final double minIm = -1.5;
public static final double maxIm = 1.5;
/*
* following array keeps track of colour for each pixel
*/
public static Color [][] image;
public static final double XFAC = (maxRe - minRe) / IMG_WIDTH;
public static final double YFAC = (maxIm - minIm) / IMG_HEIGHT;
public Main(String [] args) {
/* set the size of the frame */
setPreferredSize(new Dimension(IMG_WIDTH, IMG_HEIGHT));
/* new image
* we create the image later and paint it
*/
image = new Color[IMG_WIDTH][IMG_HEIGHT];
try{
Read(args);
} catch (ArrayIndexOutOfBoundsException x){
System.out.println("Give correct input values.");
System.exit(-1);
}
}
public void Read(String [] args){
int length = args.length;
for(int i=0;i<length;i++){
if (args[i].equals("Julia")) {
Complex c2 = getSeperate(args);
generateJuliaImage(c2);
}else if (args[0].equals("Mandelbrot")){
generateMandelbrotImage();
}
}
}
public Complex getSeperate(String [] args){
int length = args.length;
String num = args[length-1];
char realsign = '\0';
char imagsign = '\0';
for (int j = 0; j < num.length(); j++)
{
char a = num.charAt(j);
if(a =='+'){
if(j==0){
realsign = a;
}else{
imagsign=a;
}
}else if(a=='-'){
if(j==0){
realsign = a;
}else{
imagsign=a;
}
}else if ((Character.isDigit(a))&&(j==0)){
realsign = '+';
}
}
String c = "";
if (realsign=='-'){
for(int k=1;k<num.length()-1;k++){
char b = num.charAt(k);
c += b;
}
}else{
c=num;
}
String [] s = c.split("[-+*/]");
String [] imagarr = s[1].split("i");
double realnum = Double.parseDouble(s[0]);
double imagnum = Double.parseDouble(imagarr[0]);
double real=0;
double imag=0;
if (realsign=='+'){
real = realnum;
}else{
real = -realnum;
}
if (imagsign=='+'){
imag = imagnum;
}else{
imag = -imagnum;
}
Complex c2 = new Complex(real,imag);
return c2;
}
public void paintComponent(Graphics g) {
/* call the paintComponent from super class
* sets up the window
*/
super.paintComponent(g);
paintImage((Graphics2D)g);
}
public static void printPoint(Graphics2D frame,
Color z, Points p) {
frame.setColor(z);
frame.draw(new Line2D.Double(p.x, p.y, p.x, p.y));
}
public static void generateMandelbrotImage() {
for(int x=0; x < IMG_WIDTH; x++) {
for(int y=0; y < IMG_HEIGHT; y++) {
Points p = new Points(x, y);
Complex c = mapToComplexPlane(p);
image[x][y] = Mandelbrot.getMandelbrotColor(c);
}
}
System.out.println("Image Generated!");
}
public static void generateJuliaImage(Complex cons) {
for(int x=0; x < IMG_WIDTH; x++) {
for(int y=0; y < IMG_HEIGHT; y++) {
Points p = new Points(x, y);
Complex z = mapToComplexPlane(p);
image[x][y] = Julia.getJuliaColor(z,cons);
}
}
System.out.println("Image Generated!");
}
public static void paintImage(Graphics2D frame) {
for(int x=0; x < IMG_WIDTH; x++) {
for(int y=0; y < IMG_HEIGHT; y++) {
printPoint(frame, image[x][y], new Points(x,y));
}
}
System.out.println("Image Printed!");
}
public static Complex mapToComplexPlane(Points p) {
return new Complex(p.x * XFAC + minRe,
p.y * YFAC + minIm);
}
public static void main(String [] args) {
JFrame frame = new JFrame(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Main(args));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} /* end of class */