Skip to content

Commit 9740c39

Browse files
authored
Merge pull request #4 from crazycat9942/wip
1.0.4
2 parents 2458f37 + 2c3dce6 commit 9740c39

3 files changed

Lines changed: 100 additions & 25 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# ***Download the program by going to the latest release on the right hand side of the screen and running the .jar file***
2-
1+
# *Download the program by going to the latest release and running the .jar file OR by clicking the link below*
2+
https://github.com/crazycat9942/FieldArrowsJava/releases/download/v1.0.4/FieldArrowsJava2.jar
3+
34
**Simulates user defined equations including vector fields and complex-valued functions**
45

56
**Creating a vector field:**

src/Main.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Panel extends JPanel {
7474
BufferedImage tempImage = null;
7575
boolean zoomChanged = true;
7676
boolean panChanged = true;
77+
boolean keyChanged = false;
7778
double mandelbrotDetail;
7879
boolean userPressed = false;
7980
Panel()
@@ -172,6 +173,13 @@ public void mouseDragged(MouseEvent m)//when mouse dragged
172173
lastPoint = m.getPoint();
173174
}
174175
});
176+
menu.P.addKeyListener(new KeyAdapter() {
177+
@Override
178+
public void keyTyped(KeyEvent e) {
179+
super.keyTyped(e);
180+
keyChanged = true;
181+
}
182+
});
175183
}
176184
public void updateArrows()
177185
{
@@ -187,6 +195,7 @@ public Point2D.Double getVector(double coordX, double coordY)
187195
Complex R;
188196
Complex S;
189197
//here's where you put the function the vector field is representing
198+
try {
190199
Expression pExpression = new ExpressionBuilder(menu.P.getText())
191200
.variables("x", "y", "t")
192201
.build()
@@ -201,6 +210,11 @@ public Point2D.Double getVector(double coordX, double coordY)
201210
.setVariable("t", time);
202211
P = pExpression.evaluate();
203212
Q = qExpression.evaluate();
213+
}
214+
catch (IllegalArgumentException e)
215+
{
216+
return new Point2D.Double(0,0);
217+
}
204218
//double P = coordY/(Math.pow(coordX, 2) + Math.pow(coordY, 2)) + Math.cos(time);
205219
//double Q = -coordX/(Math.pow(coordX, 2) + Math.pow(coordY, 2)) + Math.sin(time);
206220
double magnitude = Math.sqrt(Math.pow(P, 2) + Math.pow(Q, 2));
@@ -258,7 +272,7 @@ else if(menu.colorWithDiv.isSelected())
258272
public Color hslToRGB(float h, float s, float l)
259273
{//h is [0,360) (degrees), s is [0, 1] l is [0, 1]
260274
//System.out.println(h + " " + s + " " + l);
261-
//l = 0.8f*l + 0.2f;
275+
l = 0.92f*l + 0.08f;
262276
if(s >= 1){s = 0.9999f;}
263277
if(l >= 1){l = 0.9999f;}
264278
s = 0.7f*s + 0.29999f;
@@ -377,7 +391,7 @@ public void run() {
377391
}
378392
else
379393
{
380-
if (!zoomChanged && !panChanged && tempImage != null) {
394+
if (!zoomChanged && !panChanged && !keyChanged && tempImage != null) {
381395
g.drawImage(tempImage, 0, 0, null);
382396
}
383397
else
@@ -389,27 +403,33 @@ public void run() {
389403
double modulus;
390404
Complex R;
391405
Color tempColor;
406+
parser.removeVariable("x");
407+
parser.removeVariable("y");
392408
for(int i = 0; i < numIStep; i++) {
393409
for (int j = 0; j < numJStep; j++) {
394410
m = new Complex(screenToCoordsX(i * iStep), screenToCoordsY(j * jStep));
395411
parser.addComplexVariable("z", m.re(), m.im());
396412
parser.parseExpression(menu.P.getText());
397413
R = parser.getComplexValue();
398414
//System.out.println(R);
399-
argument = Math.toDegrees(Math.atan2(R.im(), R.re()));
400-
if (argument < 0f) {
401-
argument = argument + 360f;
402-
}
403-
modulus = Math.sqrt(Math.pow(R.re(), 2) + Math.pow(R.im(), 2));
404-
modulus = (float) Math.pow((1f - Math.exp(-0.02f * modulus)), 0.3);
405-
tempColor = hslToRGB((float) argument, (float) modulus, (float) modulus);
406-
int rgbval = tempColor.getRGB();
407-
for(int k = i*iStep; k < (i+1)*iStep; k++)
408-
{
409-
for(int l = j*jStep; l < (j+1)*jStep; l++)
410-
{
411-
colors[k][l] = rgbval;
415+
if(!parser.hasError()) {
416+
menu.P.setForeground(Color.white);
417+
argument = Math.toDegrees(Math.atan2(R.im(), R.re()));
418+
if (argument < 0f) {
419+
argument = argument + 360f;
412420
}
421+
modulus = Math.sqrt(Math.pow(R.re(), 2) + Math.pow(R.im(), 2));
422+
modulus = (float) Math.pow((1f - Math.exp(-0.02f * modulus)), 0.3);
423+
tempColor = hslToRGB((float) argument, (float) modulus, (float) modulus);
424+
int rgbval = tempColor.getRGB();
425+
for (int k = i * iStep; k < (i + 1) * iStep; k++) {
426+
for (int l = j * jStep; l < (j + 1) * jStep; l++) {
427+
colors[k][l] = rgbval;
428+
}
429+
}
430+
}
431+
else {
432+
menu.P.setForeground(Color.red);
413433
}
414434
/*for(int k = i*iStep; k < (i + 1)*iStep; k++)
415435
{
@@ -466,7 +486,7 @@ public void run() {
466486
};
467487
range = Range.create(16);
468488
}
469-
if (!zoomChanged && !panChanged && tempImage != null) {
489+
if (!zoomChanged && !panChanged && !keyChanged && tempImage != null) {
470490
g.drawImage(tempImage, 0, 0, null);
471491
}
472492
/*else if(panChanged && tempImage != null)
@@ -979,6 +999,7 @@ protected void paintComponent(Graphics gInit) {
979999
time += 0.049;
9801000
zoomChanged = false;
9811001
panChanged = false;
1002+
keyChanged = false;
9821003
}
9831004
public void refreshScreen() {
9841005
timer = new Timer(0, new ActionListener() {

src/Menu.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
22
import net.miginfocom.swing.MigLayout;
3+
import org.jfree.chart.renderer.category.BarPainter;
34
import org.nfunk.jep.type.Complex;
45

56
import javax.swing.*;
7+
import javax.swing.text.Highlighter;
68
import java.awt.*;
79
import java.awt.event.*;
810
import java.awt.geom.Point2D;
@@ -79,8 +81,16 @@ public void init()
7981
QText = new JTextField("Q: ");
8082
P = new JTextField("cos(0.3x + 0.6y)");
8183
P.setMinimumSize(new Dimension(300, 40));
84+
P.addActionListener(new ActionListener() {
85+
@Override
86+
public void actionPerformed(ActionEvent e) {
87+
System.out.println("shiant");
88+
panel.colorComplex();
89+
}
90+
});
8291
Q = new JTextField("sin(0.3y - 0.6x)");
8392
Q.setMinimumSize(new Dimension(300, 40));
93+
Q.addActionListener(P.getActionListeners()[0]);
8494
P.setFont(new Font(P.getFont().getFontName(), Font.PLAIN, 23));
8595
Q.setFont(new Font(Q.getFont().getFontName(), Font.PLAIN, 23));
8696
//System.out.println(colorWithCurl.isSelected());
@@ -345,6 +355,9 @@ public void update(Graphics g)
345355
scaleWithMag.setEnabled(true);
346356
complexDetail.setEnabled(false);
347357
mandelbrotDetail.setEnabled(false);
358+
contourFreeform.setEnabled(false);
359+
contourCircular.setEnabled(false);
360+
contourFreeformClosed.setEnabled(false);
348361
curlArea.setText("Curl at cursor: " + df.format(panel.getCurl(panel.userMouse.x - 6, panel.userMouse.y - 31)));
349362
divArea.setText("Divergence at cursor: " + df.format(panel.getDiv(panel.userMouse.x - 6, panel.userMouse.y - 31)));
350363
magArea.setText("Magnitude at cursor: " + df.format(panel.getMag(panel.userMouse.x - 6, panel.userMouse.y - 31)));
@@ -355,13 +368,37 @@ public void update(Graphics g)
355368
panel.parser.addVariable("x", mouseCoordX);
356369
panel.parser.addVariable("y", mouseCoordY);
357370
panel.parser.addVariable("t", panel.time);
371+
panel.parser.removeVariable("z");
372+
float tempX = 0;
373+
float tempY = 0;
374+
boolean error = false;
358375
panel.parser.parseExpression(P.getText());
359-
float tempX = (float)panel.parser.getValue();
376+
if(panel.parser.hasError())
377+
{
378+
P.setForeground(Color.red);
379+
outputAtPoint.setText("Invalid input!");
380+
error = true;
381+
}
382+
else {
383+
P.setForeground(Color.white);
384+
tempX = (float) panel.parser.getValue();
385+
}
360386
panel.parser.parseExpression(Q.getText());
361-
float tempY = (float)panel.parser.getValue();
362-
outputAtPoint.setText("Output at mouse: (" + tempX + ", " + tempY + ")");
387+
if(panel.parser.hasError())
388+
{
389+
Q.setForeground(Color.red);
390+
outputAtPoint.setText("Invalid input!");
391+
error = true;
392+
}
393+
else {
394+
Q.setForeground(Color.white);
395+
tempY = (float) panel.parser.getValue();
396+
}
397+
if(!error) {
398+
outputAtPoint.setText("Output at mouse: (" + tempX + ", " + tempY + ")");
399+
}
363400
}
364-
else if(!P.getText().contains("zm"))
401+
else if(!P.getText().contains("zm"))//contains a function of z that isn't mandelbrot
365402
{
366403
Q.setEnabled(false);
367404
addTestPoint.setEnabled(false);
@@ -371,14 +408,25 @@ else if(!P.getText().contains("zm"))
371408
scaleWithMag.setEnabled(false);
372409
complexDetail.setEnabled(true);
373410
mandelbrotDetail.setEnabled(false);
411+
contourFreeform.setEnabled(true);
412+
contourCircular.setEnabled(true);
413+
contourFreeformClosed.setEnabled(true);
374414
double mouseCoordX = panel.screenToCoordsX(panel.userMouse.x - 6);
375415
double mouseCoordY = panel.screenToCoordsY(panel.userMouse.y - 31);
376416
coordAtPoint.setText("Mouse coordinates: " + mouseCoordX + " + " + mouseCoordY + "i");
377417
panel.parser.addComplexVariable("z", mouseCoordX, mouseCoordY);
378418
panel.parser.addVariable("t", panel.time);
379419
panel.parser.parseExpression(P.getText());
380-
Complex temp = panel.parser.getComplexValue();
381-
outputAtPoint.setText("Output at mouse: " + (float)temp.re() + " + " + (float)temp.im() + "i");
420+
if(panel.parser.hasError())
421+
{
422+
P.setForeground(Color.red);
423+
outputAtPoint.setText("Invalid input!");
424+
}
425+
else {
426+
Complex temp = panel.parser.getComplexValue();
427+
P.setForeground(Color.white);
428+
outputAtPoint.setText("Output at mouse: " + (float) temp.re() + " + " + (float) temp.im() + "i");
429+
}
382430
}
383431
else
384432
{
@@ -389,13 +437,18 @@ else if(!P.getText().contains("zm"))
389437
scaleWithMag.setEnabled(false);
390438
complexDetail.setEnabled(false);
391439
mandelbrotDetail.setEnabled(true);
440+
contourFreeform.setEnabled(false);
441+
contourCircular.setEnabled(false);
442+
contourFreeformClosed.setEnabled(false);
392443
double mouseCoordX = panel.screenToCoordsX(panel.userMouse.x - 6);
393444
double mouseCoordY = panel.screenToCoordsY(panel.userMouse.y - 31);
394445
coordAtPoint.setText("Mouse coordinates: " + (float)mouseCoordX + " + " + (float)mouseCoordY + "i");
395446
outputAtPoint.setText("Number of iterations to diverge: " + panel.mandelbrotDiverge(new Complex(mouseCoordX, mouseCoordY)) + " (max " + (int)panel.mandelbrotDetail + ")");
396447
}
397448
//Arrow tempArrow = new Arrow(panel.axes.screenToCoordsX(panel.userMouse.x), panel.axes.screenToCoordsY(panel.userMouse.y), panel);
398449
//jacobArea.setText("Jacobian:\n x y t\nP " + tempArrow.DPDX + " " + tempArrow.DPDY + " 3\nQ 4 5 6");
450+
Graphics2D g2d = (Graphics2D)g;
451+
g2d.setStroke(new BasicStroke(1));//thickness/size of line of test point trajectory
399452
for(Point i: testPoints)
400453
{
401454
panel.g.drawOval(i.x, i.y, 2, 2);
@@ -407,7 +460,7 @@ else if(!P.getText().contains("zm"))
407460
Arrow temp = new Arrow(panel.screenToCoordsX(tempX), panel.screenToCoordsY(tempY), panel);
408461
tempX += 3*Math.cos(temp.angle);
409462
tempY += 3*Math.sin(temp.angle);
410-
panel.g.drawLine((int)tempX2, (int)tempY2, (int)tempX, (int)tempY);
463+
g2d.drawLine((int)tempX2, (int)tempY2, (int)tempX, (int)tempY);
411464
}
412465
tempX = i.x;
413466
tempY = i.y;

0 commit comments

Comments
 (0)