I wanted to write sieve of Eratosthenes which will work using specific number of threads. I figured out, that it will work in following way:
For 2 threads up to 17.
Thread-1 takes 2, and starts to remove multiple of 2 from List. Parallel Thread-2 takes 3 and does the same. After that Thread-1 takes 5( because there is no 4 in List) and Thread-2 takes 7 and so on until they reach end.
I wrote following piece of code:
private List<Integer> array = new ArrayList<Integer>();
private List<Integer> results = new ArrayList<Integer>();
public synchronized void run(){
while(array.size() > 0){
Integer tmp = array.get(0);
for(int i = 1; i < array.size(); i++){
if( (array.get(i).intValue() % tmp.intValue()) == 0)
array.remove(i);
}
results.add(array.get(0));
array.remove(0);
}
}
public void setArray(int x){
for(int i = 2; i < x; i++)
array.add(Integer.valueOf(i));
}
public void printArray(){
for(Integer i: results){
System.out.println(i);
}
}
This code works, but I added time measurement "tool" to my main class:
ThreadTask task = new ThreadTask();
task.setArray(5000);
Long beg = new Date().getTime();
for(int i = 0; i < 3;i++){
new Thread(task).start();
}
Long sleep = 1000L;
Thread.sleep(sleep);// I am sleeping main thread to wait until other Threads are done
task.printArray();
System.out.println("Time is "+(new Date().getTime()-beg-sleep));
The problem is that running this with 2 threads is slower than running with 1 thread, and 3 threads are slower than 2 threads. Could anyone explain me, why?
EDIT:
There is one important thing about that. I don't need it to be done as fast as it can be. I need it working on Threads for one reason. My Teacher wants to compare runtimes of running same program with 1, 2 .. n threads. Results should look like in this graph.
EDIT2:
I have rewritten code to following
private HashMap<Integer,Boolean> array = new HashMap<Integer,Boolean>();
private int counter = 1;
private int x;
public void run(){
while(counter < x-1){
do{
counter++;
}
while( array.get(counter));
int tmp = counter;
for(int i = tmp; i < array.size(); i+=tmp){
if( i!= tmp)
array.put(i,true);
}
try{
Thread.sleep(0L, 1);
}
catch (Exception e){}
}
}
public void setArray(int x){
this.x = x;
for(int i = 2; i < x; i++)
array.put(i, false);
}
public void printArray(){
for(int i = 2; i < array.size();i++){
if( !array.get(i))
System.out.println(i);
}
}
Now it uses HashMap and this is how it works:
Fill HashMap with keys from 2 to n and false values.
New thread goes into while loop which is based on counter variable. Counter represents current key.
Increment counter on the begging so new threads doesn't operate on counter of earlier started thread.
Put counter value into temporary variable tmp so we can work even when another thread increment counter
Iterate through the HashMap by incrementing i with tmp ( it is actually jumping on the multiplies of i) and set their values to true.
All keys which has true value are ignored in print method. Also counter skips them when incremented.
The problem is that it still works slower with more threads. What's wrong now?
The mistake is simpler than I first thought. All your threads are doing the same thing so each thread does more work. To make a multi-threaded program work faster you have to divide up the work, which has to be performed concurrently.
When you have one thread accessing a data structure, it can be in the fastest cache of one core, use multiple threads and they need to co-ordinate their actions and since most of the work is updating the data structure, a lot of time is lost as overhead. This is the case even though your data structure is not thread safe and is likely to have a corrupted result.
BTW updating an ArrayList is very expensive and using a collection objects is also an overhead.
You will get a much faster result using a BitSet and just one thread.
public class BitSetSieveMain {
private final BitSet set;
private final int size;
public BitSetSieveMain(int x) {
size = x + 1;
set = new BitSet(size);
set.flip(2, size);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
BitSetSieveMain bitSetSieveMain = new BitSetSieveMain(5000);
bitSetSieveMain.sieve();
long time = System.nanoTime() - start;
System.out.println(time / 1000 + " micro-seconds to perform " + bitSetSieveMain);
}
}
public void sieve() {
int i = 2;
do {
for (int j = i*2; j < size; j += i)
set.clear(j);
i = set.nextSetBit(i+1);
} while (i > 0);
}
public String toString() {
return set.toString();
}
}
finally prints
87 micro-seconds to perform {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999}
I wrote a price calculator for a friend (runescape dungeoneering, but that's not particularly important). He's part of a fairly large group that does this, and one of the other admins is having troubles with it.
I wrote this in Eclipse on Windows 7, Java 6u32 (I believe it was u32). He's running Mac OS X version 10.7.4, with Java 6u33. Whenever he loads the page, the text fields don't display properly. Half of them are shortened with ellipses at the end of the line.
Picture of the problem:
http://puu.sh/KckA
Also, as a side question, I was wondering why the image only shows up after it paints once?
Thanks for all your help, and sorry about the badly named variables! I wasn't able to figure out how to embed code properly into the post, so I realize this is badly space - I just copy/pasted it over and used space to indent the imports :/.
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class IHideKeysCalc extends JApplet implements ActionListener {
JRadioButton t = new JRadioButton("Get a chaotic at your current level."), z = new JRadioButton("Get to a certain level.");
JRadioButton t2 = new JRadioButton("Yes."), z2 = new JRadioButton("No.");
JTextField startLevel = new JTextField(), endLevel = new JTextField();
JLabel e = new JLabel("What is your ending level?"), p = new JLabel("What is your starting level?");
JLabel cost = new JLabel("It will cost: 0m");
JLabel chaotics = new JLabel("You will gain: 0 chaotics");
JLabel time = new JLabel("It will take: 0 hours");
JButton calc = new JButton("Calculate!");
JLabel leech = new JLabel("Will you leech c1s from us?");
Color dots = new Color(215, 215, 215);
double[] normalCost = new double[] {0.147408147408147, 0.161616161616162, 0.156052782558807, 0.171352074966533, 0.166624674561182, 0.18543713781809, 0.180714893457874, 0.201060278814058, 0.206251654890812, 0.227434257285004, 0.228290358693966, 0.251523448295564, 0.253108393640709, 0.280820991484583, 0.28508229125052, 0.314016016332662, 0.321855573638658, 0.354987765042637, 0.365389331821599, 0.402884011746955, 0.413233529990681, 0.454829644395684, 0.469244088131267, 0.518269589876325, 0.536842873549166, 0.591842366162221, 0.617068005673544, 0.680805377374742, 0.711865189407446, 0.785616988310019, 0.824813238661612, 0.910191318818822, 0.958469472772604, 1.05785553342935, 1.11731759927444, 1.2328062178794, 1.30540086348993, 1.44105580841818, 1.52112093542576, 1.67893297878229, 1.79759198326453, 1.98403337299274, 2.0576867439263, 2.27198067413181, 2.34861652795415, 2.59287845397998, 2.67171019944355, 2.94948405053316, 2.30627996576819, 2.54621754023429, 2.63839002652197, 2.91292672667821, 3.02782375824338, 3.34294815274889, 2.99278604307127, 3.30403304019601, 3.44280379223394, 3.8011722050782, 3.35899412255012, 3.70845630500912, 3.7979745635451, 4.19312491872546, 4.11809281699623, 4.5465605645372, 4.59525960492191, 5.07347042913889, 5.11281698937835, 5.64488890579082, 5.81225689595036, 6.41724086226737, 6.46511043188491, 7.13790513866952, 7.31926635024289, 8.08104832531242, 8.18451767951496, 9.03633826548025, 9.03072096418207, 9.97065806503748, 11.6582196154413, 12.8716058180737, 13.3368037528554, 14.7248844101516, 15.2703841396691, 16.8597698035761, 17.5341941654067, 19.3591671629348, 20.1405224921476, 22.2368354307871, 23.1540859239001, 25.5640472079718, 26.7502409626548, 29.534621910156, 31.0514204050245, 34.2834446556508, 36.3576095095093, 40.1419477374292, 42.8132373341253, 47.2695988929749, 50.5171187677128, 55.775336875572, 59.0583839135564, 65.2056239687282, 69.5517856204352, 76.791366428954, 80.8126533895158, 89.2243342865382, 94.9773484573605, 104.863428916985, 112.165424237996, 123.840557129701, 130.059514288612, 143.597320738684, 154.344556268421, 170.410147105144, 178.81609217768, 197.428905849894, 209.623017271163, 231.442490011038, 248.40964248305, 2418.96525482347};
double[] normalCost2 = new double[] {0.147408147408147, 0.161616161616162, 0.156052782558807, 0.171352074966533, 0.166624674561182, 0.18543713781809, 0.180714893457874, 0.201060278814058, 0.206251654890812, 0.227434257285004, 0.228290358693966, 0.251523448295564, 0.253108393640709, 0.280820991484583, 0.28508229125052, 0.314016016332662, 0.321855573638658, 0.354987765042637, 0.365389331821599, 0.402884011746955, 0.413233529990681, 0.454829644395684, 0.469244088131267, 0.518269589876325, 0.536842873549166, 0.591842366162221, 0.617068005673544, 0.680805377374742, 0.711865189407446, 0.785616988310019, 0.824813238661612, 0.910191318818822, 0.958469472772604, 1.05785553342935, 1.11731759927444, 1.2328062178794, 1.30540086348993, 1.44105580841818, 1.52112093542576, 1.67893297878229, 1.79759198326453, 1.98403337299274, 2.0576867439263, 2.27198067413181, 2.34861652795415, 2.59287845397998, 2.67171019944355, 2.94948405053316, 2.30627996576819, 2.54621754023429, 2.63839002652197, 2.91292672667821, 3.02782375824338, 3.34294815274889, 2.99278604307127, 3.30403304019601, 3.44280379223394, 3.8011722050782, 3.35899412255012, 3.70845630500912, 3.7979745635451, 4.19312491872546, 4.11809281699623, 4.5465605645372, 4.59525960492191, 5.07347042913889, 5.11281698937835, 5.64488890579082, 5.81225689595036, 6.41724086226737, 6.46511043188491, 7.13790513866952, 7.31926635024289, 8.08104832531242, 8.18451767951496, 9.03633826548025, 9.03072096418207, 9.97065806503748, 9.16677990396157, 10.1208573381611, 10.6767809960961, 11.7880092526936, 12.4151865893764, 13.7073950498449, 14.4486866969796, 15.952517601496, 16.79338694904, 18.5413155024615, 19.5087629499962, 21.5393057908644, 22.749431329251, 25.1173757245215, 26.628575651853, 29.4002428137358, 31.4882868073428, 34.7657940225949, 37.4024999022994, 41.2956664355536, 44.4734040636548, 49.1025449226364, 52.3507084560114, 57.7997632829849, 62.0331408031167, 68.4901128512714, 72.4784892775103, 80.0226781901313, 85.613081327045, 94.52444623818, 101.572293638011, 112.144803254013, 118.272870806207, 130.583813546741, 140.900446941876, 155.566652113006, 163.822838496629, 180.875017252225, 192.680242742557, 212.736157206356, 229.030482784318, 2230.25473010176};
double[] normalTokens = new double[] {8.3, 9.1, 10.2, 11.2, 12.4, 13.8, 15.1, 16.8, 18.5, 20.4, 22.6, 24.9, 27.4, 30.4, 33.5, 36.9, 40.8, 45, 49.7, 54.8, 60.6, 66.7, 73.7, 81.4, 89.8, 99, 109.4, 120.7, 133.2, 147, 162.3, 179.1, 197.7, 218.2, 240.9, 265.8, 293.5, 324, 357.6, 394.7, 435.8, 481, 531, 586.3, 647.1, 714.4, 788.7, 870.7, 961.2, 1061.2, 1171.5, 1293.4, 1427.8, 1576.4, 1740.4, 1921.4, 2121.2, 2342, 2585.6, 2854.6, 3151.6, 3479.5, 3841.6, 4241.3, 4682.6, 5169.9, 5707.9, 6301.9, 6957.6, 7681.8, 8481.2, 9363.8, 10338.3, 11414.3, 12602.2, 13913.8, 15361.9, 16960.8, 18726, 20675, 22826.9, 25202.7, 27825.9, 30722.1, 33919.8, 37450.2, 41348.2, 45651.9, 50403.7, 55649.9, 61442.2, 67837.6, 74898.5, 82694.4, 91301.9, 100805.2, 111297.7, 122882.5, 135672.9, 149794.8, 165386.8, 182601.5, 201608.1, 222593.3, 245762.6, 271343.7, 299587.4, 330771.1, 365200.7, 403213.9, 445184, 491522.9, 542684.9, 599172.5, 661539.7, 730398.8, 806425.4, 890365.5, 983043, 9572683.4};
double[] normalTime = new double[] {1.00958241758242, 1.11495970695971, 1.06963166953528, 1.18127672595142, 1.15018594104308, 1.27023901906442, 1.2472594878124, 1.37744482155713, 1.41685187507839, 1.56473877113034, 1.56755170743479, 1.73116821561822, 1.74434730120433, 1.92641722153205, 1.95725417087426, 2.16154669376012, 2.21124718747179, 2.44205076596211, 2.50994901343847, 2.77193022359534, 2.86812333250013, 3.1674897409429, 3.29805772944222, 3.64229944530993, 3.80841107155634, 4.20592199148309, 4.42242809498221, 4.8840283338524, 5.15065412806684, 5.68826449160243, 6.02144053611861, 6.64994106345759, 7.05825151342297, 7.79497136835937, 8.29491528158907, 9.16071451973473, 9.77092350100014, 10.7907842031247, 11.311355104669, 12.4920015970756, 13.4631737052244, 14.8684207923017, 15.322810010546, 16.9221605503673, 17.602791084445, 19.4401194467945, 19.9138619998677, 21.9924132523791, 16.9295074753534, 18.6965604441613, 19.4662745138955, 21.4981078806657, 22.4508340041317, 24.794187048334, 21.9299806848883, 24.2189685678228, 25.3352524716474, 27.9796727633009, 24.6417064586782, 27.2137364257457, 27.7930719891776, 30.6940323651524, 30.0742869561431, 33.2133539448314, 33.3534941382039, 36.8348352772083, 37.0696258402337, 40.93884604578, 42.4260461563957, 46.8543540043697, 47.1239377238921, 52.0425978903589, 53.693321666918, 59.2976751068993, 60.1692757457264, 66.4495705205329, 66.7720718040988, 73.7415473122707, 76.8324023748064, 84.8519460570252, 88.1453522545292, 97.3457088871568, 101.175803906389, 111.736241351164, 116.429000968713, 128.581522955346, 133.995020100622, 147.981032299676, 154.311116147988, 170.417663624714, 178.555672255407, 197.192796295533, 207.556928886271, 229.221120116824, 239.701753082987, 264.721127983905, 278.790936846473, 307.890327561088, 325.297010290488, 359.250570287139, 376.451809570906, 415.744759391439, 439.247661643271, 485.09506067001, 506.047490207454, 558.867261912609, 590.119126007236, 651.714051617419, 691.908460702742, 764.127862324914, 796.957117808243, 880.14119407779, 939.928811085602, 1038.03585870733, 1082.69792300564, 1195.70679712407, 1262.42094024116, 1394.18878248856, 1488.70556306347, 14496.7280485445};
double[] secondCost = new double[] {205.809139511126, 205.666898683103, 205.510947895753, 205.361245312921, 205.196866006281, 205.037961182368, 204.861115491238, 204.68980138365, 204.499200257327, 204.304466121548, 204.089732263715, 203.875511955396, 203.639490465256, 203.403440451273, 203.141545545247, 202.877319302118, 202.586276067149, 202.289821292416, 201.962849114402, 201.628401442031, 201.259634189599, 200.884128316807, 200.470823998073, 200.047463215808, 199.579870710024, 199.098934496811, 198.568726422112, 198.019767421347, 197.414106074069, 196.785167022266, 196.091067618249, 195.36729726347, 194.568607981024, 193.733220320596, 192.811209255825, 191.843868432443, 190.776540948612, 189.653863969407, 188.414520387594, 187.116029983754, 185.682824831642, 184.156548244533, 182.471970368444, 180.744867551454, 178.837898847873, 176.892146461388, 174.744031015699, 172.563340671575, 170.155927101612, 168.448060074772, 166.562512333528, 164.653461422647, 162.545764924027, 160.406844613766, 158.045313603054, 156.136045952524, 153.988235498788, 151.784760223372, 149.351920748064, 147.503584655442, 145.462951839076, 143.506325452791, 141.346126978658, 139.472520504396, 137.403974289463, 135.544562182586, 133.491648277631, 131.713718729864, 129.75076664292, 127.774684202822, 125.573085489923, 123.762314431261, 121.763104645016, 120.117322794866, 118.300249603707, 116.783640524603, 114.939775999494, 113.850096472748, 112.647000728262, 110.627734605662, 107.826828419619, 105.60890512289, 103.160142677246, 101.141235060418, 98.3943606030692, 96.4191212414268, 94.1197186833565, 91.9810632039793, 89.6198071860878, 88.0967551101201, 85.6040436484927, 84.3265101874514, 82.221926362036, 80.9960672634902, 79.5357439871363, 78.2995243514211, 76.9346308757958, 75.9839103477244, 74.468989411611, 73.7032366599374, 71.4184976232158, 71.2078468065862, 68.9970151203599, 69.3621460275368, 65.7648099340712, 65.7648099340712, 63.4054359144346, 63.4054359144346, 61.4267301448194, 61.4267301448194, 58.4295546509364, 58.4295546509364, 56.8818318948696, 56.8818318948696, 54.0605778240308, 54.0605778240308, 51.9881981076397, 51.9881981076397, 50.5389169106641, 50.5389169106641};
double[] secondCost2 = new double[] {203.13233860099, 202.988993482163, 202.83183196634, 202.680772303242, 202.514902869252, 202.354348261486, 202.175666520585, 202.002343402015, 201.809507084666, 201.612311577817, 201.394863559453, 201.177636387015, 200.938302024462, 200.698606520352, 200.432666982947, 200.163983662474, 199.868030990072, 199.566147894276, 199.233188597443, 198.892128484954, 198.51607025227, 198.132501726134, 197.710323166839, 197.277156814418, 196.798734275924, 196.30585043449, 195.762470698387, 195.198956346536, 194.577236216113, 193.930575292604, 193.216917967109, 192.471554070454, 191.649036055106, 190.787344985998, 189.836303047305, 188.836911181175, 187.734219782183, 186.572493483566, 185.290042646251, 183.943974604858, 182.458255768343, 180.873997261643, 179.125423737726, 177.327672918664, 175.342698709217, 173.310851554106, 171.067687270979, 168.782062691008, 166.25880502424, 164.423053139412, 162.396315831693, 160.331400260874, 158.051620662669, 155.722735724616, 153.151469289297, 151.01064649224, 148.624436411962, 146.173777319575, 143.468024163638, 141.318387638638, 138.945107771579, 136.62122487126, 134.055559637202, 131.734290824981, 129.171505159301, 126.76642883342, 124.111065552939, 121.667993393022, 118.970679767243, 116.273634388718, 113.283811160463, 110.60278967096, 107.642766440354, 104.936178332934, 101.947891222429, 99.2288303248363, 96.1192999109482, 93.6322283836829, 90.8862970366724, 89.6688583891789, 87.9450600565222, 86.5393226499183, 84.9872775567425, 83.6651681411018, 81.8487685186973, 80.5094726227178, 78.954690408184, 77.4707819037332, 75.832421765823, 74.7640388864952, 73.0098766187729, 72.104918422045, 70.6542197712317, 69.8567106080253, 68.9065542546254, 68.1010156606756, 67.2116313316438, 66.574684853225, 65.5597456288688, 64.9942472119106, 63.3069972404224, 63.1531374790764, 61.538341766146, 61.8270638283787, 58.9825215696044, 58.9825215696044, 57.1539933435418, 57.1539933435418, 55.6254649227186, 55.6254649227186, 53.1343762606953, 53.1343762606953, 51.9271669220486, 51.9271669220486, 49.5277421737892, 49.5277421737892, 47.7862534445361, 47.7862534445361, 46.596228808774, 46.596228808774};
double[] reachLevel = new double[] {80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 86, 86, 87, 87, 88, 88, 89, 90, 90, 91, 91, 92, 93, 93, 94, 95, 96, 97, 97, 98, 99, 100, 101, 102, 103, 104, 105, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120};
double[] secondTime = new double[] {24.9946429257347, 24.9783842111745, 24.9604239225849, 24.9432943332101, 24.9243723855536, 24.9060506888359, 24.8858238572065, 24.8660689282208, 24.8442605111923, 24.8219115774579, 24.797227808174, 24.7726476204672, 24.7454977940721, 24.718299297897, 24.6882714810255, 24.6579417322399, 24.624439643112, 24.5903759437907, 24.5527527673272, 24.5143193989011, 24.4718684795311, 24.4282110190991, 24.3799813137437, 24.3300542238567, 24.2749163955096, 24.2175845383904, 24.1542567117313, 24.0880317336631, 24.0148862553222, 23.9381519219767, 23.8534012345939, 23.7641440208121, 23.6655607949053, 23.5614444982819, 23.4464515869104, 23.3246787903348, 23.1901789836552, 23.0474035039304, 22.8897163219235, 22.7256509515522, 22.544445507274, 22.3498647994608, 22.1349546550305, 21.9158909898917, 21.6739535964416, 21.4248306343984, 21.1496883589048, 20.8717319487254, 20.5647412156741, 20.3483217811742, 20.1092907297184, 19.8649748206746, 19.595132087265, 19.3186025584435, 19.0131802946162, 18.7667111995194, 18.490451007457, 18.2047319219977, 17.8891513821645, 17.6448833971303, 17.3750629489418, 17.1147038849072, 16.8271013961493, 16.5731352612704, 16.2923706355883, 16.0378836320328, 15.7567402789638, 15.5063131557712, 15.2296329829085, 14.9463360163191, 14.6310161166959, 14.3596539672288, 14.0595718337876, 13.7912768736783, 13.4947912766823, 13.2295985529344, 12.9181982399467, 12.684004188419, 12.425408200958, 12.212164466137, 11.9147945189033, 11.678465792127, 11.4174452967627, 11.202029099851, 10.9072154065515, 10.6977333721524, 10.4482346235909, 10.2179425784472, 9.96261811883693, 9.79890600858667, 9.5292281368228, 9.39100021700554, 9.13546626658899, 8.95336286950375, 8.73661022043875, 8.5538243186338, 8.35021455477117, 8.21361702837751, 7.99254171400577, 7.89238136322017, 7.58748131883126, 7.5609402291254, 7.26239303170813, 7.30945047798586, 6.86457666518511, 6.86457666518511, 6.56680908628267, 6.56680908628267, 6.31619233951591, 6.31619233951591, 5.96806504391798, 5.96806504391798, 5.77411467139106, 5.77411467139106, 5.456191060476, 5.456191060476, 5.21889040213051, 5.21889040213051, 5.04794996442499, 5.04794996442499};
private static final long serialVersionUID = 1L;
JPanel x = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image oi = getImage(getCodeBase(), "Logo.png");
g.drawImage(oi, 0, 200, 537, 93, null);
}
};
public void init() {
t.setBounds(25, 25, 220, 25);
t.setSelected(true);
t.addActionListener(this);
t.setBackground(Color.WHITE);
z.setBounds(250, 25, 150, 25);
z.addActionListener(this);
z.setBackground(Color.WHITE);
ButtonGroup group = new ButtonGroup();
group.add(t);
group.add(z);
leech.setBounds(30, 100, 160, 25);
leech.setOpaque(true);
leech.setBackground(Color.WHITE);
t2.setBounds(30, 125, 50, 25);
t2.setSelected(true);
t2.addActionListener(this);
t2.setBackground(Color.WHITE);
z2.setBounds(85, 125, 50, 25);
z2.addActionListener(this);
z2.setBackground(Color.WHITE);
ButtonGroup group2 = new ButtonGroup();
group2.add(t2);
group2.add(z2);
p.setBounds(30, 50, 160, 25);
p.setBackground(Color.WHITE);
p.setOpaque(true);
e.setBounds(195, 50, 150, 25);
e.setBackground(Color.WHITE);
e.setOpaque(true);
startLevel.setBounds(30, 75, 150, 25);
startLevel.setBackground(Color.WHITE);
startLevel.setText("1");
endLevel.setBounds(195, 75, 150, 25);
endLevel.setBackground(Color.WHITE);
endLevel.setText("1");
cost.setBounds(225, 100, 150, 25);
cost.setBackground(Color.WHITE);
cost.setOpaque(true);
chaotics.setBounds(225, 120, 160, 25);
chaotics.setBackground(Color.WHITE);
chaotics.setOpaque(true);
time.setBounds(225, 140, 150, 25);
time.setBackground(Color.WHITE);
time.setOpaque(true);
calc.setBounds(155, 175, 90, 25);
calc.setBackground(Color.WHITE);
calc.addActionListener(this);
x.setBounds(0, 0, 400, 293);
x.setVisible(true);
x.setLayout(null);
x.setBackground(Color.WHITE);
x.add(t);
x.add(z);
x.add(startLevel);
x.add(endLevel);
x.add(p);
x.add(e);
x.add(cost);
x.add(chaotics);
x.add(time);
x.add(calc);
x.add(t2);
x.add(z2);
x.add(leech);
this.add(x);
endLevel.setVisible(false);
e.setVisible(false);
calc.doClick();
this.setSize(400, 293);
this.setVisible(true);
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
#Override
public void actionPerformed(ActionEvent arg0) {
if(t.isSelected()) {
endLevel.setVisible(false);
e.setVisible(false);
if(startLevel.getText().length() < 1) {
} else {
double cost1 = 0;
double chaotic = 0;
double time = 0;
for(int i = 0; i <= normalCost.length; i++) {
if(Integer.parseInt(startLevel.getText()) == i) {
if(z2.isSelected()){
cost1 = secondCost2[i - 1];
} else {
cost1 = secondCost[i - 1];
}
time = secondTime[i - 1];
chaotic = reachLevel[i - 1];
}
}
cost.setText("It will cost: " + round(cost1, 2) + "m");
chaotics.setText("You will reach level: " + round(chaotic, 2));
this.time.setText("It will take: " + round(time, 2) + " hours");
}
x.repaint();
} else if(z.isSelected()) {
endLevel.setVisible(true);
e.setVisible(true);
if((startLevel.getText().length() < 1) || (endLevel.getText().length() < 1)) {
} else {
double cost1 = 0;
double chaotic = 0;
double time = 0;
for(int i = 0; i <= normalCost.length; i++) {
if(!((Integer.parseInt(startLevel.getText())) >= (i))) {
if(!(Integer.parseInt(endLevel.getText()) < (i))) {
if(z2.isSelected()){
cost1 = cost1 + normalCost2[i - 2];
} else {
cost1 = cost1 + normalCost[i - 2];
}
chaotic = chaotic + normalTokens[i - 2];
time = time + normalTime[i - 2];
}
}
}
cost.setText("It will cost: " + round(cost1, 2) + "m");
chaotics.setText("You will gain: " + round(chaotic / 200000, 2) + " chaotics");
this.time.setText("It will take: " + round(time / 60, 2) + " hours");
}
x.repaint();
}
}
}
To clarify some, this works as intended for other people. As far as I know, he's the only one having problems.
Your issue here is that Mac vs Windows vs Linux display Swing components differently. If you want it to look the same on all OSes you will have to use a non-default L&F.
I can see already that the guy having trouble is on Mac OSX because of how the GUI looks.