Recall the Socket based on the connection pool that we learned yesterday. The key points in its principle are as follows: the server opens a limited number of PooledConnectionHandler threads to process connections; all customer connection requests are added to the Pool connection pool (actually a linklist() object) ; Once the PooledConnectionHandler discovers that there is a connection request in the connection pool, it will immediately start processing it; everyone uses the same Pool (so it is a static variable)
So far, some basic content about java programming has been learned. Now It’s time to apply what you have learned. If you want to truly get started with any language, it should be marked by actual programming experience. I think.
I have programmed a Tetris game in C++ before and I think this game The programming of Java can embody the object-oriented thinking. Java and C++ have a lot in common, and anyone can copy it, so let’s implement it again. Anyway, I have almost forgotten it, so I should review the past and learn something new. In addition, since I have spent a lot of time, So much time to study Socket, then simply compile an online version.
The Tetris program requires a graphical user interface (GUI), and among the Java programs compiled previously, only the Applet program implements a graphical interface, but that It is completed with the help of AppletViewer or browser. To implement a graphical user interface in Java, we need to use the Frame class. The Frame class is a subclass of the window class (windows) and is the basic window of the graphical user interface. Read below By implementing a small program, you can basically understand the graphical user interface under Java.
File name: FrameMemo.java
Function: Pop up a frame with a menu and display information in the frame. In the window Click anywhere in and the program will exit.
import java.awt.*;
public class FrameMemo extends Frame
{
public String motd;
public FrameMemo(String s)
{
super(s);// super here represents the parent class, which corresponds to the class represented by this, so what is called here is the constructor of the parent class
motd=s;
resize(300,300);
SetMenu();
}
public FrameMemo ()//Overloaded constructor
{
this("This is a Memo of Frame");//What is called here is the FrameMemo(S) constructor
SetMenu();
}
public void SetMenu()
{
Menu m,n;
m=new Menu("Examples");
m.add(new MenuItem("Basic"));
m.add(new MenuItem("Simple"));
n =new Menu("More Examples");
n.add(new MenuItem("Sub Basic"));
m.add(n);
MenuBar mb=new MenuBar();
mb.add(m) ;
setMenuBar(mb);
}
public void paint(Graphics g)
{
g.drawString(motd,15,15);
g.drawString("Click anywhere to Exit",45,45);
}
public void start()
{
show();
}
public boolean mouseDown(Event e,int x,int y)
{
hide();//Hide window
dispose();/ /Release resources
System.exit(0);
return false;
}
public static void main(String args[])
{
FrameMemo m;
m=new FrameMemo();
m.start();
}
}
After creating the window, we also need to place various graphical interface objects in the window, that is to say, layout. The principle of layout has been introduced before, and we need to use a layout container, and then arrange the objects according to a certain Fill in the rules. The following example demonstrates most of the containers we need to use. I have given very specific comments to help me quickly find lost memories if I forget Java syntax in the future.
file://layout example
file://j2sdk1.4.1 compiled under
import java.awt.*;
import java.applet.*;
public class Layout extends Frame
{
Panel fnames,styles;/ /Place two font buttons in the fnames Panel and three style buttons in the styles Panel
Button sans,serif;
Button bold,bigger,smaller;//Define two font buttons and three style buttons
SampleCanvas sample;//Define the overloaded canvas object
String fName=new String("SansSerif");
int fBold=0;
int fSize=12;
public Layout(){
super("A Memo of Layout ");
setFont(new Font("Sans",Font.BOLD,12));
setLayout(new BorderLayout());//Set the BorderLayout layout. Five elements can be arranged in this way
file://The orientation is East, West, South, North, Center
file://The following program is to set the two buttons at the bottom or south
fnames=new Panel();
fnames.setLayout(new GridLayout(1,2,5,10));//is panel sets the GridLayout layout
file://This layout is a grid layout
file://The four parameters are
file://Number of rows, number of columns, horizontal spacing (pixels), vertical spacing (pixels)
sans= new Button("Sans Serif");
fnames.add(sans);
serif=new Button("Sans Serif");
fnames.add(serif); file://Add two buttons respectively
add( fnames,"South"); file://Include the Panel in the BorderLayout layout
file://The following program is to set the three buttons on the left or west
styles=new Panel();
styles.setLayout( new GridLayout(3,1,10,5));
bold=new Button("Bold");
styles.add(bold);
bigger=new Button("bigger");
styles.add(bigger) ;
smaller=new Button("Smaller");
styles.add(smaller);
add(styles,"West");
sample=new SampleCanvas();
add(sample,"Center");
setSize(200,200);//Don’t miss this sentence
// setVisible(true);
}
public void start(){
show();
}
public static void main(String[] args)//Initialization
{
Layout MainFrame=new Layout();
MainFrame.start();
}
class SampleCanvas extends Canvas
{
String message;
public SampleCanvas() file://Constructor of the class
{
rewrite("SanSerif"); file:// is only executed when creating the object, Java does not support destructors
}
public void rewrite(String s)
{
setFont(new Font(fName,fBold,fSize));
message="This font is"+fName;
repaint();
}
public void paint(Graphics g)
{
g.drawString(message,10,50);
}
}
}
(January 28) In the online version of the Tetris program, it should contain The following classes:
1.MainFrame: Main interface class, continuing from Frame, the basic definition and method functions in the class are as follows
class MainFrame extends Frame{
public MainFrame(){//Initialization work, mainly for objects ( Button, canvas, etc.) layout
}
public boolean keyDown(Event e, int key){//Handle keyboard operations
}
public boolean action(Event e, Object arg){//Handle mouse operations
}
public void start (){//Look at the notes from the past few days
show();
}
public static void main(String args[]) throws IOException{
MainFrame app;
app=new MainFrame();
app.start();
}
}
2.MainCanvas class. This canvas object will complete all logical operations related to the block, continuing from the Canvas class
3.Block class, that is, the block class. This class stores the information about the shape of the block (a Multidimensional array), etc., this class will complement the MainCanvas class. In fact, Block and MainCanvas can be made into one class, which was the case in my original C++ version. However, from an object-oriented perspective, it is divided into The two classes are more suitable.
4. ClientThread class, which is the thread class of the client
5. ServerThread class, which is the thread class of the server
ServerThread and ClientThread are responsible for the operation of network synchronization
After analyzing this part of the work, it is basically The above is completed. I decided to give up the plan to implement the online version of Tetris, because it is very boring to repeat the ideas that I was very familiar with.
I am going to target the implementation of an electronic whiteboard based on Java Applet. I will put related research into another series after the year.
The rookie notes that lasted for half a month are finally over. 15 days is enough to get started with any language. After all, language is just a tool to embody design ideas. It shouldn’t take too much time to become familiar with and use any tool. How to use tools to create beautiful works of art should always be thought about in your mind.
In the process of getting started with Java, three books gave me A lot of help. The first book is "Java Concise Tutorial" by the Machinery Industry Press, written by a British man. The language in the book is clear and simple, and you can fully grasp the basics of Java in two days. . It gives me the feeling that foreigners are very good at writing the most basic and profound technical textbooks. The second book is "Java Programming" edited by Tan Haoqiang, which is a veritable entry-level reference book for beginners. The content is relatively complete. The last book is Think in Java in chm format, which helped me understand Java more deeply, especially threads and synchronization. In addition, all the examples about Socket that I have given are derived from There is a set of teaching materials on how to write Socket programs in Java. This set of teaching materials is also very practical.
Okay, I wish CDSN friends a happy new year! Happy New Year!
Attachment: Download address
1. Book title: Java Set Socket 101
Note: This tutorial will tell you what a socket is and teach you how to use it in a Java program. By examining several practical examples, from a single client/server communication to a group of clients in a pool accessing a server, you'll learn how to use sockets to handle typical situations encountered in the real world.
URL: http://www-900.ibm.com/developerWorks/cn/edUCation/java/j-sockets/tutorial/j-sockets.zip
2. Book title: Think in Java (Chinese version)-- -chm format
URL: http://www.code-labs.com/manual/Think%20In%20Java.chm
It is still valid now
The above is the content of the memo (10) for beginners learning Java, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!