Color gradient and image transparency

Source: Internet
Author: User

Import java. Io. ioexception;

Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;

/**
*
* @ Author jagie
*
*/
Public class shadowmidlet extends MIDlet {
Canvas c = new shadowcanvas ();
Static shadowmidlet instance;
Public shadowmidlet (){
Instance = this;
}

Protected void Startapp () throws midletstatechangeexception {
Display. getdisplay (this). setcurrent (C );

}

Protected void pauseapp (){
// Todo auto-generated method stub

}

Protected void destroyapp (Boolean arg0) throws midletstatechangeexception {
// Todo auto-generated method stub

}
Public static void exitapp ()
{
Try
{
Instance. destroyapp (true );
Instance. policydestroyed ();
}
Catch (exception E)
{
}

}

}

/**
*
* @ Author jagie
*
*/
Class shadowcanvas extends canvas implements runnable {
Int W, h;

// Original Image
Image srcimage;

// Pixel array of the original image
Int [] srcrgbimage;

// The pixel array of the gradient image
Int [] shadowrgbimage;

Int imgwidth, imgheight;

Int count;

Public shadowcanvas (){
W = This. getwidth ();
H = This. getheight ();
Try {
Srcimage = image. createimage ("/av.png ");
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Imgwidth = srcimage. getwidth ();
Imgheight = srcimage. getheight ();
// Create the pixel array of the original image. An int is used to represent each pixel. The bitwise representation is 0 x1_rggbb.
Srcrgbimage = new int [imgwidth * imgheight];
// Obtain all pixels of the original image. For more information, see midp appi.
Srcimage. getrgb (srcrgbimage, 0, imgwidth, 0, 0, imgwidth, imgheight );

Shadowrgbimage = new int [srcrgbimage. Length];

System. arraycopy (srcrgbimage, 0, shadowrgbimage, 0,
Shadowrgbimage. Length );

// All pixels of the gradient image are completely transparent.
For (INT I = 0; I <shadowrgbimage. length; I ++ ){
Shadowrgbimage [I] & = 0x00ffffff;
}

New thread (this). Start ();

}
 
/**
* Obtain the color gradient RGB array,
* To obtain the data and be compatible with cldc1.0, a float class is added to the project.
* The program becomes larger.
* @ Param width
* @ Return
*/
Public final static int [] getshadecolor (INT color, int width ){
Int [] RGB;

Int shadewidth = width;
Int nrgbdata = shadewidth <2; // shadewidth * 4

RGB = new int [nrgbdata];

Intalpha =-127;
For (INT I = 0; I <shadewidth; I ++)
{
Alpha =-127 + I;
// The main algorithm is here.
Int Col = color | (128-Alpha <24 );
RGB [I] = Col;
RGB [I + shadewidth] = Col;
RGB [I + shadewidth * 2] = Col;
RGB [I + shadewidth * 3] = Col;
}
Return RGB;
}

Public final static void drawshaderect (Graphics g, int color, int X, int y, int width, int height ){
Int [] RGB = getshadecolor (color, width );
For (INT by = y; by <Y + height; by + = 2)
{
Int ntemp = Y + height-(by-y );
Ntemp = ntemp> 2? 2: ntemp;
G. drawrgb (RGB, 0, width, X, by, width,
Ntemp, true );
}
}

Int counter = 0;
Int rbgcolor = 0x00ff0000;
Public void keypressed (INT key)
{
System. Out. println ("key code =" + key );
If (Key =-6 | key =-7 | key =-21 | key =-22)
{
Shadowmidlet. exitapp ();
}
If (Key =-5)
{
If (counter % 4 = 0)
{
Rbgcolor = 0x00ffab00;
}
If (counter % 4 = 1)
{
Rbgcolor = 0x0000ff00;
}
If (counter % 4 = 2)
{
Rbgcolor = 0x000000ff;
}
If (counter % 4 = 3)
{
Rbgcolor = 0x00ff0000;
}
Counter ++;
Repaint ();
}

}
 

 
Public void paint (Graphics g ){
G. setcolor (0, 0, 0 );
G. fillrect (0, 0, W, H );

// Drawshaderect (G, 0x00ff0000, 0,100,240, 20 );
Drawshaderect (G, rbgcolor );

// Draw a gradient image
G. drawrgb (shadowrgbimage, 0, imgwidth, (W-imgwidth)/2,
(H-imgheight)/2, imgwidth, imgheight, true );
G. setcolor (0,255, 0 );
G. drawstring ("Count =" + count, W/2, 30, graphics. hcenter
| Graphics. Top );

}

Public void run (){
While (true ){

Boolean changed = false;
// Change each pixel of the gradient image
For (INT I = 0; I <shadowrgbimage. length; I ++ ){
// Obtain the Alpha value of a certain pixel of the gradient image
Int alpha = (shadowrgbimage [I] & 0xff000000) >>> 24;
// The Alpha value of the corresponding pixel of the original image
Int oldalpha = (srcrgbimage [I] & 0xff000000) >>> 24;

If (alpha <oldalpha ){
// Alpha value ++
Shadowrgbimage [I] = (alpha + 1) <24)
| (Shadowrgbimage [I] & 0x00ffffff );
Changed = true;
}
}

Try {
Thread. Sleep (2 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Count ++;
Repaint ();
// When the Alpha value of all pixels reaches the original value, the thread stops running.
If (! Changed ){
System. Out. println ("over ");
Break;
}
}

}

}

 

 

Final class magiccolorsutiles
{
// Grain = is the gradient Granularity
Public static final void drawstatebar (Graphics g, int RGB, int X, int y, int WID, int HIG, int grain)
{
Int [] rgb1 = {(RGB & 0xff0000) >>> 16, (RGB & 0x00ff00) >>> 8, RGB & 0x0000ff };
Int [] xyz = new int [3];
For (Int J = 0, K = 0; j <= HIG/2; j ++, K + = grain)
{
If (rgb1 [0] + k) <0xff)
XYZ [0] = rgb1 [0] + K;
Else
XYZ [0] = 0xff;

If (rgb1 [1] + k) <0xff)
XYZ [1] = rgb1 [1] + K;
Else
XYZ [1] = 0xff;
 
If (rgb1 [2] + k) <0xff)
XYZ [2] = rgb1 [2] + K;
Else
XYZ [2] = 0xff;
G. setcolor (XYZ [0], XYZ [1], XYZ [2]);
G. drawline (X, Y + J, x + WID, Y + J );
G. drawline (X, Y + hig-J, x + WID, Y + hig-j );
}
}

Public final static void drawshaderect (Graphics g, int color, int X, int y, int width, int height)
{
Int [] RGB = getshadecolor (color, width );
For (INT by = y; by <Y + height; by + = 4)
{
Int ntemp = Y + height-(by-y );
Ntemp = ntemp> 4? 4: ntemp;
G. drawrgb (RGB, 0, width, X, by, width, ntemp, true );
}

}
Public final static int [] getshadecolor (INT color, int width)
{
Int [] RGB;
Int shadewidth = width;
Int nrgbdata = shadewidth <2; // shadewidth * 4
RGB = new int [nrgbdata];
Intalpha =-127;
For (INT I = 0; I <shadewidth; I ++)
{
Alpha =-127 + I;
// The main algorithm is here.
Int Col = color | (128-Alpha <24 );
RGB [I] = Col;
RGB [I + shadewidth] = Col;
RGB [I + shadewidth * 2] = Col;
RGB [I + shadewidth * 3] = Col;
}
Return RGB;
}

}

 

 

 

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.