Java thread image loop Switching

Source: Internet
Author: User
Tags date1

Import java. AWT. image;
Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Import java. util. gregoriancalendar;
Import java. util. timer;
Import java. util. timertask;

Import javax. Swing. imageicon;

/**

* Multithreading is implemented by inheriting the timertask class.
*/

Public class mytimertask extends timertask {

Private int Index = 0;
Testpanel test = new testpanel ();
Private timer = new timer ();
Private Static final image pathtempimg = new imageicon ("C: // users // public // pictures // Sample Pictures // header0.jpg"). getimage ();
Private Static final image normalimg = new imageicon ("C: // users // public // pictures // Sample Pictures // header1.jpg"). getimage ();
Private Static final image activeimg = new imageicon ("C: // users // public // pictures // Sample Pictures // header2.jpg"). getimage ();
Private Static final image timg = new imageicon ("C: // users // public // pictures // Sample Pictures // header3.jpg"). getimage ();
Private Static final image fimg = new imageicon ("C: // users // public // pictures // Sample Pictures // header4.jpg"). getimage ();
Private Static final image frimg = new imageicon ("C: // users // public // pictures // Sample Pictures // header5.jpg"). getimage ();
Private Static final image simg = new imageicon ("C: // users // public // pictures // Sample Pictures // header6.jpg"). getimage ();
Private Static final image [] imagearray = new image [] {pathtempimg, normalimg, activeimg, timg, fimg, frimg, simg };
 
Date d = new date ();
Date D2 = new date ();
Calendar cpcalendar = new gregoriancalendar ();
Calendar cpcalendar2 = new gregoriancalendar ();
 
@ Override
Public void run (){
String date1 = "16:00:00 ";
String date2 = "18:36:00 ";
Simpledateformat F = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Try {
D = f. parse (date1 );
Cpcalendar. settime (d );

D2 = f. parse (date2 );
Cpcalendar2.settime (D2 );
} Catch (parseexception e ){
E. printstacktrace ();
}

Calendar C2 = calendar. getinstance ();
If (c2.gettimeinmillis () <cpcalendar. gettimeinmillis ()){
Test. setpanelimage (null );
System. Out. println ("the playlist playback time has not started! Please wait for ING ....");
}
If (c2.gettimeinmillis ()> cpcalendar. gettimeinmillis () & c2.gettimeinmillis () <cpcalendar2.gettimeinmillis ()){
System. Out. println ("Index =" + index );
If (Index = imagearray. Length ){
Index = 0;
}

Test. setpanelimage (imagearray [Index]);
Index ++;


System. Out. println ("thread running ing... current time:" + c2.gettime ());
}
If (c2.gettimeinmillis ()> cpcalendar2.gettimeinmillis ()){
This. Cancel ();
Timer = NULL;
Test. setpanelimage (null );
System. Out. println ("playlist completed! ");
Return;
}

}

Public void start (){
Timer. scheduleatfixedrate (this, new date (), 1000); // set the scheduled task execution time and its speed (once in 2 S)
System. Out. println ("enable thread: scheduled time:" + this. scheduledexecutiontime ());

}
Public long getscheduletime (){

Return this. scheduledexecutiontime ();

}
Public static void main (string [] ARGs ){

Mytimertask MTT = new mytimertask ();

MTT. Start ();

}

}

 

======================================

Import java. AWT. image;
Import java. Io. ioexception;
Import java. util. timer;

Import javax. Swing. box;
Import javax. Swing. imageicon;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;

Public class testpanel {

Private jframe F = new jframe ("test ");
Private box horizontal = Box. createhorizontalbox ();

Public void setpanelimage (image ){
Horizontal. removeall ();
Jtestpanel Panel = new jtestpanel (image );
Horizontal. Add (panel );
F. Add (horizontal );
F. setvisible (true );
}
/**
* Create the application.
*/
Public testpanel (){
Init ();
}

Public void Init (){
// Disable the window decorations (to maximize and minimize)
F. setundecorated (true );
// Top display
F. setalwaysontop (true );
// Set window maximization.
F. setextendedstate (javax. Swing. jframe. maximized_both );
F. getgraphicsconfiguration (). getdevice (). setfullscreenwindow (f );
// F. setsize (415,280 );
Image IMG = new imageicon ("C: // users // public // pictures // Sample Pictures // header6.jpg"). getimage ();
Jtestpanel Panel = new jtestpanel (IMG );

F. getlayeredpane (). Add (panel, new INTEGER (integer. min_value ));
Panel. setbounds (0, 0, new screensize (). getscreenwidth (), new screensize (). getscreenheight ());


(Jpanel) F. getcontentpane (). setopaque (false );
F. setdefaclocloseoperation (jframe. exit_on_close );
F. setvisible (true );
}

}

====================================

Import java. AWT .*;

Import javax. Swing .*;

@ Suppresswarnings ("serial ")
Public class jtestpanel extends jpanel {

Protected image;

Public jtestpanel (image ){

Super ();
This. Image = image;

}

Public void paintcomponent (Graphics g)
{
Super. paintcomponent (g );

// The parameter is used to locate the image.

If (this. image! = NULL) g. drawimage (this. Image, 0, 0,300,200, this );
}

}

 

====================================

/**
* Get screen resolution
* @ Author cmxie
*
*/
Public class screensize {
/**
* Screen width
*/
Private int screenwidth;
/**
* Screen height
*/
Private int screenheight;
/**
* Set the screen width.
* @ Param screenwidth
*/
Public void setscreenwidth (INT screenwidth ){
This. screenwidth = screenwidth;
}
/**
* Get the screen height
* @ Return
*/
Public int getscreenheight (){
Setscreenheight (INT) Java. AWT. toolkit. getdefatooltoolkit (). getscreensize (). Height );
Return screenheight;
}
/**
* Set the screen height.
* @ Param screenheight
*/
Public void setscreenheight (INT screenheight ){
This. screenheight = screenheight;
}
/**
* Obtain the screen width.
* @ Return
*/
Public int getscreenwidth (){
Setscreenwidth (INT) Java. AWT. toolkit. getdefatooltoolkit (). getscreensize (). width );
Return screenwidth;
}

Public static void main (string [] ARGs ){
System. Out. println ("width =" + new screensize (). getscreenwidth ());
System. Out. println ("heigh =" + new screensize (). getscreenheight ());
}
 
}

 

====================================

The next image is displayed every n seconds.

Locate the image display position by yourself.

 

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.