Current location - Trademark Inquiry Complete Network - Futures platform - Java Swing is also amazing: interlocking.
Java Swing is also amazing: interlocking.
preface

There is a long-standing debate about how Java can't do the desktop well. Although Swing and Java D have a history of more than ten years, and there are many open source Swing components such as Kidd JGoodies aver, it is still not an easy task to use Java as a desktop program. The series of articles "Java is amazing" just want to learn about Java and explore Swing with you through some simple and vivid examples. Actually, you just need a little more creativity and patience. Your Java program can also be amazing! This article will take you into the wonderful journey of Java.

Two-dimensional casing effect

In network communication, it is often necessary to express the bearing relationship between protocols. For example, ip protocol can be carried on SDH or ATM as a high-level protocol, and IP as a protocol can also carry more high-level protocols, such as the concept of Voice over IP or even Everything over IP in telecommunications. It is perfect to use nested three-dimensional sleeves to express the load of the protocol (as shown below).

The concrete implementation is very simple. The main codes are as follows

Import? Java awt *; ? Import? Java awt geom *; ? Import? javax swing *; ? Import? aver *; ? Public? Class? PipleComponent? Extension? JComponent? {? Public? Invalid? Paint (graphics? g)? {? Figure d? g d? =? (figure d)? g; ? G d setrendering hint (rendering hint key _ anti-aliasing? rendering hints VALUE _ ANTIALIAS _ ON); ? Shape? parenthoolloshape = create piple(g d averUtil getRandomColor()null); ? create piple(g d averUtil getRandomColor()parenthoollowshape); ? create piple(g d averUtil getRandomColor()parenthoollowshape); ? create piple(g d averUtil getRandomColor()parenthoollowshape); ? create piple(g d averUtil getRandomColor()parenthoollowshape); ? }? Private? Shape? CreatePiple (graphic d? g d int? x? int? y? int? Width? int? Height and color? Color shape? Bracket shape)? {? If (bracket shape! =null){? Rectangular? bounds = parenthoollowshape get bounds(); ? Rectangular? rightClip=new? Rectangular (boundary x+ boundary width/boundary y+ boundary height); ? Area? Clip = new? Area (bracket shape); ? Clip add (new? area(right clip)); ? G d setClip; ? }? int? circleWidth? =? Height/; ? GradientPaint? Paint? =? New? GradientPaint(x? y? Brighter color ()? x? y? +? (int)? (height? *? ) ? The color is darker ()? True); ? G d setPaint; ? Ellipse D Double? leftCircle? =? New? Ellipse D Double(x circleWidth? / y? circleWidth? Height); ? Ellipse D Double? Right circle? =? New? Ellipse d double (x? +? Width circleWidth / y? circleWidth? Height); ? int? Thickness =; ? Ellipse D Double? rightHollowCircle? =? New? Ellipse D Double(right circle getX()+ thickness? Right circle getY()+ thickness? RightCircle getWidth () thickness *? right circle get height()thickness *); ? Rectangular? rect? =? New? Rectangular (x? y? Width? Height); ? Area? Area? =? New? Area (left circle); ? Region addition (new? Area (rect));); ? Area minus (new? Right circle));; ? G d filling (area); ? G d setColor (darker color ()); ? G d filling (right circle); ? Paint? =? New? GradientPaint(x? y? Dark gray in color? x? y? +? (int)? (height? *? ) ? Light gray in color? True); ? G d setPaint; ? g d fill(right hollow circle); ? G d setClip (empty); ? Return? rightHollowCircle? }? Public? Static electricity Invalid? main(String[]? args)? {? JFrame? Frame? =? New? JFrame(); ? frame setDefaultCloseOperation(JFrame EXIT _ ON _ CLOSE); ? Frame set size (? ); ? Frame addition (new? piple component()); ? frame set visible(true); ? }? }

Three summaries

The main knowledge points of this article

Gradient Fill creates a GradientPaint and sets the light and dark fill modes.

Just look at the graphics using the set Clip function of Java D technology similar to mask/cut in clip.

The use of area is mainly the intersection and merger of area and other common graphics processing technologies. See java awt geom Area class for details.

It's too easy to use random colors. If you have aver jar, you can use averUtil getRandomColor () directly. If not, just use the new color. Note that the fourth int parameter is used to increase the change of Alpha transparency.

If you are interested, you can try to achieve the following effects with the above Java D technology.