Accidentally saw a digital inside the matrix of the effect of rain, feel very cool, so write a code imitation under. As follows
The effect is very primitive, did not do too much modification, directly on the code:
Importjava.awt.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.KeyAdapter;Importjava.awt.event.KeyEvent;ImportJava.awt.image.MemoryImageSource;ImportJava.util.Random;ImportJavax.swing.JDialog;ImportJavax.swing.JPanel;ImportJavax.swing.Timer; Public classRainextendsJDialogImplementsActionListener {PrivateRandom random =NewRandom (); PrivateDimension screensize; PrivateJPanel Graphicspanel; //row height, column width Private Final Static intGap = 20; //location information for storing the top of the Raindrop (margintop) Private int[] Posarr; //Number of rows Private intlines; //Number of columns Private intcolumns; PublicRain () {initcomponents (); } Private voidinitcomponents () {setlayout (NewBorderLayout ()); Graphicspanel=NewGraphicspanel (); Add (Graphicspanel, borderlayout.center); //set cursor not visibleToolkit Defaulttoolkit =Toolkit.getdefaulttoolkit (); Image Image= Defaulttoolkit.createimage (NewMemoryImageSource (0, 0,NULL, 0, 0)); Cursor Invisiblecursor= Defaulttoolkit.createcustomcursor (Image,NewPoint (0, 0), "cursor"); SetCursor (Invisiblecursor); //Esc key to exitKeypresslistener Keypresslistener =NewKeypresslistener (); This. Addkeylistener (Keypresslistener); //This.setalwaysontop (TRUE); //go to the title bar This. setundecorated (true); //Full Screen This. Getgraphicsconfiguration (). GetDevice (). Setfullscreenwindow ( This); This. Setdefaultcloseoperation (Jdialog.dispose_on_close); SetVisible (true); ScreenSize=Toolkit.getdefaulttoolkit (). Getscreensize (); Lines= Screensize.height/Gap; Columns= Screensize.width/Gap; Posarr=New int[Columns + 1]; Random=NewRandom (); for(inti = 0; i < posarr.length; i++) {Posarr[i]=Random.nextint (lines); } //10 frames per second NewTimer (100, This). Start (); } /** * @returnRandom characters*/ Private Chargetchr () {return(Char) (Random.nextint (94) + 33); } @Override Public voidactionperformed (ActionEvent e) {graphicspanel.repaint (); } Private classGraphicspanelextendsJPanel {@Override Public voidPaint (Graphics g) {graphics2d g2d=(graphics2d) G; G2d.setfont (GetFont (). Derivefont (Font.Bold)); G2d.setcolor (Color.Black); G2d.fillrect (0, 0, Screensize.width, screensize.height); //Current Column intCurrentcolumn = 0; for(intx = 0; x < screensize.width; X + =Gap) { intEndpos =Posarr[currentcolumn]; G2d.setcolor (Color.cyan); G2d.drawstring (String.valueof (GETCHR ()), X, Endpos*gap); intCG = 0; for(intj = endPos-15; J < Endpos; J + +) { //Color GradientCG + = 20; if(CG > 255) {CG= 255; } g2d.setcolor (NewColor (0, CG, 0)); G2d.drawstring (String.valueof (GETCHR ()), X, J*gap); } //At the end of each frame, the position of raindrops on the current column is randomly moved down the linePosarr[currentcolumn] + = Random.nextint (5); //re-generates a random position when the raindrop position exceeds the screen height if(Posarr[currentcolumn] * gap >getheight ()) {Posarr[currentcolumn]=Random.nextint (lines); } currentcolumn++; } } } Private classKeypresslistenerextendsKeyadapter {@Override Public voidkeypressed (KeyEvent e) {if(E.getkeycode () = =Keyevent.vk_escape) {System.exit (0); } } } Public Static voidMain (string[] args) {NewRain (); }}
Java implementation of the Hacker Empire Digital Rain Effect