Java: I want to use global variables, but doing so slows by program down by a factor of two.

This is a runnable version of the code.

import java.awt.Color;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Simple extends JComponent {
public static double pi = Math.PI;
public static Complex one = new Complex(1,0), i = new Complex(0,1);
public static JFrame window = new JFrame();
public static Complex[][] codomain = new Complex[1280][720];
public static double plane_width =2, plane_height=720*2.0/1280;
public static double xoff=0, yoff=0;
public static int max_iters=400*4;
public static int t=2;
    public static void main (String[] args) {
    fillCodomainArray(0,0,1280-1,720-1,codomain);
    window.add(new Simple());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize((int)1280+28,(int)720+57);
    window.setVisible(true);
    window.addMouseListener(new SimpleML());
}
public void paint(Graphics g) {
    for (int x=0; x<1280; x++)
        for (int y=0; y<720; y++) {
            g.setColor(calcColor(codomain[x][y]));
            g.fillRect(x,y,1,1);
        }  
}
public static void fillCodomainArray(int x0, int y0, int xF, int yF, Complex[][] arrayToFill) {
    int w = arrayToFill.length;
    int h = arrayToFill[0].length;
    double time1 = System.currentTimeMillis();
    for (int x=x0; x<=xF; x++)
        for (int y=y0; y<=yF; y++)
            arrayToFill[x][y]=f(pixelToComplex(x,y,w,h));
    System.out.println("Filling the codomain array took " + (System.currentTimeMillis()-time1)/1000 + " seconds");
}

//converts pixel in window to point in complex plane
public static Complex pixelToComplex(double x, double y, int w, int h) {
    return new Complex((x-w/2)*plane_width/(w/2)+xoff,-(y-h/2)*plane_height/(h/2)+yoff);
}
public static Complex f(Complex z) {
    int r = t;
    Complex c = new Complex(3.5949999999,0);    
    c=z;
    int n=1;
    while (abs(z)<=4 && n<=max_iters) { 
        n++;
        z = A(P(z,t),c);
    }
    if (n>=max_iters)
        z.escapeTime=-100;
    else z.escapeTime=n;
    return z;
}
public static Color calcColor(Complex w) { //Return RGB value for f(z)
    double n = w.escapeTime;
    if (n<0) return Color.BLACK;
    int r = (int)(Math.sin(0.2 * n + 4) * 127.5+127.5);
    int g = (int)(Math.sin(0.3 * n + 2) * 127.5+127.5);
    int b = (int)(Math.sin(0.1 * n + 3) * 127.5+127.5);
    return new Color(r,g,b);                
}   
//Functions of complex variables defined
public static double abs(Complex z) {             //Length of z
    return Math.sqrt(z.re*z.re+z.im*z.im);
}
public static double abs(double x) {
    return Math.abs(x);
}
public static double phase(Complex z) {           //Phase of z
    return (Math.atan2(z.im,z.re)+2*pi)%(2*pi);
}

public static Complex conj(Complex z) {           //Conjugate
    return new Complex(z.re,-z.im);
}

public static Complex A(Complex z, Complex w) {   //Addition of two complexes
    return new Complex(z.re+w.re,z.im+w.im);
}
public static Complex M(Complex z, Complex w) {   //Multiplication
    return new Complex(z.re*w.re-z.im*w.im, z.re*w.im+z.im*w.re);
}
public static Complex P(Complex z, int n) {       //Complex to integer power
    Complex w = z;
    for (int i=1; i<n; i++)
        w = M(w,z);
    return w;
}
}
class Complex {
double re;
double im;
double escapeTime;
public Complex() {
    re=0;
    im=0;
    escapeTime=0;
}
public Complex(double a, double b) {
    re=a;
    im=b;
    escapeTime=0;
}
public String toString() {
    String str="";
    if (re>=0) str+=" " +re;
    else str+=re;
    if (im>=0) str+=" + " + im + "i";
    else str+=" - " + Math.abs(im) + "i";
    return str;
}
}
class SimpleML implements MouseListener  {   
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e)  {
    Complex z = Simple.pixelToComplex(e.getX()-9, e.getY()-38,1280,720);
    Complex w = Simple.codomain[e.getX()-9][e.getY()-38];
    Color color = Simple.calcColor(w);
    System.out.println("\n\n__________________________\n");
    System.out.println("Pixel clicked: "+ (e.getX()-9) + "  " + (e.getY()-38));
    System.out.println("z    =  "+ z);
    System.out.println("w    =  "+ w);
    System.out.println("\u03C6(w) =   " + Math.toDegrees(Simple.phase(w)));
    System.out.println("|w|  =   " + Simple.abs(w));
    System.out.println("C(w) =   (" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() +")");
    System.out.println("__________________________");
    System.out.println("Escape Time = " + w.escapeTime);
}
}

Changing int r=2 on line 46 to int r=t causes the performance decline. If the program executes too slowly on your computer set the variable max_iters on line 14 to something smaller.~~~~

/r/learnprogramming Thread Parent