Presentation is loading. Please wait.

# Lab4: Theory + GUI Lecturer: Mauro Conti T.A.: Eyüp S. Canlar.

## Presentation on theme: "Lab4: Theory + GUI Lecturer: Mauro Conti T.A.: Eyüp S. Canlar."— Presentation transcript:

Lab4: Theory + GUI Lecturer: Mauro Conti T.A.: Eyüp S. Canlar

Outline Homework Discussion Theoretical Excercise Exercise involving GUI

Exercise 1 class C { private int i = 0; public synchronized void m() { for (int k=0; k<1000000; k++) i++; for (int k=0; k<1000000; k++) i--; i++; notifyAll(); } public synchronized void n() throws InterruptedException { if (i<9) wait(); System.out.print(i+" "); }

Exercise 1 class T1 extends Thread { private C c; public T1(C c) {this.c =c;} public void run() { for (int i=0; i<1000; i++) c.m(); } class T2 extends Thread { private C c; public T2(C c) {this.c = c;} public void run() { try { c.n();} catch(InterruptedException e){} }

Exercise 2 class Nodo{ private String info; private Nodo next; public Nodo(String s){info=s;} public void setNext (Nodo n) {next=n;} public static void stampaInfo(Nodo n){ synchronized(n) { If (n!=null) System.out.print(n.info + " \n"); } public synchronized void stampaAncheNext(){ System.out.print(info + " \n"); for(int i=0; i<9999999; ++i); stampaInfo(next); System.out.print("FINESTAMPA \n"); }

Exercise 2 class T extends Thread { private Nodo n; public T(Nodo n){this.n=n;} public void run(){ n.stampaAncheNext();} } class Esercizio { public static void main(String [] args){ Nodo uno = new Nodo("UNO"), due=new Nodo("DUE"); uno.setNext(due); due.setNext(uno); T[] array = {new T(uno), new T(due)}; int k = (int) (Math.random()*2); array[k].start(); array[(k+1)%2].start(); }

Exercise 3 You have to make a SWING application The application must have three buttons: Start, Pause, Stop When the Start is pressed a thread is created that changes the background color of the application every 30 ms The background color has to be selected randomly (e.g random RGB values) When you press the Pause button you have to suspend the thread. However, it should be still possible to restart the thread When Stop is pressed the thread should be stopped and it should not be possible to restart it again. Chapter 5 contains material about GUIs.

Exercise 3: Creating a GUI Public class Lab4Gui{ …. public void createGUI(){ JFrame guiFrame = new JFrame(Lab 4); Container cp = guiFrame.getContentPane(); JButton start = new JButton(Start); …. cp.add(start); …. guiFrame.pack(); guiFrame.setVisible(); }

Exercise 3: Events Public class Lab4GUI implements ActionListener{ …. public void ActionPerformed(ActionEvent e){ if(/* start is pressed */) /* create thread that changes bgc */ if(/* pause is pressed*/) /* pause bgc changing thread */ if(/* stop is pressed */) /* stop bgc changing thread */ } …. }

Exercise 3: Changing BGC Class ChangeBGCThread extends Thread{ …. public void run(){ int r, g, b …. while(/* some condition */){ /* Get random values for r, g, and b */ Color color = new Color(r, g, b); /* set background color */ …. /* repaint container */ …. } …. }

Download ppt "Lab4: Theory + GUI Lecturer: Mauro Conti T.A.: Eyüp S. Canlar."

Similar presentations

Ads by Google