// ================================================== // CIS 790 PROF. AUCIELLO /* THIS IS BASED ON F141 WHICH IS SINGLE-THREADED AND VERY SLOW AT DISPLAYING IMAGES. ATTEMPTS TO SPEED UP PROCESS WITH MULTI-THREADING AND (1) OTHER PROGRAMMING TECHNIQUE. Notes while programming: The init() method (procedure) will be the 1st Thread. This thread will load the arrays, which is time-consuming! PUT THE LOADING OF ARRAYS INTO RUN(), WHILE PAINT() DISPLAYS THE IMAGES. Question: While the "delay flicker" loop in Paint is operating, does the Load-Images routine steal cycles? 5:28am. Not threading .. DEFINITIONS: THREAD = SINGLE FLOW OF CONTROL WITHIN A PROGRAM. runs on its own while rest of program does other tasks. Demonstrates "Multi-tasking" ability of Java. MULTI-THREADING = MULTIPLE FLOWS OF CONTROL WITHIN PROGRAM. Concurrent Activities. Multi-Threads are not Parallelly-Processed. Each Thread competes for CPU time. Tasks with "POTENTIALLY LONG WAITS" should run as "THREADS"! When WAIT occurs, JAVA can shift to another task for execution. Topics: Threading (allowing multi-threading) Tracing Program Flow. Applet Program Cycle start(), run(), stop() methods. Text -- setColor(Color.xxx) Background Colors -- setBackground(Color.xxx) Standard color names and RGB values. */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.*; import java.io.*; import java.lang.Math; import java.lang.String; import java.lang.Thread; import java.lang.ThreadGroup; import java.net.*; import java.util.*; import java.util.Date; // extends runnable attributes to this applet (athread) public class f201b extends java.applet.Applet implements Runnable { // define all global variables. TextField inputField0,inputField1; Font theFont = new Font("TimesRoman",Font.BOLD,16); Date theDate; Thread runner; // the thread address is in runner. int x = 10; Image currentgif; // THIS DEFINES currentgif AS AN IMAGE. Image erasegif; Image gifarray[] = new Image[150]; String namearray[] = { "BLUE.GIF", "SD1.JPG", "SD2.JPG", "SD3.JPG", "SD4.JPG", "SD5.JPG", "SD6.JPG", "SD7.JPG", "SD8.JPG", "SD9.JPG", "SF0.JPG", "SF1.JPG", "SF2.JPG", "SF3.JPG", "SF4.JPG", "SF5.JPG", "SF6.JPG", "SF7.JPG", "IMAGE0.GIF", "IMAGE7A.GIF", "ALAN.GIF", "APRIL.GIF", "N.JPG", "H.JPG", "J.JPG", "ILDA.JPG", "DALI.JPG", "IMAGE001.JPG", "MONICA.GIF", "MAC.GIF", "ESCOBEDO.GIF", "MO3.JPG", "MO6.JPG", "W6.JPG", "W8.JPG", "BEATRIS.GIF", "IMAGE6.GIF", "IMAGE7A.GIF", "IMAGE12.GIF", "IMAGE14.GIF", "IMAGE15.GIF", "IMAGE16.GIF", "IMAGE23.GIF" }; int nal = namearray.length; int ilc; int speed = 30; // if there is no thread, start one, shift to run(). public void init() { System.out.println("runner @ start() time = " + runner); if ( runner == null); { runner = new Thread(this); runner.start(); inputField0 = new TextField(80);// create, add, and add (inputField0); // initialize input field1. inputField0.setText(" SLIDE SHOW JAVA APPLET PROGRAMMING (CIS 790) PROF. J. AUCIELLO"); Label namelabel4 = new Label("Speed: 1=fast 300=slow",Label.RIGHT); add (namelabel4); // def and add label. inputField1 = new TextField(7); // create, add, and add (inputField1); // initialize input field1. inputField1.setText("68"); Button button1 = new Button("Start"); // def button. add(button1); } } // control "c" takes control here. public void stop() { if (runner != null); // if not equal null, make it = null. { runner.stop(); runner = null; System.out.println("runner @ stop() time = " + runner); } } // infinite loop, gets and converts date, takes 1 second nap, // Java Run System leaves this routine briefly to attend to // paint() routine. public void run() { // start run() { System.out.println("Loading Thread is running -- (loading) run() method"); System.out.println(" loading(1) = " + runner); try { System.out.println("Timer Thread is running -- (timer) run() method"); System.out.println(" run(1) = " + runner); for ( int i=0; i < nal; i++ ) { gifarray[i] = getImage(getCodeBase(),namearray[i]); } // block started and ended. Thread.sleep(1000); String s1 = inputField1.getText(); speed = Integer.parseInt(s1); } //sleeps 10000 msecs. catch (InterruptedException e) {} } } // ends run. // ============================================================== // this displays images. public void paint(Graphics g) { // start 'paint' int x = 1; int hor = 1; String yourname = "f201b: PROF. AUCIELLO'S GALLERY (loading images into array ... ) "; Font fs = new Font("Sans Serif", Font.BOLD, 12); Font fm = new Font("Monospaced", Font.BOLD, 14); String s2 = inputField1.getText(); speed = Integer.parseInt(s2); g.setFont(fs); setBackground(Color.blue); g.setColor(Color.yellow); g.setFont(fm); hor = 220; ilc = 1; outerloop: while ( ilc < nal ) { // start outerloop. g.drawString(yourname, 20, 70); String strvar = namearray[ilc]; g.setColor(Color.red); g.drawString(strvar, 375, 85); g.drawString(yourname, 20, 70); g.drawString("Speed = " + speed, 275, 85); erasegif = gifarray[0]; g.drawImage(erasegif, hor-80, 100, this); currentgif = gifarray[ilc]; if (currentgif != null) { g.drawImage(currentgif, hor-80, 100, this); x = 1; while ( x < speed ) // (12 = 2 sec delay) (50 = 10 sec delay) // delay to reduce flicker. // syntax of "while" { System.out.println("x = " + x); g.drawString(" " + x, 1, 1); x ++; } // 'while started and ended g.setColor(Color.blue); g.drawString(strvar, 375, 85); repaint(); } // end "if" ilc = ilc + 1; } // end 'outerloop' } // end paint() public boolean action(Event event, int speed) { String s1 = inputField1.getText(); speed = Integer.parseInt(s1); repaint(); return true; } } // end program