Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Five java programs that demonstrate various concepts such as creating threads using runnable interface, synchronization in threads, creating frame windows in an applet, and drawing graphical shapes. The first program creates and starts three threads using the mythread class that implements runnable interface. The second program illustrates synchronization by having two threads print a counter. The third program creates a frame window within an applet. The fourth program creates a menu and submenu within an applet. The fifth program draws various shapes using the graphics class.
What you will learn
Typology: Exercises
1 / 7
package mythread; class MyThread implements Runnable { String name; Thread t; MyThread (String thread){ name = "threadname"; t = new Thread( this , name); System. out .println("New thread: " + t); t.start(); } public void run() { try { for ( int i = 5; i > 0; i--) { System. out .println(name + ": " + i); Thread. sleep (1000); } } catch (InterruptedException e) { System. out .println(name + "Interrupted"); } System. out .println(name + " exiting."); } } class MultiThread { public static void main(String args[]) { new MyThread("One"); new MyThread("Two"); new MyThread("Three"); try { Thread. sleep (10000); } catch (InterruptedException e) { System. out .println("Main thread Interrupted"); } System. out .println("Main thread exiting."); } }
class PrintDemo { public void printCount() { try { for ( int i = 5; i > 0; i--) { System. out .println("Counter --- " + i ); } } catch (Exception e) { System. out .println("Thread interrupted."); } } }
class ThreadDemo extends Thread { private Thread t; private String threadName; PrintDemo PD;
ThreadDemo( String name, PrintDemo pd) { threadName = name; PD = pd; }
public void run() { synchronized (PD) { PD.printCount(); } System. out .println("Thread " + threadName + " exiting."); }
public void start () { System. out .println("Starting " + threadName ); if (t == null ) { t = new Thread ( this , threadName); t.start (); } } }
public class SyncRunner {
public static void main(String args[]) { PrintDemo PD = new PrintDemo();
ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD ); ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );
T1.start(); T2.start();
// wait for threads to end try { T1.join(); T2.join();
} catch ( Exception e) { System. out .println("Interrupted"); } } }
package AppletWind;
//Create a child frame window from within an applet. import java.awt.; import java.awt.event.; import java.applet.; /
class SampleFrame extends Frame { SampleFrame(String title) { super (title); MyWindowAdapter adapter = new MyWindowAdapter( this );
addWindowListener(adapter); } public void paint(Graphics g) { g.drawString("This is in frame window", 10, 40); } } class MyWindowAdapter extends WindowAdapter { SampleFrame sampleFrame; public MyWindowAdapter(SampleFrame sampleFrame) { this .sampleFrame = sampleFrame; } public void windowClosing(WindowEvent we) { sampleFrame.setVisible( false ); }} public class AppletFrame extends Applet { Frame f; public void init() { f = new SampleFrame("A Frame Window"); f.setSize(250, 250); f.setVisible( true ); } public void start() { f.setVisible( true ); } public void stop() { f.setVisible( false ); } public void paint(Graphics g) { g.drawString("This is in applet window", 10, 20); } }
package appMenu;
import java.awt.*; class AWTMenu extends Frame { MenuBar mbar; Menu menu,submenu, chips; MenuItem m1,m2,m3,m4,m5,m9,m10, m11;
public AWTMenu() {
// Set frame properties setTitle("JOSEE AWT "); // Set the title setSize(400,400); // Set size to the frame setLayout( new FlowLayout()); // Set the layout setBackground(Color. magenta ); setVisible( true ); // Make the frame visible setLocationRelativeTo( null ); // Center the frame
// Create the menu bar mbar= new MenuBar();
// Create the menu menu = new Menu("Programmes"); chips= new Menu("Lecturer");
// Create the submenu submenu= new Menu("MSIt");
// Create MenuItems m1= new MenuItem("mobile programming"); m2= new MenuItem("Java programming"); m3= new MenuItem("LAN MAN"); m4= new MenuItem("computer architecture"); m5= new MenuItem(" Wed Development ");
m9= new MenuItem(" Mr. Lameck Nsama"); m10= new MenuItem("Mr. Thinakaran Anansingh"); m11 = new MenuItem("Mr. Chuunga "); // Attach menu items to menu menu.add(m1); menu.add(m2); menu.add(m3); chips.add(m9); chips.add(m10); chips.add(m11); // Attach menu items to submenu submenu.add(m4); submenu.add(m5);
// Attach submenu to menu menu.add(submenu);
// Attach menu to menu bar mbar.add(menu);
mbar.add(chips); // Set menu bar to the frame setMenuBar(mbar); } public static void main(String args[]) { new AWTMenu(); } }
package praphix;
import java.applet.; import java.awt.;
public class fillColor extends Applet{
public void paint(Graphics g){
g.drawLine(30,300,200,10); g.drawOval(250,50,100,100); g.drawRect(400,50,200,100);