Many friends are very curious and interested in Java programming! Below @月下@ will share with you an enlightenment chapter on java programming, so that everyone can have a clearer understanding of the simple programming and understanding behind the java games we play! !
Games, look at the popularity of games this year, everyone can appreciate its infinite charm. More and more programmers are joining the game development industry. And game development itself is a lot of fun and challenge. This series uses the Java language to teach you step by step about game development and understand every technical point of Java game development. Let everyone use Java as a language tool flexibly and create their own games using Java. is the ultimate goal of this series. Friends who have experience in Java will definitely wonder why we chose Java game development when Java enterprise applications (J2EE) and Java mobile applications (J2ME) are rampant in the Java world? The reason is very simple. Game development mainly uses J2SE technology, and J2EE and J2ME are both developed on the basis of J2SE and must also rely on J2SE to exist. The three are complementary to each other. J2SE is the foundation, and tall buildings rise from the ground. As long as you have laid the foundation, what procedures can't be done? There are also many successful application developments on J2SE. You can even easily port the game program you developed to the above two application directions. Well, bring your confidence and determination and join us on this wonderful journey. In the first part, we mainly introduce some features and simple applications of the Java language for Java beginners. Let everyone move their joints. If you are an experienced Java programmer, you can skip this chapter, but understanding the knowledge about Java in game development in this part will be very helpful for your subsequent study. Directory guide: 1. Introduction to Java 2. Java environment settings 3. Basic Java syntax 4. Advantages and disadvantages of developing games with Java 5. Commonly used Java API packages in game development 6. Reference materials 1. Introduction to Java Java is a language developed by SUN. Since its release in 1995, it has been widely used for its superior object-oriented ideas and simple functions. Microsoft President Bill Gates said: "Java is the most outstanding programming language." Since 1997, many universities have used Java to replace C as the programming teaching language. Java can be seen everywhere now, and Java has become a common language for programmers. It has gradually penetrated into various fields. If you are a programmer, you may come into contact with Java sooner or later. But what exactly is Java? Java is an object-oriented programming language similar to C++. Like other languages, it has its own grammar and writing rules. The Java platform we often mention is composed of Java Application Programming Interface (API), Java Virtual Machine and (JVM). Java API is a set of compiled functional interfaces provided by Java to programmers. The Java Virtual Machine (JVM) is one of the most outstanding designs in Java. It is a virtual platform built on the CPU and OS. Our Java program does not run directly on the native operating system but on the Java virtual machine, which is interpreted by the Java VM to the native operating system. In this way, Java programs can be executed on different platforms, which is the so-called "write once, execute anywhere". This means that any computer system with a Java VM installed can run Java programs, regardless of the computer system on which the application was originally developed. For example, a Java program you wrote under the Window platform can run directly on the Linux platform without making any modifications. Different platforms have different Java VM versions. The Java platform structure is shown in the figure below: When people mention "learning Java", they usually mean the Java language and API.2. Java environment settings In order to write and run the Java programs we write, we need to install the JDK development package in our local machine. JDK is a compressed package with built-in various Java development tools, Java virtual machines, and Java APIs. We can get it from java. sun. com website to download the latest version for free. It should be noted here that the JDK compression packages of different system platforms are different. The method introduced below is based on the Windows platform. The processing under Linux is similar, except that the file extension type changes. The downloaded JDK is an executable file. We can install it directly after running it. The default installation path is C:\j2sdk1.4.1_02. In order to allow the computer to find the program you want to run, JDK will automatically add JAVA_HOME=C:\j2sdk1.4.1_02 to your system environment variable. But if you want to run your own program conveniently, this is not enough. We have to set the system class path ourselves. The classpath can be set by using the -classpath option in the javac compiler command and java interpreter command, or by setting the classpath environment variable. In Java program development, we will refer to a large number of API libraries provided by Java, so we need to tell the computer where these Java APIs are located. Classpath is to help us find the configuration of these Java class paths, just like path in DOS. To successfully compile and run a Java program, we must also tell the computer the compiler javac in the JDK. exe and execution tool java. The path where the exe is located. These two tools are placed in the bin directory of your JDK installation directory, such as: C:\j2sdk1.4.1_02\bin Let's take a look at the specific environment settings on the computer: PATH=C:\j2sdk1.4.1_02\bin; c:\windows; c\windows\command classpath=. ; C:\j2sdk1.4.1_02\lib\tools. jar; d:\test Note: The "." in classpath represents the current directory, which allows you to compile your own class in the current directory without errors. D:\test is any class placement path specified by ourselves. In this way, even if the Java operation is not in the current directory, the Java classes in the d:\test directory can be compiled. 3. Java Basic Syntax There are many IDE tools for writing Java programs, such as Eclipse, JCreator, and JBuilder, which are all very good tools. And the easiest way is to use a text editor. Next, we will use a text editor to create a Java application, and based on this, we will talk about the basic syntax of Java and feel the sense of accomplishment brought by your first Java program. Before starting, you can download our source program TestDate. java, or enter manually: package my. test; import java. util. *; /** * Date test program * @author Tianyi. Li */ public class TestDate { // Declare private variables pass private Date pass; /** * Print date method and initialize the value of pass */public void printDate() { // Call date function pass = new Date(); // Display the current date System. out. println ("Current date: " + pass); } public static void main (String arg []) { // Initialization class TestDate TestDate testDate = new TestDate (); testDate. printDate(); } } Then compile the program. The compiler converts the java source program into bytecode that the Java VM can understand, so that any computer with a Java VM installed can interpret and run the program. We can successfully compile TestDate by entering the following command in the command line of the Dos window. java program: javac TestDate. java interprets and runs programs. The interpretation and running of Java programs refers to calling the Java VM bytecode interpreter to convert Java bytecodes into platform-related machine codes so that your computer can understand and run the program.Before running the above example program, we must first create a new my directory in the current directory, create a new subdirectory test under my, and put our compiled TestDate. The class file is moved under test. Then enter: java my. test. TestDate On the command line, you can see the terminal window displayed as follows: Why do you need to create these new subdirectories? The package mechanism below will tell you everything. If you think this is a bit troublesome, it doesn't matter. Most Java IDEs will automatically generate these directories on your system. Or after you understand the following package mechanism, comment out package my. test, perform operation demonstration directly in the current directory. Package (package) Everyone writes different programs with different naming methods. How to effectively manage these names without duplicate names. Java draws on the uniqueness method of Internet domain name addresses and introduces the package mechanism. package uses the Internet domain name written upside down, with dots separating each path. This ensures that class libraries with different functions are placed in different directories. Here package is equivalent to a namespace, and classes in different namespaces will not interfere with each other. When creating a local package path, press the "." symbol to create the corresponding subdirectory. In the above example: package my. test; is equivalent to creating a new my. test package, and this package separates the TestDate class and external classes according to the directory structure of my\test. So our compiled TestDate. The class file should be placed under the directory my\test. In addition to the name separation function, the package also has a function that can set the access permissions of this namespace to another namespace class. Only classes named public can be called from classes in another namespace. In the permissions section below we will see several ways to divide permissions in Java. Import Import and package are corresponding. Package mainly manages its own class library, while import mainly handles calls to class libraries in another package. Since the package mechanism hides the visibility of the class library, if we want to reference a class in another class library to implement the corresponding function, we have to know the location of the class library, that is, we need to get the package path on the class library. When the program starts, the Java virtual machine looks for the referenced package based on the environment variable classpath. If it is not found, go to the directory where the current program is located to search. If it is not found again, an error will occur when the program runs. Seeing this, do you understand the role of classpath in class library path configuration? After Java finds the referenced path, it will load all the classes specified by the path into memory. This is a major feature of Java and is used to speed up developers' calling programs. import java. util. *; In order to create a new Date type variable to implement our function of displaying the current date, in the example we quoted java. util package, the "*" used here represents all classes under this package, including the Date class under this package. If we only want to reference the Date class, we can also write it like this: import java. util. Date; In this way, only the specified classes are loaded into the memory during the running of the program instead of loading all classes. If you are interested in the loading process, you can try adding the parameter –verbose when executing the program, that is: java –verbose my. test. The TestDate screen displays the names of all libraries brought into memory. Object-oriented Java is an object-oriented (OOP) programming language. In Java, object-oriented appears in the form of classes, and objects are instances of classes. Therefore, to learn Java classes, you must first understand object-oriented thinking. 1. Class We use the modifier "class" to represent a class. It represents the type of a certain type of thing, which encapsulates the overall appearance (data members) and behavior (member functions) of this thing. For example, birds can be a class, which encapsulates the basic behavior of birds: flying, and also encapsulates the basic appearance of birds: they all have wings.After encapsulating a class, even if we change some code of the class, as long as the methods being called are not changed, the class can be referenced