Java implements special effects on pictures with snow float

Source: Internet
Author: User

1) production points
1. Multithreading
2. Use of java. util. Random classes

(2) embedded Applet HTML page source code
<HTML>

<HEAD>

<TITLE> SnowPic </TITLE>

</HEAD>

<BODY>

<Applet code = "SnowPic. class" align = "baseline" width = "1024" height = "768">

<Param name = "graphic" value = "Bridge.jpg">

<Param name = "snows" value = "100">

<Param name = "snowSize" value = "4">

<Param name = "threadsleep" value = "40">

</Applet>

</BODY>

</HTML>
Note: The java class in the image above is called the SnowPic ghost, and the image name is "bridge.jpg" and the size is 1024*768.

Java source code
Import java. applet. Applet;

Import java. awt .*;

Import java. util. Random;

Public class SnowPic extends Applet implements Runnable

{

Thread mainThread;

Image offScreen, gAlc [];

Random rand;

Int stopFlag, snows, wind, threadSleep, and snowSize;

Int [] snowX, snowY;

Long stopTime = 0;

Dimension dim;

MediaTracker mt;

Public SnowPic (){}

Int getParameter (String s1, int s2)

{

String s = getParameter (s1 );

Return (s! = Null )? Integer. parseInt (s): s2;

}

Int getParameter (String s1, int s2, int max, int min)

{

String s = getParameter (s1 );

If (s! = Null)

{

If (s2 = Integer. parseInt (s)> max) return max;

Else if (s2 <min) return min;

Else return s2;

} Else return s2;

}

String getParameter (String s1, String s2)

{

String s = getParameter (s1 );

Return (s! = Null )? S: s2;

}

Public void init ()

{

Rand = new Random ();

Dim = getSize ();

OffScreen = createImage (dim. width, dim. height );

Snows = getParameter ("snows", 100,500, 0 );

SnowSize = getParameter ("snowSize", 3,10, 3 );

ThreadSleep = getParameter ("threadSleep", 80 ,1000, 10 );

SnowX = new int [snows];

SnowY = new int [snows];

For (int I = 0; I <snows; I ++)

{

SnowX = rand. nextInt () % (dim. width/2) + dim. width/2;

SnowY = rand. nextInt () % (dim. height/2) + dim. height/2;

}

Mt = new MediaTracker (this );

GAlc = new Image [1];

GAlc [0] = getImage (getDocumentBase (), getParameter ("graphic", "test.gif "));

Mt. addImage (gAlc [0], 0 );

Try

{

Mt. waitForID (0 );

}

Catch (InterruptedException ex) {return ;}

StopFlag = 0;

}

 

Public void start ()

{

If (mainThread = null)

{

MainThread = new Thread (this );

MainThread. start ();

}

}

Public void stop ()

{

MainThread = null;

}

Public void run ()

{

While (mainThread! = Null)

{

Try

{

Thread. sleep (threadSleep );

}

Catch (InterruptedException ex) {return ;}

Repaint ();

}

}

Public void drawBackSnow (Graphics g)

{

G. setColor (Color. white );

For (int I = 0; I <snows; I ++)

{

G. fillOval (snowX, snowY, snowSize, snowSize );

SnowX + = rand. nextInt () % 2 + wind;

SnowY + = (rand. nextInt () % 6 + 5)/5 + 1;

If (snowX> = dim. width) snowX = 0;

If (snowX <0) snowX = dim. width-1;

If (snowY> = dim. height | snowY <0)

{

SnowX = Math. abs (rand. nextInt () % dim. width );

SnowY = 0;

}

}

Wind = rand. nextInt () % 5-2;

}

Public void paint (Graphics g)

{

OffScreen. getGraphics (). setColor (Color. black );

OffScreen. getGraphics (). fillRect (0, 0, dim. width, dim. height );

OffScreen. getGraphics (). drawImage (gAlc [0], 0, 0, this );

DrawBackSnow (offScreen. getGraphics ());

G. drawImage (offScreen, 0, 0, null );

}

 

Public void update (Graphics g)

{

Paint (g );

 

}

}
Save as SnowPic. java


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.