Professional Documents
Culture Documents
Objektivat:
T zbatohet hyrje interaktive, ku nj aplikacion mund t
ket interaksion me shfrytzuesin dhe t krkoj input
kur sht e nevojshme pr kompjutim.
T shfrytzohet inheritimi pr t disenjuar dritare
grafike q afishojn output n forma teksti, ngjyrash
dhe figurash.
T tregohet si nj objekt mund t mbaj mend
gjendjen e kompjutimit me an t variablave t
fushave.
Controller
main
Input View
Output View
import java.text.*;
import javax.swing.*;
/** FahrenheitToCelsius konverton vlern e temperaturs
* input: gradt Fahrenheit, int
* output: gradt Celsius, double
*/
public class FahrenheitToCelsius
{ public static void main(String[] args)
{ String input = JOptionPane.showInputDialog("Grad Fahrenheit:");
int f = new Integer(input).intValue();
double c = (5.0/9.0) * (f - 32);
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("Pr grad Fahrenheit " + f + ",");
System.out.println("grad Celsius " + formatter.format(c));
}
}
FahrenheitToCelsius
JOptionPane
main
System.out
println
showInputDialog
Integer
intValue
DecimalFormat
format
Dialogu dals
import java.text.*;
import javax.swing.*;
/** FahrenheitToCelsius konverton vlern e temperaturs
* input: gradt Fahrenheit, int
* output: gradt Celsius, double
*/
public class FahrenheitToCelsius
{ public static void main(String[] args)
{ String input = JOptionPane.showInputDialog("Grad Fahrenheit:");
int f = new Integer(input).intValue();
double c = (5.0/9.0) * (f - 32);
DecimalFormat formatter = new DecimalFormat("0.0");
JOptionPane.showMessageDialog(null, f + " Fahrenheit jan "
+ formatter.format(c) + " Celsius");
}
}
import javax.swing.*;
/** FrameTest krijon dhe afishon nj korniz */
public class FrameTest
{ public static void main(String[] args)
{ JFrame sampleFrame = new JFrame();
sampleFrame.setSize(300, 200);
sampleFrame.setVisible(true);
System.out.println("Korniza u shfaq!");
}
}
FrameTest
main(...)
{ samplePanel ==
sampleFrame ==
...
a1 : TestPanel
a1
a3
paintComponent(g == a2 )
{ ... }
Instruksionet nga klasa JPanel
}
a2 : Graphics
setColor(...) { ... }
drawString(...) { ... }
...
a3 : JFrame
setSize(...) { ... }
setVisible(...) { ... }
...
FrameTest
main
TestPanel
paintComponent
JFrame
getContentPane
setSize
Graphics
JPanel
setColor
drawString
setVisible
70
200
100
300
70
100
60
90
70
100
40
40
Metoda
Semantika
setColor(c)
Vizaton segmentin
drawString(s, x, y)
Vizaton drejtkndshin
Mbush drejtkndshin
Vizaton elipsn
Mbush elipsn
ka semantikn vijuese:
Konstruktohet nj ClockWriter objekt n memorien
qendrore;
Objektit t ri i drgohet mesazh pr t ekzekutuar
instruksionet n konstruktorin e tij, t quajtur poashtu
ClockWriter.
a2 : Graphics
setColor(...) { ... }
drawString(...) { ... }
...
FieldExample
main(...)
{ ... > }
a1 : FieldExample
private int count ==
paintComponent( g ==
{ count = count + 1;
...
}
a2
a3 : JFrame
setSize(...) { ... }
setVisible(...) { ... }
...
ClockWriter
JPanel
paintComponent
Graphics
GregorianCalendar
setColor
get
drawString
import java.awt.*;
import javax.swing.*;
import java.text.*;
/** Konverton vlern e temperaturs. */
public class FahrenheitToCelsiusWriter extends JPanel
{ private int fahrenheit;
private double celsius;
private int leftMargin = 20;
private int lineHeight = 20;
/** Konstruktori. */
public FahrenheitToCelsiusWriter()
{ String input = JOptionPane.showInputDialog("Grad Fahrenheit:");
fahrenheit = new Integer(input).intValue();
celsius = (5.0/9.0) * (fahrenheit - 32);
int frameHeight = 200;
JFrame f = new JFrame();
f.getContentPane().add(this);
f.setTitle("Celsius n Fahrenheit");
f.setBackground(Color.white);
f.setSize(3 * frameHeight / 2, frameHeight);
f.setVisible(true);
}