25 March 2017

Off-Topic | Ubuntu | No wifi connection after standby modus

If you're using Ubuntu on Lenovo ThinkPad T430 series, you might have the same problem as me: No internet connection after you've woken up your laptop from standby modus.

I haven't found any better solution yet but this will solve your problem: go to terminal and run this code sudo service network-manager restart please write comments if you have more automated solution for me.

12 March 2017

Java | Client & Server | Simple Socket Programming

To make an application that can communicate with other applications like chat-app or multiplayer games, we need a server that serve data between clients. A Client send data to the server and the server send it to another client. Server and client send data to each other via sockets.
How to create a Server
  • Create a class with a main method
  • Create an instance of ServerSocket. You need to pass any port number as a parameter ServerSocket ss = new ServerSocket(9999);
  • In order to accept an incoming client connection, we have to call the ServerSocket.accept() method. As soon as connection request is made from client, this method will return a socket that created specifically for this client.
  • To send and receive data from client, you can use OutputStreamWriter and BufferedReader
  • The Server
How to create a Client
  • Create a class with a main method
  • Create an instance of Socket class and pass the port number and the private ip address of your server as parameter.  You can use localhost instead of ip for local testing. Socket s = new Socket(ip, port);
  • To send and receive data from client, you can use OutputStreamWriter and BufferedReader
  • The Client
How it works
Run the server. once the accept() method is called, the server will wait for a client to connect. When you run the client, a connection request is made and ServerSocket will then create a socket that is connected to the client's socket until Socket.close() method is called. To make a simple chat application, put a Writer and a Reader in a while loop and use a Scanner to take user input.

10 March 2017

Java | Important Syntax

  • Ternary Operator
  • It is basically a simplified if statement. If the condition is true, the first value will be assigned to the variable. if false, the second value will be assigned.
    The statement is false. So x will have the value of b

Java | try with resources statement

Since Java 1.7 you can declare all resources that can be closed. e.g BufferedReader right after the try statement. With this feature, all resources will be closed and errors will be caught automatically. Before Java 1.7 you'll have to close all resources manually. like this:
managing resources before java 1.7
Since Java 1.7 this is simplified to this:
managing resources after java 1.7

9 March 2017

Java | Important classes

BufferedReader vs Scanner
  • BufferedReader has a bigger buffer (8192 chars) than the one using in scanner (1024 chars). Use BufferedReader if you want to get long String from a stream.
  • BufferedReader is synchronized or thread safe while scanner is not
  • Use scanner if you want to parse a file and use BufferedReader if you want to read line by line
  • To use readLine() method, you have to create BufferedReader object and put a class that extends Reader object as a parameter: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); The parameter of the InputStreamReader is your input source, in this example we get an input from the console. System.in Be sure to put BufferedReader object in try block to manage the resources.
using BufferedReader in try block
Serializable
Let's think about a game. After some hours of playing game, you might want to have a break. You want to save the current state of your game so you can continue later from the same point. When saving a game or an application. You want to save the state of objects that have been created during the runtime of your application. In Java an Object cannot be stored in a file as an Object. It can only be stored if it converted to a stream (of bytes) beforehand. The process of converting is called serialization.

Some reasons for serialization are:
  • An object is created on the heap and the heap is for temporary storage. Only serialized objects can be saved as a file
  • To transmit data or state of an object to different location through the network
To make an Object serializable the class muss implement Serializable interface
make a class serializable
This is how you save an object into a file.
save an object as a file
And to retrieve the saved object.
retrieve the state of an object from a file

JavaFX | Basic Knowledge

The Stage

In JavaFX the The user interface is the 'Stage'. Like in a theater, the stage is where all the events will be presented to the audiences or the users.
To create a JavaFX application on IntelliJ, you simply have to create a new JavaFX class.
Creating JavaFX application
After you created the JavaFX application, you can see, there is a start method to be implemented. For now the curtains of the stage is still closed. You can open the curtains by adding primaryStage.show(); to the start method.
adding show method to open the stage
If you now run the application, you'll see an empty 'stage'. The stage is there, now it is up to you to perform a show

The Scene

Now that you have a stage, you might want to perform a show. To do that, you'll need a scene. In a scene, there are some props or in Java terms, 'nodes'. A node can be a button, text field or just a label. To create a good scene you might want all these nodes to be organized nicely. Some nodes that build one feature, should also be placed side by side so the user will know intuitively what these nodes are about. Nodes is organized in a class called Parent. In Web term, it is similar to div tags. You can organize the nodes vertically using VBox Parent or horizontally using HBox Parent.

Layouts

BorderPane
BorderPane is a layout with five areas: Top, left, center, right and bottom.
GridPane
As the name said, GridPane works with grid system.
use indexes to organize the nodes
use hgap and vgap to control the space between grid
when there is not enough space, the nodes will shrink to fit the window