Monday, April 24, 2017

Java | Memorry Consumtion | LinkedList Vs ArrayList

If memory matters, use ArrayList!
LinkedList might allocate fewer entries, but those entries are astronomically more expensive than they'd be for ArrayList -- enough that even the worst-case ArrayList is cheaper as far as memory is concerned.Louis Wasserman

Sunday, April 16, 2017

JavaFX | Having access of other Controllers from a specific Controller

There are some good ways to do this:
  1. Create a reference of a Controller
  2. private ControllerClass controller;
    
    /*...
    
    Parent root;
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("/path/ofYourFxml.fxml"));
    
    controller = fxmlloader.getController();
    //Now you can use a method like getter and setter to set or get any value of variable
    controller.yourMethodToCall()
    
    Stage stage = new Stage();
    stage.setScene(new Scene(root, 1200, 700));
    stage.show()
    
  3. For dirt and dirty solution: declare any variable you want to use in other class as static (Not recommended for a complex application(static problem))
  4. Use a shared data Model among Controllers
  5. Explaination follows...
    
    

Saturday, April 15, 2017

Java | Probably the most unused keyword in Java | The "volatile" keyword

In multithreaded application the behavior or the result of some processes might look very strange to a new programmer. (including me when i write this post)

The Problem

In multi cores computers, different Threads can be run on different cores. It make sense since you want to get the advantages of having multi cores. The problem comes when you are using primitive data types to control processes that handled by different threads. Primitive data types are stored in cache (L1) of a core in your CPU. These data are not shared between the cores. So the data that are using in one core is not equals with the data that used in other cores. This will lead to unexpected result of your program.

The Solution

Decorate your variable with a keyword "volatile" like this <code>private volatile int someInteger;</code>

JavaFx | How to populate a ListView with an arrayList

It is very simple: you have to pass your list (Collection.List) to an ObservableArrayList (FXCollection) like this. ObservableList anyList = FXCollections.observableArrayList(yourList)
transform an ArrayList into observableArrayList

Friday, April 14, 2017

First Trump character created

Today I created the second character of the game, the basic Trump character. It is a guy pushing the border wall, trying to keep the mexicans out. This will be the most basic character of the Trump team. He will cost little, move at an avarage pace and have little health.
Although that could change later. (Maybe we will create a character without a wall to be the basic one and have this a a character with much health.)
Anyway here are some sketches and then the final animation. As you can see the wall is quite heavy for Mr. Trump to push so he is breathing heavely.
First sketches
Final animation

Saturday, April 1, 2017

JavaFX | Make an update of the UI | Platform.runlater

In javaFX any update of the user interface should be done by the javaFX application thread. Any other thread trying to update the UI will crash the application. So if you have a multi threaded application every line of code that update the UI should be wrapped in Platform.runLater block like this:
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // Update UI here.
    }
});
Or using lambda Expression:
Platform.runLater(
  () -> {
    // Update UI here.
  }
);