As a result of trying to improve the rhythm of life but failing to kill one day, the working time is also reduced.
By the way, this time we will make the UI part using JavaFX. Then let's make the standby part for communication. ** I don't have time! (what goes around comes around)**
In a nutshell, it's a "GUI drawing library for Java".
The feature is that the design is described in XML and CSS. In addition, it is very easy because you can use an application called SceneBuilder that can be designed by drag and drop for free. Somehow it can be used on the RIA platform.
For history and history, see see this wiki.
Let's create the main window using the above Scene Builder.
While respecting Mr. IP Messanger, it looks like this
When this is output, an FXML format file is output. This time, we named it MainStage because it is the main screen (Stage). Let's create a directory for storage and put it in the source.
In the image above, MainStage.java is also stored, but this is a separately made one. Unlike C # Form, FXML is a file that records the structure (and event handler information), so It is necessary to create the java source to process separately.
It seems better to think that this area is divided like an MVC model.
It may be better to divide the files by control and view ...
I want to call the created FXML for the time being.
I've researched various things, but I can only find a template for the JavaFX Project. Specifically, it only describes how to call the screen as it is from the _main () method.
This time I want to call it from another Main method, so I checked various things. The pages around here were helpful
Roughly speaking, Application is the main management of Stage (may be treated the same as window). Stage draws a scene. Scene is composed of various nodes stored in Parent. Is it like that?
The specific procedure is like this.
Let's do it concretely.
This is easy.
MainStage.java
package kugui.owd.privateMessenger.stage;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainStage extends Application {
@Override
public void start(Stage stage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource(
getClass().getSimpleName() + ".fxml" )
);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
return;
}
}
The first thing to watch out for is overriding the start () method. It seems that the Application inheritance method passed to Application.launch () is executed according to its own life cycle.
It looks like this in simple writing
Although init () was originally implemented, start () is declared as an abstract class, so it will not build unless it is overridden. Basically, the initialization process should be done at the timing of start ().
The processing flow of the above code is as follows.
There are many samples that describe the processing in the same class around here, so it may not stumble so much.
Since the inheritance class of Application has been prepared for the time being, set the caller next.
I added a little to ppa.java who left it in the previous article.
ppa.java
public class Ppa
{
public static void main( String[] args) {
System.out.println( "Good night World");
Application.launch(MainStage.class, args);
return;
}
}
Application.launcher () seems to have the following restrictions.
In that case, you have to think a little about how to implement it.
By the way, this launch () is a synchronous method. If you write a process after this, it will be executed after the window is closed.
When I get here, I will display it for the time being.
Yup. There seems to be no problem so far.
To be honest, I've never built a multi-threaded program. At least I have never seen it as an embedded device. Because it's okay to build in multiple processes, and there are more people who process information using IRQ than IPC. Oh, I haven't seen it in frameware.
I get the impression that you are using Morimori when it comes to games. It's a big deal, so let's get used to it here.
It's natural that things that haven't been done get better. But if you do it, there is nothing. The world is made of such things.
The usage of threads is roughly summarized as follows.
It's simple. Let's make it.
There are two ways to create a "threading-compatible class" in Java.
What's the difference? It seems that the operation is the same. Due to Java language specifications that do not allow multiple inheritance, use the Runnable interface when you want to inherit other classes and create threads! It seems that.
Now, create a new standby thread class RxThread by inheriting the Java.lang.Thread class. By the way, Thread class can be used without import.
RxThread.java
package kugui.owd.privateMessenger;
public class RxThread extends Thread{
public void run() {
int countPray = 0;
try {
while(true) {
System.out.println(String.format("%2d : please let me see the good dream...", countPray));
Thread.sleep(1000);
countPray++;
}
}catch(InterruptedException ie ) {
/* no operation */
}catch(Exception ex) {
ex.printStackTrace();
}
return;
}
}
With loop processing to check the operation.
It seems that Thread also has something like a life cycle ... and the void run () method is called when the thread is executed. So, implement the run () method and describe the process. I searched for a description that would call other methods, but I couldn't find it, so it seems necessary to write to call from run () from initialization to termination processing.
Add the following processing to ppa.java's main () method
ppa.java
public static void main( String[] args) {
RxThread rxThread = new RxThread();
Thread thread = new Thread(rxThread);
System.out.println( "Good night World");
thread.start();
Application.launch(MainStage.class, args);
thread.interrupt();
System.out.println( "r u still awake?");
return;
}
When I check the operation, it looks like this.
_ ↓ window It looks like this when closed _
I'll add a little bit below about what I got stuck about threading so far.
To be honest, I thought, "If the parent thread disappears, the child thread will also disappear." Yes, it feels a little strong when it is written as "parents die but children live".
When I try to move it, it looks like this.
If you think about it carefully, it's like "parents are dead and children are zombies", so it's okay to be pitch black. Therefore, it is necessary to make sure that the child process is terminated before the parent process is terminated.
That's why I looked it up. How to terminate a thread. Then I found that a method called Thread.stop () was implemented.
When I write this, Eclipse is sick this time.
Even the strikethrough is put out like thread. ~~ stop ~~ (). why? If you think this method, deprecated method. Details: Oracle --Documentation Class Thread (Japanese) --See stop () section
To cancel the wait, use the Thread.interrupt () method and let the thread end the thread itself. By the way, suspend () that suspends Thread operation and resume () that resumes it are also deprecated. The reason is "because it is easy to cause deadlock!" Basically, it seems that interference with other threads is limited to interrupt queues using thread.interrupt ().
Well, if you make the control system disjointed, you will be able to strangle yourself later.
I tried this and it works fine. If it's a pattern class that implements the Runnable interface, you'll have to do it here. (start () method or)
Since this time is Socket communication, communication using webSocket.
Hmmm, how do you explain Socket communication in an easy-to-understand manner? Since there is a data storage area that is also used as a port used for sending and receiving data, An image that specifies the port to be used during standby and monitors it.
When I looked it up, it seems that java socket communication is very easy to make.
Practice immediately.
Delete the process of RxThread.run () that was kneaded above once and change it to standby process
RxThrea.java
package kugui.owd.privateMessenger;
import java.io.IOException;
import java.net.*;
public class RxThread extends Thread{
public void run() {
int port = 80; //For test.
ServerSocket sSocket;
try {
sSocket = new ServerSocket(port);
sSocket.accept();
System.out.println( String.format("port %d : socket accept", port));
}catch(IOException ioE) {
ioE.printStackTrace();
}
if( sSocket != null ){
sSocket.Close();
}
System.out.println( "finished");
return;
}
}
Start immediately after assembling.
Access localhost with a browser to send data to port 80.
The result is this.
I was able to stand by without any problems, so that's it for today. Today's operation is ... I got confused by restarting windows on the way, but assuming about 2 hours ... 06:45:53.
Recommended Posts