Java Robot: Repeating every 30 minutes - java

What I want to do is run this bot every 30 minutes. What it does it take a screenshot so that I can look at things. I tried using
thread.sleep();
But that wasn't working for me. I set it to some small interval to see if it would actually work, and it just runs once and stops. I am new-ish to Java, but I haven't worked with the Java robot class much, or loops of any kind. Here is my class...
public class usiwa{
public static void main(String[] args) throws AWTException, IOException, InterruptedException{
Robot bot = new Robot();
Date date = new Date();
Random ra = new Random();
int x0 = MouseInfo.getPointerInfo().getLocation().x;
int y0 = MouseInfo.getPointerInfo().getLocation().y;
int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
int y1 = ra.nextInt(80 - 70 + 1) + 70;
int dx = x1 - x0;
int dy = y1 - y0;
// time in msecs
int t = 1000;
int res = Math.max(dx, dy);
if(res > t) res = t;
int d = t/res;
float inv = (float) 1/(res - 1);
float a = 0;
long s = 0;
long e = 0;
s = System.currentTimeMillis();
for(int i = 0; i < res; i++) {
a += inv;
bot.mouseMove(x0 + (int) (a*dx), y0 + (int) (a*dy));
bot.delay(d);
}
e = System.currentTimeMillis();
System.out.println("Total time: " + (float) (e - s)/1000);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(3000);
Rectangle r = new Rectangle(0, 0, 1000, 1000);
BufferedImage p = bot.createScreenCapture(r);
DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy-hh.mm.ss a");
ImageIO.write(p, "png" , new File("C:/Users/Kalob_2/Desktop/Tracker/" + dateFormat.format(date) + ".png"));
}
}
All I want to do is make all of the actions above repeat every 30 minutes.
Thanks for any help.

Here is how you repeat a task. Wrap your existing code in this while loop:
while(true) {
// do something
Thread.sleep(1000); // 1 second, the parameter is miliseconds
}
Alternatively, create a cron job to run your code. The following will repeat every 30 minutes.
30 * * * *

If you want to pause your thread you can do:
Thread.sleep(30*60*1000); \\ 30 minutes
But you are much better off using ScheduledExecutorService

public class usiwa {
public static void main(String[] args) throws AWTException, IOException,
InterruptedException {
Robot bot = new Robot();
Date date = new Date();
Random ra = new Random();
while (true) {
int x0 = MouseInfo.getPointerInfo().getLocation().x;
int y0 = MouseInfo.getPointerInfo().getLocation().y;
int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
int y1 = ra.nextInt(80 - 70 + 1) + 70;
int dx = x1 - x0;
int dy = y1 - y0;
// time in msecs
int t = 1000;
int res = Math.max(dx, dy);
if (res > t)
res = t;
int d = t / res;
float inv = (float) 1 / (res - 1);
float a = 0;
long s = 0;
long e = 0;
s = System.currentTimeMillis();
for (int i = 0; i < res; i++) {
a += inv;
bot.mouseMove(x0 + (int) (a * dx), y0 + (int) (a * dy));
bot.delay(d);
}
e = System.currentTimeMillis();
System.out.println("Total time: " + (float) (e - s) / 1000);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(3000);
Rectangle r = new Rectangle(0, 0, 1000, 1000);
BufferedImage p = bot.createScreenCapture(r);
DateFormat dateFormat = new SimpleDateFormat(
"MM_dd_yyyy-hh.mm.ss a");
ImageIO.write(p, "png",
new File("C:/Users/Kalob_2/Desktop/Tracker/"
+ dateFormat.format(date) + ".png"));
}
}
}

Related

There is a "lag" or "delay" when it comes to changing many ImageIcons inside JLabels. How to avoid?

I am currently working on a 2D-simulator game that takes place in a Perlin noise-generated terrain that is shown on a 41x23 grid. The player (as of the moment, at the center but not yet given an overlaying icon) can move using the arrow keys, but doing so will keep the player static but move the map accordingly. However, when I move, the JFrame lags like hell. Some JLabel instances change their ImageIcons slower than others, creating huge latency and un-"playability". I have tried replacing the inefficient function update with four functions that "efficiently" move the player faster - but the lag or delay remains. I have also reformatted and refactored the function, to no avail. So, I am stuck.
For more info, I am using 32x32 icons that represent the structures and the domain, and the JFrame is 1280x720 in size. I am confident that this is not due to hardware, as the program runs with other memory- or core- consuming programs. Is there any way to solve the lag or delay?
Main Class
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main {
private JFrame frame;
public static ImageIcon water = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/terrain/water.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon sand = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/terrain/sand.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon grass = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/terrain/grass.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon stone = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/terrain/stone.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon ice = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/terrain/ice.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon oak = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/structure/oak.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
public static ImageIcon nullstructure = new ImageIcon(new ImageIcon(Main.class.getResource("/textures/structure/nullstructure.png")).getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* #throws InterruptedException
*/
public Main() throws InterruptedException {
initialize();
}
/**
* Initialize the contents of the frame.
* #throws InterruptedException
*/
private void initialize() throws InterruptedException {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(0, 0, 1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Coordinate playerPos = new Coordinate(0,0);
JLabel[][] terrainArray = new JLabel[41][23];
JLabel[][] structureArray = new JLabel[41][23];
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, 1280, 720);
frame.getContentPane().add(layeredPane);
layeredPane.setLayout(null);
JPanel terrainGrid = new JPanel();
terrainGrid.setBounds(0, 0, 1280, 720);
layeredPane.add(terrainGrid);
GridBagLayout gbl_terrainGrid = new GridBagLayout();
gbl_terrainGrid.columnWidths = new int[]{0};
gbl_terrainGrid.rowHeights = new int[]{0};
gbl_terrainGrid.columnWeights = new double[]{Double.MIN_VALUE};
gbl_terrainGrid.rowWeights = new double[]{Double.MIN_VALUE};
terrainGrid.setLayout(gbl_terrainGrid);
JPanel structureGrid = new JPanel();
layeredPane.setLayer(structureGrid, Integer.valueOf(1));
structureGrid.setBounds(0, 0, 1280, 720);
structureGrid.setBackground(new Color(0,0,0,0));
structureGrid.setOpaque(false);
layeredPane.add(structureGrid);
GridBagLayout gbl_structureGrid = new GridBagLayout();
gbl_structureGrid.columnWidths = new int[]{0};
gbl_structureGrid.rowHeights = new int[]{0};
gbl_structureGrid.columnWeights = new double[]{Double.MIN_VALUE};
gbl_structureGrid.rowWeights = new double[]{Double.MIN_VALUE};
structureGrid.setLayout(gbl_structureGrid);
Coordinate[][] map = new Coordinate[Coordinate.MAP_SIZE][Coordinate.MAP_SIZE];
for(int i = 0; i < Coordinate.MAP_SIZE; i++) {
for(int j = 0; j < Coordinate.MAP_SIZE; j++) {
map[i][j] = new Coordinate(i - ((Coordinate.MAP_SIZE - 1)/2) , j - ((Coordinate.MAP_SIZE - 1)/2));
}
}
for(int i = 0; i < 41; i++) {
for(int j = 0; j < 23; j++) {
terrainArray[i][j] = new JLabel("");
structureArray[i][j] = new JLabel("");
structureArray[i][j].setIcon(Main.nullstructure);
terrainArray[i][j].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + i, playerPos.getZ() - 11 + j)).returnTerrainIcon());
structureArray[i][j].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + i, playerPos.getZ() - 11 + j)).returnStructureIcon());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = i; gbc.gridy = j;
terrainGrid.add(terrainArray[i][j], gbc);
structureGrid.add(structureArray[i][j],gbc);
}
}
frame.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
playerPos.setZ(playerPos.getZ() - 1);
moveUP(terrainArray, structureArray, map, playerPos);
break;
case KeyEvent.VK_DOWN:
playerPos.setZ(playerPos.getZ() + 1);
moveDOWN(terrainArray, structureArray, map, playerPos);
break;
case KeyEvent.VK_RIGHT:
playerPos.setX(playerPos.getX() + 1);
moveRIGHT(terrainArray, structureArray, map, playerPos);
break;
case KeyEvent.VK_LEFT:
playerPos.setX(playerPos.getX() - 1);
moveLEFT(terrainArray, structureArray, map, playerPos);
break;
}
}
});
}
public void moveUP(JLabel[][] terrainArray, JLabel[][] structureArray, Coordinate[][] map, Coordinate playerPos) {
for(int x = 0; x < 41; x++) {
for(int z = 22; z > 0; z--) { //23 - 1
terrainArray[x][z].setIcon(terrainArray[x][z-1].getIcon());
structureArray[x][z].setIcon(structureArray[x][z-1].getIcon());
}
terrainArray[x][0].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + x, playerPos.getZ() - 11)).returnTerrainIcon());
structureArray[x][0].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + x, playerPos.getZ() - 11)).returnStructureIcon());
}
}
public void moveDOWN(JLabel[][] terrainArray, JLabel[][] structureArray, Coordinate[][] map, Coordinate playerPos) {
for(int x = 0; x < 41; x++) {
for(int z = 0; z < 22; z++) { //23 - 1
terrainArray[x][z].setIcon(terrainArray[x][z+1].getIcon());
structureArray[x][z].setIcon(structureArray[x][z+1].getIcon());
}
terrainArray[x][22].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + x, playerPos.getZ() + 11)).returnTerrainIcon());
structureArray[x][22].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + x, playerPos.getZ() + 11)).returnStructureIcon());
}
}
public void moveLEFT(JLabel[][] terrainArray, JLabel[][] structureArray, Coordinate[][] map, Coordinate playerPos) {
for(int z = 0; z < 23; z++) {
for(int x = 40; x > 0; x--) {
terrainArray[x][z].setIcon(terrainArray[x-1][z].getIcon());
structureArray[x][z].setIcon(structureArray[x-1][z].getIcon());
}
terrainArray[0][z].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20, playerPos.getZ() - 11 + z)).returnTerrainIcon());
structureArray[0][z].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20, playerPos.getZ() - 11 + z)).returnStructureIcon());
}
}
public void moveRIGHT(JLabel[][] terrainArray, JLabel[][] structureArray, Coordinate[][] map, Coordinate playerPos) {
for(int z = 0; z < 23; z++) {
for(int x = 0; x < 40; x++) {
terrainArray[x][z].setIcon(terrainArray[x+1][z].getIcon());
structureArray[x][z].setIcon(structureArray[x+1][z].getIcon());
}
terrainArray[40][z].setIcon(findEntry(map, new Coordinate(playerPos.getX() + 20, playerPos.getZ() - 11 + z)).returnTerrainIcon());
structureArray[40][z].setIcon(findEntry(map, new Coordinate(playerPos.getX() + 20, playerPos.getZ() - 11 + z)).returnStructureIcon());
}
}
public static ImageIcon brightenImage(ImageIcon input, float brightness, float offset) {
BufferedImage bI = new BufferedImage(input.getImage().getWidth(null), input.getImage().getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D bIgr = bI.createGraphics();
bIgr.drawImage(input.getImage(), 0, 0, null);
bIgr.dispose();
BufferedImage bO = new BufferedImage(input.getImage().getWidth(null), input.getImage().getHeight(null), BufferedImage.TYPE_INT_RGB);
RescaleOp rop = new RescaleOp(brightness, offset, null);
bO = rop.filter(bI, null);
ImageIcon output = new ImageIcon(bO);
return output;
}
// public void update(JLabel[][] terrainArray, JLabel[][] structureArray, Coordinate[][] map, Coordinate playerPos) {
// for(int i = 0; i < 41; i++) {
// for(int j = 0; j < 23; j++) {
// terrainArray[i][j].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + i, playerPos.getZ() - 11 + j)).returnTerrainIcon());
// structureArray[i][j].setIcon(findEntry(map, new Coordinate(playerPos.getX() - 20 + i, playerPos.getZ() - 11 + j)).returnStructureIcon());
// }
// }
// }
public Coordinate findEntry(Coordinate[][] map, Coordinate pos) {
Coordinate entry = null;
for(int x = 0; x < Coordinate.MAP_SIZE; x++) {
for(int z = 0; z < Coordinate.MAP_SIZE; z++) {
if(pos.getX() == map[x][z].getX() && pos.getZ() == map[x][z].getZ()) {
entry = map[x][z];
}
}
}
return entry;
}
}
Coordinate Class
import javax.swing.ImageIcon;
public class Coordinate {
private int x, z;
private int alt;
public static final int MAP_SIZE = 199;
private ImageIcon str = Main.nullstructure;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public int getAlt() {
return alt;
}
public void setAlt(int alt) {
this.alt = alt;
}
public Coordinate(int x0, int z0) {
this.x = x0;
this.z = z0;
int a = (int) Noise.mapTo(-1 * Math.sqrt(0.5), Math.sqrt(0.5), 0, 100, Noise.noise(x*0.1, z*0.1));
int b = (int) Noise.mapTo(-1 * Math.sqrt(0.5), Math.sqrt(0.5), 0, 100, Noise.noise(x*0.05, z*0.05));
int c = (int) Noise.mapTo(-1 * Math.sqrt(0.5), Math.sqrt(0.5), 0, 100, Noise.noise(x*0.01, z*0.01));
this.alt = (int) ((0.55*c) + (0.25*b) + (0.20*a));
if(Math.random() < 0.05 && alt >= 52 && alt < 70) {
str = Main.oak;
}
}
public ImageIcon returnTerrainIcon() {
if(alt >= 0 && alt < 50) {
return Main.water;
}
else if(alt >= 50 && alt < 52) {
return Main.brightenImage(Main.sand, (float) ((float) 0.95 + ((alt - 50) * 0.025)), (float) 0.36);
}
else if(alt >= 52 && alt < 70) {
return Main.brightenImage(Main.grass, (float) ((float) 0.85 + ((alt - 52) * 0.025)), (float) 0.36);
}
else if(alt >= 80 && alt < 90) {
return Main.brightenImage(Main.stone, (float) ((float) 0.65 + ((alt - 70) * 0.05)), (float) 0.36);
}
else {
return Main.ice;
}
}
public ImageIcon returnStructureIcon() {
return str;
}
}
Perlin (Not original)
import java.util.Random;
//<pre>
// Copyright 2001 Ken Perlin
// Courtesy of https://mrl.cs.nyu.edu/~perlin/experiments/packing/render/Noise.java
/**
Computes Perlin Noise for one, two, and three dimensions.<p>
The result is a continuous function that interpolates a smooth path
along a series random points. The function is consitent, so given
the same parameters, it will always return the same value.
#see ImprovedNoise
*/
public final class Noise {
/**
Initialization seed used to start the random number generator.
*/
static Random randseed = new Random();
public static int seed = (int) Math.floor(randseed.nextInt());
private static final int P = 8;
private static final int B = 1 << P;
private static final int M = B - 1;
private static final int NP = 8;
private static final int N = 1 << NP;
private static int p[] = new int[B + B + 2];
private static double g2[][] = new double[B + B + 2][2];
private static double g1[] = new double[B + B + 2];
private static double[][] points = new double[32][3];
static {
init();
}
private static double lerp(double t, double a, double b) {
return a + t * (b - a);
}
private static double s_curve(double t) {
return t * t * (3 - t - t);
}
/**
Computes noise function for one dimension at x.
#param x 1 dimensional parameter
#return the noise value at x
*/
public static double noise(double x) {
int bx0, bx1;
double rx0, rx1, sx, t, u, v;
t = x + N;
bx0 = ((int) t) & M;
bx1 = (bx0 + 1) & M;
rx0 = t - (int) t;
rx1 = rx0 - 1;
sx = s_curve(rx0);
u = rx0 * g1[p[bx0]];
v = rx1 * g1[p[bx1]];
return lerp(sx, u, v);
}
/**
Computes noise function for two dimensions at the point (x,y).
#param x x dimension parameter
#param y y dimension parameter
#return the value of noise at the point (x,y)
*/
public static double noise(double x, double y) {
int bx0, bx1, by0, by1, b00, b10, b01, b11;
double rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v, q[];
int i, j;
t = x + N;
bx0 = ((int) t) & M;
bx1 = (bx0 + 1) & M;
rx0 = t - (int) t;
rx1 = rx0 - 1;
t = y + N;
by0 = ((int) t) & M;
by1 = (by0 + 1) & M;
ry0 = t - (int) t;
ry1 = ry0 - 1;
i = p[bx0];
j = p[bx1];
b00 = p[i + by0];
b10 = p[j + by0];
b01 = p[i + by1];
b11 = p[j + by1];
sx = s_curve(rx0);
sy = s_curve(ry0);
q = g2[b00];
u = rx0 * q[0] + ry0 * q[1];
q = g2[b10];
v = rx1 * q[0] + ry0 * q[1];
a = lerp(sx, u, v);
q = g2[b01];
u = rx0 * q[0] + ry1 * q[1];
q = g2[b11];
v = rx1 * q[0] + ry1 * q[1];
b = lerp(sx, u, v);
return lerp(sy, a, b);
}
/**
Computes noise function for three dimensions at the point (x,y,z).
#param x x dimension parameter
#param y y dimension parameter
#param z z dimension parameter
#return the noise value at the point (x, y, z)
*/
static public double noise(double x, double y, double z) {
int bx, by, bz, b0, b1, b00, b10, b01, b11;
double rx0, rx1, ry0, ry1, rz, sx, sy, sz, a, b, c, d, u, v, q[];
bx = (int) Math.IEEEremainder(Math.floor(x), B);
if (bx < 0)
bx += B;
rx0 = x - Math.floor(x);
rx1 = rx0 - 1;
by = (int) Math.IEEEremainder(Math.floor(y), B);
if (by < 0)
by += B;
ry0 = y - Math.floor(y);
ry1 = ry0 - 1;
bz = (int) Math.IEEEremainder(Math.floor(z), B);
if (bz < 0)
bz += B;
rz = z - Math.floor(z);
//if (bx < 0 || bx >= B + B + 2)
//System.out.println(bx);
b0 = p[bx];
bx++;
b1 = p[bx];
b00 = p[b0 + by];
b10 = p[b1 + by];
by++;
b01 = p[b0 + by];
b11 = p[b1 + by];
sx = s_curve(rx0);
sy = s_curve(ry0);
sz = s_curve(rz);
q = G(b00 + bz);
u = rx0 * q[0] + ry0 * q[1] + rz * q[2];
q = G(b10 + bz);
v = rx1 * q[0] + ry0 * q[1] + rz * q[2];
a = lerp(sx, u, v);
q = G(b01 + bz);
u = rx0 * q[0] + ry1 * q[1] + rz * q[2];
q = G(b11 + bz);
v = rx1 * q[0] + ry1 * q[1] + rz * q[2];
b = lerp(sx, u, v);
c = lerp(sy, a, b);
bz++;
rz--;
q = G(b00 + bz);
u = rx0 * q[0] + ry0 * q[1] + rz * q[2];
q = G(b10 + bz);
v = rx1 * q[0] + ry0 * q[1] + rz * q[2];
a = lerp(sx, u, v);
q = G(b01 + bz);
u = rx0 * q[0] + ry1 * q[1] + rz * q[2];
q = G(b11 + bz);
v = rx1 * q[0] + ry1 * q[1] + rz * q[2];
b = lerp(sx, u, v);
d = lerp(sy, a, b);
return lerp(sz, c, d);
}
private static double[] G(int i) {
return points[i % 32];
}
private static void init() {
int i, j, k;
double u, v, w, U, V, W, Hi, Lo;
java.util.Random r = new java.util.Random(seed);
for (i = 0; i < B; i++) {
p[i] = i;
g1[i] = 2 * r.nextDouble() - 1;
do {
u = 2 * r.nextDouble() - 1;
v = 2 * r.nextDouble() - 1;
} while (u * u + v * v > 1 || Math.abs(u) > 2.5 * Math.abs(v) || Math.abs(v) > 2.5 * Math.abs(u) || Math.abs(Math.abs(u) - Math.abs(v)) < .4);
g2[i][0] = u;
g2[i][1] = v;
normalize2(g2[i]);
do {
u = 2 * r.nextDouble() - 1;
v = 2 * r.nextDouble() - 1;
w = 2 * r.nextDouble() - 1;
U = Math.abs(u);
V = Math.abs(v);
W = Math.abs(w);
Lo = Math.min(U, Math.min(V, W));
Hi = Math.max(U, Math.max(V, W));
} while (u * u + v * v + w * w > 1 || Hi > 4 * Lo || Math.min(Math.abs(U - V), Math.min(Math.abs(U - W), Math.abs(V - W))) < .2);
}
while (--i > 0) {
k = p[i];
j = (int) (r.nextLong() & M);
p[i] = p[j];
p[j] = k;
}
for (i = 0; i < B + 2; i++) {
p[B + i] = p[i];
g1[B + i] = g1[i];
for (j = 0; j < 2; j++) {
g2[B + i][j] = g2[i][j];
}
}
points[3][0] = points[3][1] = points[3][2] = Math.sqrt(1. / 3);
double r2 = Math.sqrt(1. / 2);
double s = Math.sqrt(2 + r2 + r2);
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
points[i][j] = (i == j ? 1 + r2 + r2 : r2) / s;
for (i = 0; i <= 1; i++)
for (j = 0; j <= 1; j++)
for (k = 0; k <= 1; k++) {
int n = i + j * 2 + k * 4;
if (n > 0)
for (int m = 0; m < 4; m++) {
points[4 * n + m][0] = (i == 0 ? 1 : -1) * points[m][0];
points[4 * n + m][1] = (j == 0 ? 1 : -1) * points[m][1];
points[4 * n + m][2] = (k == 0 ? 1 : -1) * points[m][2];
}
}
}
private static void normalize2(double v[]) {
double s;
s = Math.sqrt(v[0] * v[0] + v[1] * v[1]);
v[0] = v[0] / s;
v[1] = v[1] / s;
}
public static double mapTo(double a1, double a2, double b1, double b2, double x) {
return b1 + ((x-a1)*(b2-b1))/(a2-a1);
}
}
The icons and resources to be used are in this Github code (repository, I guess, I am new to Github):
https://github.com/rubiksRepository/Perlin.git
I would appreciate any help given. Thanks!
So far, Raildex's suggestion is working. The compilation of the map into a single buffered image has reduced the lag tremendously and made the game "playable". For additions in the code, I have made a texture class that supports the textures used; and a MapField whose object has fields that have the compiled BufferImage.
import java.awt.image.BufferedImage;
public class MapField {
private BufferedImage terrain;
private BufferedImage structure;
public MapField(Coordinate[][] map) {
BufferedImage[] colsT = new BufferedImage[Coordinate.MAP_SIZE];
BufferedImage[] colsS = new BufferedImage[Coordinate.MAP_SIZE];
for(int z = 0; z < Coordinate.MAP_SIZE; z++) {
colsT[z] = Texture.mergeTeU(map[z]);
colsS[z] = Texture.mergeStU(map[z]);
}
terrain = Texture.mergeH(colsT);
structure = Texture.mergeH(colsS);
}
public BufferedImage getTerrain() {
return terrain;
}
public void setTerrain(BufferedImage terrain) {
this.terrain = terrain;
}
public BufferedImage getStructure() {
return structure;
}
public void setStructure(BufferedImage structure) {
this.structure = structure;
}
}
Also in the Texture class, there are methods that can be used to merge rows and columns of BufferedImages, using a Graphics2D drawing process:
public static BufferedImage toBI(ImageIcon input) {
BufferedImage output = new BufferedImage(input.getImage().getWidth(null), input.getImage().getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D outputGr = output.createGraphics();
outputGr.drawImage(input.getImage(), 0, 0, null);
outputGr.dispose();
return output;
}
public static BufferedImage mergeH(BufferedImage[] x) {
int i = 0;
BufferedImage rowOutput = new BufferedImage(x[0].getWidth() * x.length, x[0].getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D rowOutputG = rowOutput.createGraphics();
for(BufferedImage xBI : x) {
rowOutputG.drawImage(xBI, i * x[0].getWidth(), 0, null);
i++;
}
rowOutputG.dispose();
return rowOutput;
}
public static BufferedImage mergeTeH(Coordinate[] x) {
BufferedImage[] xIcons = new BufferedImage[x.length];
for(int i = 0; i < x.length; i++) {
xIcons[i] = Texture.toBI(x[i].returnTerrainIcon());
}
return mergeH(xIcons);
}
public static BufferedImage mergeStH(Coordinate[] x) {
BufferedImage[] xIcons = new BufferedImage[x.length];
for(int i = 0; i < x.length; i++) {
xIcons[i] = Texture.toBI(x[i].returnStructureIcon());
}
return mergeH(xIcons);
}
public static BufferedImage mergeU(BufferedImage[] x) {
int i = 0;
BufferedImage colOutput = new BufferedImage(x[0].getWidth(), x[0].getHeight() * x.length, BufferedImage.TYPE_INT_ARGB);
Graphics2D colOutputG = colOutput.createGraphics();
for(BufferedImage xBI : x) {
colOutputG.drawImage(xBI, 0, i * x[0].getHeight(), null);
i++;
}
colOutputG.dispose();
return colOutput;
}
public static BufferedImage mergeTeU(Coordinate[] x) {
BufferedImage[] xIcons = new BufferedImage[x.length];
for(int i = 0; i < x.length; i++) {
xIcons[i] = Texture.toBI(x[i].returnTerrainIcon());
}
return mergeU(xIcons);
}
public static BufferedImage mergeStU(Coordinate[] x) {
BufferedImage[] xIcons = new BufferedImage[x.length];
for(int i = 0; i < x.length; i++) {
xIcons[i] = Texture.toBI(x[i].returnStructureIcon());
}
return mergeU(xIcons);
}
So the bottomline is: for scroll-based maps, you can compile the textures corresponding to the map to improve playability (FPS or lag reduction). For interaction with the game, editing the BufferedImage at a pixel range via turning it to transparency or changing textures can help, at least for me. Not really great in answering the general gist since I am addressing my case only (trying to emulate this solution on others though) and the scroll-base map is on a case-to-case basis, but I hope my solution helps.

YoloV3 object detection with tflite model returns around 160 bounding boxes randomly all tagged with first class from the label text

The shape of the TFLite model is [1, 2535, 85]. You can find the TFLite model here and label text here.
This is how the bug looks.
This is the project I used https://github.com/hunglc007/tensorflow-yolov4-tflite/tree/master/android with some few changes. The changes are as following:
Added the TFLite model and the text in the assets folder (the label text is already present in the project, its the same).
Line 57 DetectorActivity.java.
private static final String TF_OD_API_MODEL_FILE = "yolov3-tiny.tflite";
private static final String TF_OD_API_LABELS_FILE = "file:///android_asset/coco.txt";
line 181 tflite/YoloV4Classifier.java.
private static boolean isTiny = true;
line 426 tflite/YoloV4Classifier.java, (Replace the function to the down below).
This is the code:
private ArrayList<Recognition> getDetectionsForTiny(ByteBuffer byteBuffer, Bitmap bitmap) {
ArrayList<Recognition> detections = new ArrayList<Recognition>();
Map<Integer, Object> outputMap = new HashMap<>();
// outputMap.put(0, new float[1][OUTPUT_WIDTH_TINY[0]][4]);
outputMap.put(0, new float[1][OUTPUT_WIDTH_TINY[1]][labels.size() + 5]);
Object[] inputArray = {byteBuffer};
tfLite.runForMultipleInputsOutputs(inputArray, outputMap);
int gridWidth = OUTPUT_WIDTH_TINY[0];
float[][][] bboxes = (float [][][]) outputMap.get(0);
// float[][][] out_score = (float[][][]) outputMap.get(1);
int count = 0;
for (int i = 0; i < gridWidth; i++) {
float maxClass = 0;
int detectedClass = -1;
final float[] classes = new float[labels.size()];
for (int c = 0; c < labels.size(); c++) {
classes [c] = bboxes[0][i][c + 5];
}
for (int c = 0; c < labels.size(); ++c) {
if (classes[c] > maxClass) {
detectedClass = c;
maxClass = classes[c];
}
}
final float score = maxClass;
if (score > getObjThresh()) {
final float xPos = bboxes[0][i][0];
final float yPos = bboxes[0][i][1];
final float w = bboxes[0][i][2];
final float h = bboxes[0][i][3];
final RectF rectF = new RectF(
Math.max(0, xPos - w / 2),
Math.max(0, yPos - h / 2),
Math.min(bitmap.getWidth() - 1, xPos + w / 2),
Math.min(bitmap.getHeight() - 1, yPos + h / 2));
detections.add(new Recognition("" + i, labels.get(detectedClass), score, rectF, detectedClass));
count++;
}
}
Log.d("Count", " " + count);
return detections;
}
Please I don't know where I'm going wrong! Struggling with it since days! Thanks for helping.

How can I set the value of c when generating a Mandelbrot fractal?

The equation for generating a Mandelbrot fractal is Zn+1 = Zn^2+C. The issue is that, in a computer program, C is used for zoom/resolution and location on screen. My question is, how can I make it so that I can get a fractal like this:
Wolfram
(equation f(z) = sin(z/c), z0 = c )
My code (from Rosetta Code):
public class MandelbrotSet extends JFrame {
private static final long serialVersionUID = 5513426498262284949L;
private final int MAX_ITER = 570;
private final double ZOOM = 150;
private BufferedImage image;
private double zx, zy, cX, cY, tmp;
public MandelbrotSet() {
super("Mandelbrot Set");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
zx = zy = 0;
cX = (x - 400) / ZOOM;
cY = (y - 300) / ZOOM;
int iter = MAX_ITER;
while (zx * zx + zy * zy < 4 && iter > 0) {
tmp = zx * zx - zy * zy + cX;
zy = 2.0 * zx * zy + cY;
zx = tmp;
iter--;
}
image.setRGB(x, y, iter | (iter << 8));
}
}
}
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
new MandelbrotSet().setVisible(true);;
}
}
By trigonometric theorems
sin(A+i*B)=sin(A)*cos(i*B)+ cos(A)*sin(i*B)
=sin(A)*cosh(B )+i*cos(A)*sinh(B )
and for the quotient using z=x+i*y and c=a+i*b
(x+i*y)/(a+i*b)=(x+i*y)*(a-i*b)/(a*a+b*b)
so that for the sine expression above
A = (a*x+b*y)/(a*a+b*b)
B = (a*y-b*x)/(a*a+b*b)
In javascript a small script to generate this fractal can look like this:
function cosh(x) { return 0.5*(Math.exp(x)+Math.exp(-x)); }
function sinh(x) { return 0.5*(Math.exp(x)-Math.exp(-x)); }
function rgb(r,g,b) { return "rgb("+r+","+g+","+b+")"; }
var colors = new Array(24);
for(var k=0; k<8; k++) {
colors[ k] = rgb(k*64,(7-k)*64,(7-k)*64);
colors[ 8+k] = rgb((7-k)*64,k*64,(7-k)*64);
colors[16+k] = rgb((7-k)*64,(7-k)*64,k*64);
}
var cvs = document.getElementById('sine-fractal');
var ctx = cvs.getContext('2d');
var cx = 0.0, cy = 0.0;
var dx = 1.0;
var tiles = 100;
var scale = Math.min(cvs.width, cvs.height) / tiles;
ctx.scale(scale, scale);
function localx(i) { return cx-dx + 2*i*dx/tiles; }
function localy(j) { return cy-dx + 2*j*dx/tiles; }
for (var i = 0; i < tiles; i++) {
var a = localx(i);
for (var j = 0; j < tiles; j++) {
var b = localy(j);
var r2 = a*a + b*b;
var x = a, y = b;
var rounds = 0;
var max = 500;
while (x * x + y * y < 4 && rounds < max) {
var u = (a*x + b*y) / r2, v = (a*y - b*x) / r2;
x = Math.sin(u) * cosh(v);
y = Math.cos(u) * sinh(v);
rounds++;
}
ctx.fillStyle = colors[rounds % 24];
ctx.fillRect(i, j, 1, 1);
}
}
<canvas id='sine-fractal' width=200 height=200></canvas>

JavaCV Perspective Correction

I converted the perspective correction code implemented using OpenCV and C++ at:
https://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/
to obtain the following OpenCV code implemented in Java:
public class project
{
static Point2f center;
public static void main(String args[])
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
center = new Point2f(0,0);
Mat src = new Mat();
src = Highgui.imread("image.jpg");
if(src == null)
{
System.out.println("Image not loaded");
System.exit(1);
}
Mat bw = new Mat();
Imgproc.cvtColor(src, bw, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(bw, bw, new Size(3,3));
Imgproc.Canny(bw, bw, 100, 100, 3,true);
Mat lines = new Mat();
int threshold = 70;
int minLineSize = 30;
int lineGap = 10;
Imgproc.HoughLinesP(bw, lines, 1, Math.PI / 180, threshold,
minLineSize, lineGap);
for (int x = 0; x < lines.cols(); x++)
{
double[] vec = lines.get(0, x);
double[] val = new double[4];
val[0] = 0;
val[1] = ((float) vec[1] - vec[3]) / (vec[0] - vec[2]) * -vec[0] + vec[1];
val[2] = src.cols();
val[3] = ((float) vec[1] - vec[3]) / (vec[0] - vec[2]) * (src.cols() - vec[2]) + vec[3];
lines.put(0, x, val);
}
List<Point2f> corners = new ArrayList<Point2f>();
for (int i = 0; i < lines.cols(); i++)
{
for (int j = i+1; j < lines.cols(); j++)
{
Mat m1 = null,m2 = null;
double[] d1 = lines.get(0,i);
double[] d2 = lines.get(0, j);
m1.put(0, i, d1);
m2.put(0, j, d2);
Point2f pt = computeIntersect(m1, m2);
if (pt.x >= 0 && pt.y >= 0)
corners.add(pt);
}
}
List<Point2f> approx = new ArrayList<Point2f>();
List<Point2f> curve;
MatOfPoint2f mat2f = new MatOfPoint2f();
for(int k=0;k<corners.size();++k)
{
Point2f rec = corners.get(k);
Point p = new Point(rec.x,rec.y);
mat2f.fromArray(p);
}
MatOfPoint2f mat2frec = new MatOfPoint2f();
Imgproc.approxPolyDP(mat2f, mat2frec, Imgproc.arcLength(mat2f, true) * 0.02,true);
if (approx.size() != 4)
{
System.out.println("The object is not quadrilateral!");
}
// Get mass center
for (int i = 0; i < corners.size(); i++)
{
center.x = center.x + corners.get(i).x;
center.y = center.y + corners.get(i).y;
}
center.x *= (1. / corners.size());
center.y *= (1. / corners.size());
sortCorners(corners, center);
Mat dst = src.clone();
// Draw lines
for (int i = 0; i < lines.cols(); i++)
{
double[] v = lines.get(0, i);
Scalar cc = new Scalar(0,255,0,0);
Core.line(dst, new Point(v[0], v[1]), new Point(v[2], v[3]), cc);
}
Scalar c1 = new Scalar(0,0,255,0);
Scalar c2 = new Scalar(0,255,0,0);
Scalar c3 = new Scalar(255,0,0,0);
Scalar c4 = new Scalar(255,255,255,0);
// Draw corner points
Core.circle(dst, new Point(corners.get(0).x,corners.get(0).y), 3, c1, 2);
Core.circle(dst, new Point(corners.get(1).x,corners.get(1).y), 3, c2, 2);
Core.circle(dst, new Point(corners.get(2).x,corners.get(2).y), 3, c3, 2);
Core.circle(dst, new Point(corners.get(3).x,corners.get(3).y), 3, c4, 2);
Scalar c5 = new Scalar(0,255,255,0);
// Draw mass center
Core.circle(dst, new Point(center.x,center.y), 3, c5, 2);
Mat quad = Mat.zeros(300, 220, CvType.CV_8UC3);
List<Point2f> quad_pts = new ArrayList<Point2f>();
quad_pts.add(new Point2f(0, 0));
quad_pts.add(new Point2f(quad.cols(), 0));
quad_pts.add(new Point2f(quad.cols(), quad.rows()));
quad_pts.add(new Point2f(0, quad.rows()));
Mat transmtx = Imgproc.getPerspectiveTransform((Mat) corners, (Mat) quad_pts);
Imgproc.warpPerspective(src, quad, transmtx, quad.size());
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", dst, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try
{
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
File outputfile = new File("Image.jpg");
ImageIO.write(bufImage, "jpg", outputfile);
}
catch (Exception e) {
e.printStackTrace();
}
MatOfByte matOfByte2 = new MatOfByte();
Highgui.imencode(".jpg", dst, matOfByte2);
byte[] byteArray2 = matOfByte2.toArray();
BufferedImage bufImage2 = null;
try
{
InputStream in = new ByteArrayInputStream(byteArray2);
bufImage2 = ImageIO.read(in);
File outputfile2 = new File("Quadrilateral.jpg");
ImageIO.write(bufImage, "jpg", outputfile2);
}
catch (Exception e) {
e.printStackTrace();
}
}
static Point2f computeIntersect(Mat es, Mat es2)
{
int size = (int) es.total() * es.channels();
float[] buff = new float[size];
es.get(0, 0, buff);
int size1 = (int) es.total() * es.channels();
float[] buff1 = new float[size1];
es.get(0, 0, buff1);
float x1=buff[0], y1 = buff[1], x2 = buff[2], y2 = buff[3];
float x3 = buff1[0], y3 = buff1[1], x4 = buff1[2], y4 = buff1[3];
float denom;
float d;
d = (Float) null;
d = (float)((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
if (d != (Float) null)
{
Point2f pt = new Point2f();
pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
return pt;
}
else
return new Point2f(-1, -1);
}
static void sortCorners(List<Point2f> corners,Point2f center)
{
List<Point2f> top = null, bot = null;
for (int i = 0; i < corners.size(); i++)
{
if (corners.get(i).y < center.y)
top.add(corners.get(i));
else
bot.add(corners.get(i));
}
Point2f tl = top.get(0).x > top.get(1).x ? top.get(1) : top.get(0);
Point2f tr = top.get(0).x > top.get(1).x ? top.get(0) : top.get(1);
Point2f bl = bot.get(0).x > bot.get(1).x ? bot.get(1) : bot.get(0);
Point2f br = bot.get(0).x > bot.get(1).x ? bot.get(0) : bot.get(1);
corners.clear();
corners.add(tl);
corners.add(tr);
corners.add(br);
corners.add(bl);
}
}
I'm having trouble converting List< Point2f > to MatOfPoint2f. The arcLength(..) function is therefore not working and the code doesn't seem to work. I'm hoping someone can help.
This a part of the implementation that i used in my project.I already had the exact corner points using an algo i developed but the rest is given in this code.Do not use point2fs. Use point arrays and them convert them into matofpoint2fs.
the jarfile containing Imshow can be downloaded from here.It is very effective in testing your o/p at any point of time. Add this package to your program: https://github.com/master-atul/ImShow-Java-OpenCV
Details regarding approxpolydp:
http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#approxPolyDP%28org.opencv.core.MatOfPoint2f,org.opencv.core.MatOfPoint2f,double,boolean%29
And u don't have to use arclength. Just give an approx value for epsilon depending on the clarity of your input.(like 2.0 or 3.0..)
(sort is the function used to sort the corners).
int a[][],imgarr[][];
Point p[];
BufferedImage img;
int w,h;
void sort()
{
int x = (a[0][0] + a[1][0] + a[2][0] + a[3][0])/4;
int y = (a[0][1] + a[1][1] + a[2][1] + a[3][1])/4;
int j = 0;
int order[] = new int[4];
double tans[] = new double[4];
double tans1[] = new double[4];
int tmpar[][] = new int[4][2];
p = new Point[4];
for(int i = 0;i<4;i++)
{
tans1[i] = tans[i] = Math.atan2(a[i][1] - y , a[i][0] - x);//finding angles for sorting corners
}
Arrays.sort(tans1);
for(int i = 0;i<2;i++)
{
double temp = tans1[i];
tans1[i]= tans1[3-i];
tans1[3-i] = temp;
}
for(int i=0;i<4;i++)
{
for(j = 0;j<4;j++)
{
if(tans1[i]==tans[j])
break;
}
order[i] = j;
}
for(int i = 0;i<4;i++)
{
for(j=0;j<2;j++)
{
tmpar[i][j] = a[i][j];
}
}
for(int i = 0;i<4;i++)
{
for(j = 0;j<2;j++)
{
a[i][j] = tmpar[order[i]][j];
}
}
p[0] = new Point(a[0][1],a[0][0]);
p[1] = new Point(a[1][1],a[1][0]);
p[2] = new Point(a[2][1],a[2][0]);
p[3] = new Point(a[3][1],a[3][0]);
}
void transform() throws Exception
{
Point farray[] = new Point[4];
try
{
img = ImageIO.read(new File("C:/Users/Documents/a.jpg"));
}
catch(Exception r)
{
System.out.println("no file");
}
PixelGrabber pg;
if(img==null)
{
return;
}
w = img.getWidth();
h = img.getHeight();
imgarr = new int[h][w];
try
{
for(int i = 0; i < h ; i++)
{
pg = new PixelGrabber(img,0,i,w,1,imgarr[i],0,w);
pg.grabPixels();
}
changeto256();
}
catch(Exception e)
{
System.out.println("here "+e);
}
int m=0;
byte array[] = new byte[w*h];
int iar[] = new int[w*h];
for(int i = 0 ; i < h ; i++)
{
for(int j = 0 ; j < w ; j++)
{
array[m++]= (byte)imgarr[i][j];
}
}
farray[3] = new Point(0,0);
farray[0] = new Point(w,0);
farray[1] = new Point(w,h);
farray[2] = new Point(0,h);
Mat mat = new Mat(h,w, CvType.CV_8U);
mat.put(0, 0, array);
Imshow is = new Imshow("try");
MatOfPoint2f quad = new MatOfPoint2f(p);
MatOfPoint2f rect = new MatOfPoint2f(farray);
Mat transmtx = Imgproc.getPerspectiveTransform(quad,rect);
Mat output = new Mat(w,h,CvType.CV_8U);
Imgproc.warpPerspective(mat, output, transmtx, new size(w,h),Imgproc.INTER_CUBIC);
is.showImage(output);
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", output, matOfByte);
byte[] byteArray = matOfByte.toArray();
File f = new File("retrieve1.jpg");
BufferedImage img1 =null;
InputStream in = new ByteArrayInputStream(byteArray);
img1 = ImageIO.read(in);
WritableRaster raster = (WritableRaster)img1.getData();
raster.setDataElements(0,0,byteArray);
img1.setData(raster);
try
{
ImageIO.write(img1,"jpg",f);
}
catch(Exception e)
{}
}

java mandelbrot set moving wrong

so I've been trying to program the mandelbrot set in java, I know the code isn't very optimized but i've just started out doing this.
The idea is that when i click a pixel it will put that pixel in the center (that represents a certain complex number) and calculate the set around it. This works at the beginning but if i zoom in it will start to behave weird. I'm guessing it's either my midX, midY or the display function that's weird but i've been looking at it for a long time and can't figure it out, would appreciate some help.
class set {
DotWindow w;
int[][] arrayColor;
int max = 100;
Grayscale gray;
double zoom = 1.1;
double midX = -0.5;
double midY = 0;
public static void main(String[] args) {
new set().run();
}
void run() {
setup();
runLoop();
}
void runLoop() {
int x;
int y;
while (true) {
GameEvent event = w.getNextEvent();
switch (event.getKind()) {
case GameEvent.KEY_PRESSED:
int key = event.getKey();
if (key == 43) {
zoom = zoom * 1.1;
} else if (key == 45) {
zoom = zoom / 1.1;
}
display();
break;
case GameEvent.MOUSE_CLICKED:
midX = midX - (1 - event.getX() / 250.0);
midY = midY - (1 - event.getY() / 250.0);
System.out.println(midX);
display();
break;
}
}
}
void setup() {
w = new DotWindow(500, 500, 1);
w.checkMouse(true, false, false, false, false);
w.checkKeys(true, false, false);
arrayColor = new int[500][500];
zoom = zoom / 1.1;
display();
}
int calculate(double re, double im) {
double Zre = 0;
double Zim = 0;
double Zim2 = 0;
double Zre2 = 0;
int iterations = 0;
for (int k = 0; k < max; k++) {
if (Zre2 + Zim2 > 4.0) {
return k;
}
Zim2 = Zim * Zim;
Zre2 = Zre * Zre;
Zim = 2.0 * Zre * Zim + im;
Zre = Zre2 - Zim2 + re;
iterations = k;
}
return iterations;
}
void display() {
for (double y = 0; y < 500; y++) {
for (double x = 0; x < 500; x++) {
double value = calculate((midX - (1 - x / 250) / zoom),
(midY - (1 - y / 250) / zoom));
if (value == 99) {
w.setDot((int) x, (int) y, Color.BLACK);
} else {
w.setDot((int) x, (int) y, gray = new Grayscale(
255 - (int) value * 2));
}
}
}
}
}
case GameEvent.MOUSE_CLICKED:
midX = midX - (1 - event.getX() / 250.0)/zoom;
midY = midY - (1 - event.getY() / 250.0)/zoom;
System.out.println(midX);
display();
You've already zoomed in, say to 10x, but you change central coordinates not regarding the zoom value.

Categories