I'm making a sorting algorithm visualizer and i want the UI to update in real time to show how it works, step by step.
But the UI only updates when the sort() method is done, I want it to update when updateHeight() is done
the sort() method is initiated by an onAction
#FXML
public void sort() throws InterruptedException {
minHeight = RectHeight[0];
counter = 0;
minIndex = 0;
temp = 0;
for(int i=1;i<RectList.size();i++){
System.out.println(RectList.size());
System.out.println(counter);
sort2();
}
}
public void sort2() throws InterruptedException {
System.out.println("start sort");
System.out.println(minHeight);
System.out.println("find min");
for (int i = counter; i < (RectList.size()); i++) {
System.out.println("i= " + i);
if (RectHeight[i] < minHeight) {
minHeight = RectHeight[i];
System.out.println("minHeight= " + minHeight);
System.out.println("minIndex= " + i);
minIndex = i;
}
}
updateHeight();
Thread.sleep(500);
}
public void updateHeight() {
temp = RectHeight[counter];
RectHeight[counter] = RectHeight[minIndex];
RectList.get(counter).setHeight(RectHeight[counter]);
RectHeight[minIndex] = temp;
RectList.get(minIndex).setHeight(temp);
counter++;
minHeight = RectHeight[counter];
minIndex = counter;
}
Never sleep on the FX Application thread. That thread is responsible for rendering the UI, so by blocking it with Thread.sleep() you prevent the UI from being rendered, as you have observed.
Instead, use the Animation API. A simple Timeline should work here:
#FXML
public void sort() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.millis(500), e -> sort2()));
timeline.setCycleCount(rectList.size());
minHeight = rectHeight[0];
counter = 0;
minIndex = 0;
temp = 0;
timeline.play();
}
public void sort2() {
System.out.println("start sort");
System.out.println(minHeight);
System.out.println("find min");
for (int i = counter; i < (rectList.size()); i++) {
System.out.println("i= " + i);
if (rectHeight[i] < minHeight) {
minHeight = rectHeight[i];
System.out.println("minHeight= " + minHeight);
System.out.println("minIndex= " + i);
minIndex = i;
}
}
updateHeight();
}
public void updateHeight() {
temp = rectHeight[counter];
rectHeight[counter] = rectHeight[minIndex];
rectList.get(counter).setHeight(rectHeight[counter]);
rectHeight[minIndex] = temp;
rectList.get(minIndex).setHeight(temp);
counter++;
minHeight = rectHeight[counter];
minIndex = counter;
}
Related
So I tried creating an animation which fades a string in (from black) on screen, but when I run it, it doesn't seem to work properly. I'm not getting any errors, just a black screen (the stage is black).
Here is the code:
public static void fadeIn(String string, Text tBox) {
final IntegerProperty counter = new SimpleIntegerProperty(0);
final BoolProp firstLoop = new BoolProp(false);
Color fadeIn[] = new Color[16];
String hexVal = "";
String hash = "#";
for (int i = 0; i > 10; i += 1) {
hexVal = "";
if (i == 10)
break;
for (int c = 0; c >6; c += 1) {
hexVal += i;
}
fadeIn[i] = Color.web(hash + hexVal);
}
fadeIn[10] = Color.web("#aaaaaa");
fadeIn[11] = Color.web("#bbbbbb");
fadeIn[12] = Color.web("#cccccc");
fadeIn[13] = Color.web("#dddddd");
fadeIn[14] = Color.web("#eeeeee");
fadeIn[15] = Color.web("#ffffff");
Timeline line = new Timeline();
KeyFrame frame = new KeyFrame(Duration.seconds(0.05), event -> {
if (counter.get() == 16) {
line.stop();
} else {
if (firstLoop.get()) {
firstLoop.set(false);
tBox.setText(string);
}
tBox.setFill(fadeIn[counter.get()]);
counter.set(counter.get()+1);
}
});
line.getKeyFrames().add(frame);
line.setCycleCount(Animation.INDEFINITE);
line.play();
}
And it's being referenced as follows:
public class Tester extends Application {
public static void main(String args[]) {
launch(args);
}
#Override public void start(Stage stage) {
VBox box = new VBox();
Text text = new Text();
box.getChildren().addAll(text);
stage.setScene(new Scene(box, 500, 500, Color.BLACK));
stage.show();
Animations.fadeIn("Testing 123", text);
}
}
The class BoolProp is as follows (I know one already exists, but I didn't have access to the documentation when I was writing the method.):
public class BoolProp {
private boolean val;
public BoolProp() {
val = false;
}
public BoolProp(boolean val) {
this.val = val;
}
public boolean get() {
return val;
}
public void set(boolean val) {
this.val = val;
}
}
There are multiple issues in your code. Lets get to them one by one :
The for loops inside the fadeIn() are incorrect.
Code:
for (int i = 0; i > 10; i += 1)
This loop will never execute since the condition will always be false. What you are looking for is :
for (int i = 0; i < 10; i++)
Similarly, fix the other loop as well.
Inside fadeIn(), you initialize the firstLoop to false and then inside the KeyFrame constructor, you try to check if the value is true, which leads to never setting the text on the Text.
There are few other issues with initializing of String, which we can overlook for now.
If you fix 1 and 2, you should be good to have a running application.
I'm trying to build simple multithreading application. But I'm confused about Java monitors. I have many threads that want to format with their data one array. So for example I have Supermarket Threads (data of the thread is in txt file) So first thread have these product (Milk, Cheese, Chocolate) and country code for each product 1,2, 3
SupermarketA
Milk 1
Cheese 2
Chocolate 3
SupermarketB
Yogurt 1
Orangle 2
Bannana 3
Tea 7
Kiwi 9
and I want to format array that has to fields (country_code and count)
So my array should look like that
Country_code count
1 2
2 2
3 2
7 1
9 1
Code
public class SortedArray{
private int num = 0; // num is country code
private int count = 0;
}
So here's my monitor class
public class SingleArray {
private SortedArray[] array;
private int arrayIndex;
private static final int MAX_SIZE = 5;
public SingleArray() {
array = new SortedArray[MAX_SIZE];
arrayIndex = 0;
initArray();
}
private void initArray() {
for (int i = 0; i < MAX_SIZE; i++) {
array[i] = new SortedArray();
}
}
public synchronized void inc(){
awaitUnderMax();
notifyAll();
}
private void awaitUnderMin(){
while (arrayIndex == 0) try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void dec(){
awaitUnderMin();
notifyAll();
}
public void add(ArrayList<Integer> count){
for (int i = 0; i < count.size(); i++) {
singleArray.inc();
int num = count.get(i);
if (singleArray.arrayIndex == 0) { // if array is empty add value to it
singleArray.array[0].num = num;
singleArray.array[0].count++;
singleArray.arrayIndex++;
} else {
if (!isThere(num)) { // if num is a new value to array
singleArray.inc();
int index1 = singleArray.arrayIndex;
if (num > singleArray.array[index1 - 1].num) {
singleArray.inc();
singleArray.array[index1].num = num;
singleArray.inc();
singleArray.array[index1].count++;
singleArray.inc();
singleArray.arrayIndex++;
System.out.println(Thread.currentThread().getName() + " first " + singleArray.array[index1].num);
} else if (num < singleArray.array[index1 - 1].num) { // jei num mazesne uz paskutinia masyvo reiksme
int index = index1 - 1 < 0 ? index1 : index1 - 1;
while (index > 0 && num < singleArray.array[index].num) {
index--;
}
if (index != singleArray.arrayIndex) {
System.out.println(Thread.currentThread().getName() + " sec " + singleArray.array[index].num);
singleArray.array = addPos(singleArray.array, index + 1, num);
}
}
}
}
}
}
public boolean isThere(int number){
for(int i=0; i<singleArray.arrayIndex; i++){
singleArray.inc();
if(number == singleArray.array[i].num){
singleArray.array[i].count++;
return true;
}
}
return false;
}
private void awaitUnderMax(){
while (arrayIndex >= MAX_SIZE) try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void removeValue(int number, int howManyItems){
for(int i=0; i<arrayIndex; i++){
dec();
if(number == array[i].num){
int numberToDelete = array[i].count - howManyItems >= 0 ? howManyItems : array[i].count;
if(array[i].count >= numberToDelete){
array[i].count -= numberToDelete;
}
if(array[i].count == 0){
deleteItem(i);
}
}
if(array[i].count == 0){
deleteItem(i);
}
}
}
Each thread call add(ArrayList<Integer> count) method
So basically what add method does:
Find place where to insert new value (dependng if new value is greater or lower than a previous)
call isThere(int num) method that check if new value is already in array (if so increment count singleArray.array[i].count++) otherwise add new value to array
If array is full arrayIndex == MAX_SIZE wait current thread for other threads to decrement arrayIndex (this is oly one part of code I also have other threads that based on county code decrement array)
So the biggest problem is that multiplethreads need to update single array at the same time (I know that adding synchronized keyword to add method should solve this problem but it only let one thread to run this method at once!) So sometimes all works fine, but sometimes I get really starnge results (for example that country code is 0 (That is imposible!!!) and sometimes new values is placed in wrong array posiitons). Also I think that semaphores should solve this problem, but is it possible to do that with monitors? Thank's for the answers.
EDIT v2
to #Elyasin
public Thread[] setUpShopsBuilderThreads(){
int size = data.getSize();
ArrayList<ArrayList<String>> a = new ArrayList<>();
ArrayList<ArrayList<Integer>> b = new ArrayList<>();
ArrayList<ArrayList<Double>> c = new ArrayList<>();
Thread[] threads = new Thread[size];
for (int i = 0; i < size; i++) {
int tmp = data.getIndex(i);
int range = i + 1 < size ? data.getIndex(i + 1) : data.getWaresSize();
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> count = new ArrayList<>();
ArrayList<Double> price = new ArrayList<>();
for (int j = tmp; j < range; j++) {
name.add(data.getName(j));
count.add(data.getCount(j));
price.add(data.getPrice(j));
}
a.add(name);
b.add(count);
c.add(price);
}
procesas_1 p1 = new procesas_1(a.get(0), b.get(0), c.get(0));
procesas_2 p2 = new procesas_2(a.get(1), b.get(1), c.get(1));
procesas_3 p3 = new procesas_3(a.get(2), b.get(2), c.get(2));
procesas_4 p4 = new procesas_4(a.get(3), b.get(3), c.get(3));
procesas_5 p5 = new procesas_5(a.get(4), b.get(4), c.get(4));
Thread worker1 = new Thread(p1);
Thread worker2 = new Thread(p2);
Thread worker3 = new Thread(p3);
Thread worker4 = new Thread(p4);
Thread worker5 = new Thread(p5);
threads[0] = worker1;
threads[1] = worker2;
threads[2] = worker3;
threads[3] = worker4;
threads[4] = worker5;
return threads;
}
public static void main(String[] args) {
Starter start = new Starter();
start.read();
start.printShopsData();
start.printUserData();
Thread[] builderThreads = start.setUpShopsBuilderThreads();
for(int i=0; i<builderThreads.length; i++){
builderThreads[i].start();
}
}
what about using the concurrent safe datasets java already provides?
if you want it sorted, this one looks it might work for you:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html
just add it as in a normal Collection
i got 4 threads running and ech run increases the size of a global arraylist.But i cant access the resulting arraylist after the threads are finished. Is there a way to get access to the resulting arraylist..or how does it work with threads and resutling datastructures??
List<MyObject> head = a.subList(0, 2000);
List<MyObject> body = a.subList(2001, 5000);
List<MyObject> body2 = a.subList(5001, 8000);
List<MyObject> tail = a.subList(8001, a.size());
//System.out.println(tail.size());
createAndRunFirst(head);
createAndRunFirst(body);
these are the calls
and this is the method which is called 4 times..and i need the currCDO arrylist
public void algo(List<MyObject>list){
MyObject a = null;
MyObject b = null;
int e=0;
String curr1="";
String curr2="";
for (int i = 0; i < list.size(); i++) {
a =list.get(i);
curr1= a.getStreetName();
if(curr1.contains("-")){
curr1=curr1.replace("-", " ");
}
if(curr1.contains("STRASSE")){
curr1=curr1.replace("STRASSE", "STR.");
}
else{
curr1=a.getStreetName();
}
for (int j = 0; j < lotse.size(); j++) {
b = lotse.get(j);
curr2=b.getStreetName();
if(curr2.contains("-")){
curr2=curr2.replace("-", " ");
}
if(curr2.contains("STRASSE")){
curr2=curr2.replace("STRASSE", "STR.");
}
else{
curr2=b.getStreetName();
}
int d = dL.execute(curr1,curr2);
if(curr1.length()==curr2.length()){
e=0;
}
if(curr1.length()< curr2.length()){
e=(curr2.length()*30)/100;
//System.out.println(d);
}
if(curr1.length()> curr2.length()){
e=(curr1.length()*30)/100;
//System.out.println(d);
}
if(d<e && a.getPcode().contains(b.getPcode())){
int x=a.getInstituteName().length();
int y=b.getInstituteName().length();
if(x<y){
currCDO.add(a);
}
if(y<x){
currCDO.add(a);
if(x==y){
currCDO.add(a);
}
break;}else{
//System.out.println(a.getInstituteName()+"******");
restCDO.add(a);
}
}//System.out.println(currCDO.size() +"*****");
}
}
}
public void createAndRunFirst(final List<CrawlerDataObject> list) {
Thread thread = new Thread(new Runnable() {
#Override
public void run(){
algo(list);
}
});
thread.start();
}
Assuming currCDO is global, are you waiting for all the threads to finish?
You might want to consider building an output list for each thread and then concactenating them at the end. This would avoid locking. You could use a threadsafe list too, but that might be slower. I don't know c# specifically, but I assume there are threadsafe lists.
int i0 = 0;
int c = a.size()/cThread;
new List<MyObject>()[cThread];
for (int iThread=0 ; iThread<cThread ; ++iThread, i0+=c) {
createAndRunFirst(a.subList(i0, i0 + c - 1), aOut[iThread]);
}
waitForAllThreads(); // i don't know the c# for this
for (int iThread=0 ; iThread<cThread ; ++iThread) {
currCDO += aOut[iThread];
}
public void createAndRunFirst(final List<CrawlerDataObject> list, List<CrawlerDataObject> out) {
Thread thread = new Thread(new Runnable() {
#Override
public void run(){
algo(list, out);
}
});
public void algo(List<MyObject> list, List<MyObject> currCDO) {
}
there are 2 part . I am use google app engine java.
1, task Queue, to start 2 process
2, each process using ThreadManager.createThreadForCurrentRequest(new Runnable() {
for 2 Thread
I expect to set the "total_i" value by Thread based on (int i_from, int i_to).
when pass value (1,2) and (3,4) to each Thread, the total of total_i should be 6 and 14.
But the 2 Threads give me the same value 14. I really confused and need help.
Thanks
//part 1 :
total_count = 4; // temp set
record_count= 2;
Queue queue = QueueFactory.getDefaultQueue();
// queue 分段读取 detail data, 每次30个
for (int i = 1; i <= total_count; i += record_count) {
code_from = String.valueOf(i);
code_to = String.valueOf(i + record_count - 1);
queue.add(TaskOptions.Builder.withUrl("/test_DetailDown").method(TaskOptions.Method.GET).param("from", code_from).param("to", code_to));
}
//part 2:
#SuppressWarnings("serial")
public class test_DetailDown extends HttpServlet {
AtomicInteger counter = new AtomicInteger();
final static int ThreadCount = 2;
int[] recordArr = new int[ThreadCount];
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, DeadlineExceededException {
resp.getWriter().println("start");
// this is called by "Queue_Detail_Down" from Queue with parameter : from,
// to
String code_from = req.getParameter("from");
String code_to = req.getParameter("to");
TodayDetail(Integer.valueOf(code_from), Integer.valueOf(code_to));
// process to down data
}
// this is the process to get today datail
private void TodayDetail(int code_from, int code_to) {
int total_i = 0;
// int record_count = 2;
// loop symbol to get detail data from stock.zaobao
for (int i = 0; i < ThreadCount; i++) {
final int i_from = code_from; // pass parameter
final int i_to = code_to;
// thread
Thread thread = ThreadManager.createThreadForCurrentRequest(new Runnable() {
public void run() {
counter.incrementAndGet();
get_detail_data(i_from, i_to); // down web data
counter.decrementAndGet();
}
});
thread.start(); // end thread process
}
// wait all thread
while (true) {
int num = counter.get();
if (num <= 0)
break;
try {
Thread.sleep(300);
} catch (InterruptedException ex) {
}
}
// save data
for (int i = 0; i < ThreadCount; i++) {
total_i = total_i + recordArr[i];
}
System.out.println("after Thread process, total_i =" + total_i);
log("end of program");
}
// core process, get data
private void get_detail_data(int i_from, int i_to) {
for (int i = 0; i < ThreadCount; i++) {
recordArr[i] = i_from + i_to;
}
}
}
i'm posting a simple game code, written in java. It took my hours today to write this but now i'm stuck! The problem is, in the second level of this game (after failure or success) i can't see the red tiles. It works only the first time.
games logic:
it starts up with a 3x3 matrix and rebound the dimension of this matrix in case of success (testing the ability of memory, memorising the coordinates of red tiles in 1200 ms). So we show the red tiles first, than we check the estimations. If there became a wrong try, failure!. If it is a bigger matrix than 3x3, it gets smaller.
the code is a little bit long but just one class, so it is so easy yo execute it. If you have time i would be appreciate.
So there it goes:
package skeleton;
public class Memory extends JFrame implements ActionListener {
private static final long serialVersionUID = 5963518754230629235L;
private static int minDimX = 3, minDimY = 3;
private static int maxDimX = 15, maxDimY = 15;
private static int counter = 13;
private static int memoriseTime = 1200; // milliseconds
private static int memGridWidthX = 60;
private static int memGridWidthY = 60;
private JPanel centerPanel;
private JButton memTile;
private Random generator;
private int memGridDimX, memGridDimY, coef, numberOfMemTilesToGuess;
int[][] memTileCoordinates;
int[] randomNums;
Border grayBorder = LineBorder.createGrayLineBorder();
public Memory(int xDim, int yDim) {
waitALittleBit(1300);
memGridDimX = xDim;
memGridDimY = yDim;
coef = 3;
numberOfMemTilesToGuess = memGridDimX*memGridDimY / coef;
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(memGridDimY, memGridDimX, 0, 0));
add(centerPanel, BorderLayout.CENTER);
System.out.println("int[" + memGridDimX + "][" + memGridDimY + "] array is created ");
randomNums = new int[numberOfMemTilesToGuess];
for (int i = 0 ; i < numberOfMemTilesToGuess ; i ++) {
System.out.println("> we are in for the "+ (i+1) +". time!");
int randomNum;
boolean randomNumberAlreadySelected = false;
do {
randomNum = calculateARandomNumber(memGridDimX * memGridDimY);
if (i != 0) { //for the first time, we don't need to compare any numbers
randomNumberAlreadySelected = isThisRandomNumberExistInAlreadyFoundRandomNumbersArray(randomNum, randomNums, i);
if (randomNumberAlreadySelected)
System.out.println("######## RECALCULATING RANDOM NUMBER !! ##########");
else
System.out.println("They are not equal, go on!");
}
} while (randomNumberAlreadySelected);
randomNums[i] = (Integer)randomNum;
}
//show the memory tiles
setMemTiles(randomNums, true);
waitALittleBit(memoriseTime);
//hide the memory tiles
setMemTiles(randomNums, false);
}
private int calculateARandomNumber(int limit) {
generator = new Random();
System.out.println("* Calculating random number which is smaller than " + memGridDimX * memGridDimY);
int randomNum = generator.nextInt() % (memGridDimX * memGridDimY);
System.out.println("- Calculated random number: " + randomNum);
if (randomNum < 0) {
System.out.println(".. it is negative, so we add: " + memGridDimX * memGridDimY);
randomNum += memGridDimX * memGridDimY;
}
System.out.println(".. and we add 1 to have a grid array between 1 and 12");
randomNum += 1;
System.out.println(".. and our new random number is: " + randomNum);
return randomNum;
}
private boolean isThisRandomNumberExistInAlreadyFoundRandomNumbersArray(int number, int[] numberArr, int numberArrSize) {
for (int j = 0 ; j < numberArrSize ; j ++) {
System.out.println("# Comparing random number: " + number + " and " + (j+1) + ". random number selected earlier: " + numberArr[j]);
if (number == numberArr[j])
return true;
}
return false;
}
private void setMemTiles(int[] randomNums, boolean showMemTiles) {
centerPanel.removeAll();
memTileCoordinates = new int[randomNums.length][2];
for (int i = 1 ; i <= memGridDimY ; i ++) {
for (int j = 1 ; j <= memGridDimX ; j ++) {
int rnX = -1;
int rnY = -1;
boolean isMemTile = false;
for (int k = 0 ; k < randomNums.length ; k ++) {
int rn = randomNums[k];
if (rn % memGridDimX == 0) {
rnY = rn / memGridDimX;
rnX = memGridDimX;
} else {
rnY = rn / memGridDimX + 1;
rnX = rn % memGridDimX;
}
if (i == 1 && j == 1 && !showMemTiles) { //do it once
System.out.println("********* ************");
System.out.println("Random label number: " + rn + " and it's position in the grid: " + rnX + "," + rnY);
memTileCoordinates[k][0] = rnX;
memTileCoordinates[k][1] = rnY;
System.out.println("> Memory Tiles coordinates: " + memTileCoordinates[k][0] + "," + memTileCoordinates[k][1]);
System.out.println("> Memory Tiles length: " + memTileCoordinates.length);
System.out.println("********* ************");
}
if (rnX == j && rnY == i)
isMemTile = true;
}
memTile = new JButton();
if (isMemTile) {
update(getGraphics());
if (showMemTiles) {
memTile.setBackground(Color.red);
System.out.println("%%%% PAINTING MEM TILES IN RED %%%%");
} else
memTile.setBackground(Color.white);
update(getGraphics());
} else
memTile.setBackground(Color.white);
if (!showMemTiles) // we listen actions after the memory tiles disappears
memTile.addActionListener(this);
centerPanel.add(memTile);
update(getGraphics());
}
}
setJPanelSettings();
}
private void setJPanelSettings() {
setSize(memGridDimX * memGridWidthX, memGridDimY * memGridWidthY);
setTitle("Memory :: " + memGridDimX + "x" + memGridDimY);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
private void waitALittleBit(long waitingMillisSeconds) {
long t0 = System.currentTimeMillis();
long t1 ;
do {
t1 = System.currentTimeMillis();
} while (t1 - t0 < waitingMillisSeconds);
}
public static void main(String[] args) {
new Memory(minDimX,minDimY);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(">>> An action came >>>");
JButton memTile = (JButton) e.getSource();
Dimension size = memTile.getSize();
int chosenMemTileDimX = memTile.getX();
int chosenMemTileDimY = memTile.getY();
int chosenMemTilePosX = chosenMemTileDimX / size.width + 1; // we add 1 becausee we present our tile numbers as 1:1, 1:2, ... ( instead of 0:0,0:1, ... )
int chosenMemTilePosY = chosenMemTileDimY / size.height + 1;
System.out.println("Chosen Tile's x dimension: " + chosenMemTileDimX + " ans y dimension: " + chosenMemTileDimY);
System.out.println("Chosen Tile's width: " + size.width + " and height: " + size.height);
System.out.println("Chosen Tile's coordinates: " + chosenMemTilePosX + ", " + chosenMemTilePosY);
boolean tileIsMemTile = false;
System.out.println("Memory Tiles Coordinates: ");
for (int i = 0 ; i < memTileCoordinates.length ; i ++) {
int memTileDimX = memTileCoordinates[i][0];
int memTileDimY = memTileCoordinates[i][1];
System.out.println("x: " + memTileDimX + ", y: " + memTileDimY);
if (chosenMemTilePosX == memTileDimX && chosenMemTilePosY == memTileDimY)
tileIsMemTile = true;
}
if (tileIsMemTile) {
System.out.println("!!! Right Tile !!!");
memTile.setBackground(Color.red);
numberOfMemTilesToGuess -= 1;
System.out.println("It rest " + numberOfMemTilesToGuess + " tiles to guess");
} else {
System.out.println("!!! Wrong Tile !!!");
Icon falseTileIcon;
try {
falseTileIcon = new ImageIcon(getClass().getResource("wrong.png"));
if (falseTileIcon.getIconHeight() > 0)
memTile.setIcon(falseTileIcon);
} catch (Exception e1) {
memTile.setBackground(Color.black);
}
update(getGraphics()); // good trick!!
waitALittleBit(1000);
//TODO !!! FAILED IN LEVEL MESSAGE !!!
dispose();
if (memGridDimX == minDimX && ( memGridDimY == minDimY || memGridDimY == minDimY + 1))
new Memory(minDimX, minDimY);
else if (memGridDimX == memGridDimY)
new Memory(memGridDimX - 1, memGridDimY);
else
new Memory(memGridDimX, memGridDimY -1);
}
System.out.println(">>> Action processed >>>");
if (numberOfMemTilesToGuess == 0) {
System.out.println("\n END OF THE LEVEL");
System.out.println("Congratulations, you guessed all the tiles without error !! \n");
dispose();
//TODO !!!! SHOW INTERLEVEL INFORMATION !!!!
if (memGridDimX != maxDimX && memGridDimY != maxDimY) {
if (memGridDimX == memGridDimY)
new Memory(memGridDimX, memGridDimY + 1);
else
new Memory(memGridDimX + 1, memGridDimY);
} else
System.out.println("You have a really good memory my friend!");
}
}}
I used update(getGraphics()); many times, it was just for a test...
Thanks in advance.
You where told in your last posting to NOT use the update() method.
do
{
t1 = System.currentTimeMillis();
}
while (t1 - t0 < waitingMillisSeconds);
Also, never use a tight loop like that. Its just as bad as using Thread.sleep(...);
You where given suggestions in your last posting!
I think you can use LWJGL, which is a java game library.
https://www.lwjgl.org
Some of the issues may be due to the thread on which the various bit of code run. The first time through you are running on the "Main" thread, and to be completely correct calls to update the AWT/Swing GUI should be done using the "Event Dispatcher Thread".
The actionPerformed method is called from the "Event Dispatcher Thread" and so then the second time you call into the constructor for Memory you are in this thread.
Also, aside from the fact you have implemented a delay with a spin-loop, which is not efficient; you should not delay the "Event Dispatcher Thread".
See this short overview: http://www.javamex.com/tutorials/threads/swing_ui.shtml