| Source Code and Commented Algorithms |
|  |
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 15/03/97
// Fichier : alphabet.java
// -----------------------------------------------
// Objet : definition de police de caracteres
// constitues de point (comme sur les
// affichages "Reuters" de la Bourse.
// -----------------------------------------------
public final class alphabet {
public static byte DIGIT_WIDTH = 3;
public static byte DIGIT_HEIGHT = 5;
public static byte CHAR_WIDTH = 5;
public static byte CHAR_HEIGHT = 6;
static byte A[][]={{0,0,1,0,0},{0,1,0,1,0},{1,0,0,0,1},{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1}};
static byte B[][]={{1,1,1,1,0},{1,0,0,0,1},{1,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,0}};
static byte C[][]={{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,0},{1,0,0,0,0},{1,0,0,0,1},{0,1,1,1,0}};
static byte D[][]={{1,1,1,0,0},{1,0,0,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,1,0},{1,1,1,0,0}};
static byte E[][]={{1,1,1,1,1},{1,0,0,0,0},{1,1,1,0,0},{1,0,0,0,0},{1,0,0,0,0},{1,1,1,1,1}};
static byte F[][]={{1,1,1,1,1},{1,0,0,0,0},{1,1,1,0,0},{1,0,0,0,0},{1,0,0,0,0},{1,0,0,0,0}};
static byte G[][]={{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,0},{1,0,1,1,1},{1,0,0,1,0},{0,1,1,1,0}};
static byte H[][]={{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1}};
static byte I[][]={{0,1,1,1,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,1,1,1,0}};
static byte J[][]={{0,1,1,1,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{1,0,1,0,0},{0,1,0,0,0}};
static byte K[][]={{1,0,0,1,1},{1,0,1,0,0},{1,1,0,0,0},{1,0,1,0,0},{1,0,0,1,0},{1,0,0,1,1}};
static byte L[][]={{1,0,0,0,0},{1,0,0,0,0},{1,0,0,0,0},{1,0,0,0,0},{1,0,0,0,0},{1,1,1,1,1}};
static byte M[][]={{1,0,0,0,1},{1,1,0,1,1},{1,0,1,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1}};
static byte N[][]={{1,0,0,0,1},{1,1,0,0,1},{1,0,1,0,1},{1,0,0,1,1},{1,0,0,0,1},{1,0,0,0,1}};
static byte O[][]={{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}};
static byte P[][]={{1,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,0},{1,0,0,0,0},{1,0,0,0,0}};
static byte Q[][]={{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,1,1},{0,1,1,1,1}};
static byte R[][]={{1,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,0},{1,0,0,1,0},{1,0,0,0,1}};
static byte S[][]={{0,1,1,1,1},{1,0,0,0,0},{0,1,1,1,0},{0,0,0,0,1},{0,0,0,0,1},{1,1,1,1,0}};
static byte T[][]={{1,1,1,1,1},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0}};
static byte U[][]={{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}};
static byte V[][]={{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,0,1,0},{0,0,1,0,0}};
static byte W[][]={{0,0,0,0,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,1,0,1},{0,1,0,1,0}};
static byte X[][]={{0,0,0,0,0},{1,0,0,0,1},{0,1,0,1,0},{0,0,1,0,0},{0,1,0,1,0},{1,0,0,0,1}};
static byte Y[][]={{0,0,0,0,0},{1,0,0,0,1},{0,1,0,1,0},{0,0,1,0,0},{0,1,0,0,0},{1,0,0,0,0}};
static byte Z[][]={{0,0,0,0,0},{1,1,1,1,1},{0,0,0,1,0},{0,0,1,0,0},{0,1,0,0,0},{1,1,1,1,1}};
public static byte LETTRES[][][]={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
static byte C0[][]={{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}};
static byte C1[][]={{0,1,0},{1,1,0},{0,1,0},{0,1,0},{0,1,0}};
static byte C2[][]={{1,1,1},{0,0,1},{1,1,1},{1,0,0},{1,1,1}};
static byte C3[][]={{1,1,1},{0,0,1},{1,1,1},{0,0,1},{1,1,1}};
static byte C4[][]={{0,0,1},{0,1,1},{1,1,1},{0,0,1},{0,0,1}};
static byte C5[][]={{1,1,1},{1,0,0},{1,1,1},{0,0,1},{1,1,1}};
static byte C6[][]={{1,1,1},{1,0,0},{1,1,1},{1,0,1},{1,1,1}};
static byte C7[][]={{1,1,1},{0,0,1},{0,0,1},{0,0,1},{0,0,1}};
static byte C8[][]={{1,1,1},{1,0,1},{1,1,1},{1,0,1},{1,1,1}};
static byte C9[][]={{1,1,1},{1,0,1},{1,1,1},{0,0,1},{1,1,1}};
static byte POINT[][]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{1,0,0}};
public static byte CHIFFRES[][][]={C0,C1,C2,C3,C4,C5,C6,C7,C8,C9};
public static byte ESPACE = 2;
// espace entre chaque lettres d'un mot (en pixels)
alphabet() {};
}
// -----------------------------------------------
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 15/03/97
// Fichier : character_matrix.java
// -----------------------------------------------
// Objet : sous classe de matrix specialisee
// pour les mots.
// -----------------------------------------------
import matrix;
class character_matrix extends matrix {
// matrice de points correspondant a un caractere.
private void character_matrix_init(byte d[][]) {
set_size(alphabet.CHAR_WIDTH,alphabet.CHAR_HEIGHT);
init(d);
}
character_matrix(char my_char) {
// cette methode est un constructeur permettant de
// generer la matrice de point associe a un caractere.
// pour cela, il faut traduire le char en int puis
// chercher la valeur correspondante dans le tableau
// de matrice : alphabet.LETTRES (cf. fichier alphabet.java)
// exemple de conversion d'un caractere en entier. attention :
// il faut que le caractere soit un chiffre ! sinon levee d'une exception.
// int indice = java.lang.Integer.valueOf(java.lang.String.valueOf(my_char)).intValue();
String alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int indice = alph.indexOf(my_char);
// java.lang.Character.digit(my_char,3)
//- java.lang.Character.digit('A',3);
// System.out.print("Test de verification du constructeur character_matrix : ");
// System.out.println(indice);
character_matrix_init(alphabet.LETTRES[indice]);
}
}
// -----------------------------------------------
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 15/03/97
// Fichier : character_matrix.java
// -----------------------------------------------
// Objet : sous classe de matrix specialisee
// pour les mots.
// -----------------------------------------------
import matrix;
class character_matrix extends matrix {
// matrice de points correspondant a un caractere.
private void character_matrix_init(byte d[][]) {
set_size(alphabet.CHAR_WIDTH,alphabet.CHAR_HEIGHT);
init(d);
}
character_matrix(char my_char) {
// cette methode est un constructeur permettant de
// generer la matrice de point associe a un caractere.
// pour cela, il faut traduire le char en int puis
// chercher la valeur correspondante dans le tableau
// de matrice : alphabet.LETTRES (cf. fichier alphabet.java)
// exemple de conversion d'un caractere en entier. attention :
// il faut que le caractere soit un chiffre ! sinon levee d'une exception.
// int indice = java.lang.Integer.valueOf(java.lang.String.valueOf(my_char)).intValue();
String alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int indice = alph.indexOf(my_char);
// java.lang.Character.digit(my_char,3)
//- java.lang.Character.digit('A',3);
// System.out.print("Test de verification du constructeur character_matrix : ");
// System.out.println(indice);
character_matrix_init(alphabet.LETTRES[indice]);
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 1503/97
// Fichier : digit_matrix.java
// -----------------------------------------------
// Objet : la classe digit_matrix derivee de
// la classe matrix permet de generer
// des matrices simples pour gerer des
// chiffres.
// -----------------------------------------------
import matrix;
class digit_matrix extends matrix {
// matrice de points correspondant a un chiffre.
void digit_matrix_init(byte d[][]) {
set_size(alphabet.DIGIT_WIDTH,alphabet.DIGIT_HEIGHT);
init(d);
}
digit_matrix(char my_char) {
int indice = java.lang.Integer.valueOf(java.lang.String.valueOf(my_char)).intValue();
digit_matrix_init(alphabet.CHIFFRES[indice]);
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 1503/97
// Fichier : matrix.java
// -----------------------------------------------
// Objet : ensemble de classes basee sur la
// la classe "matrix" qui permet de
// conserver, modifier, afficher des
// ensembles de points.
// -----------------------------------------------
import java.awt.Graphics;
class matrix {
// matrice de point generale, de taille variable
// suivant les derivations.
int height,width;
byte data[][];
matrix() {}
void init(byte d[][]) {
data = d;
// System.out.print("Test matrix.init : ");
// System.out.println(data[0][0]);
}
protected void set_size(int w,int h) {
height = h;
width = w;
data = new byte[width][height];
}
int get_width() { return width; }
int get_height() { return height; }
protected void init_area(matrix m,int x,int y) {
// cette methode permet de "remplir" une zone de
// le matrice avec la sous-matrice d'un caractere
// ou bien d'un chiffre.
// System.out.print("Test matrix.init_area pour le parametre m : ");
// System.out.println(m.data[0][0]);
int largeur_ss_matrice = m.get_width();
int hauteur_ss_matrice = m.get_height();
// System.out.println("matrix.init_area :");
// System.out.println(largeur_ss_matrice);
// System.out.println(hauteur_ss_matrice);
int dx,dy;
for (dx = x; dx < x + hauteur_ss_matrice ; dx++)
for (dy = y ; dy < (y + largeur_ss_matrice); dy++) {
// System.out.print("Boucle for/for : dx = ");
// System.out.print(dx);
// System.out.print(" ; dy = ");
// System.out.println(dy);
// System.out.print("Just checking x et y : ");
// System.out.print(x);
// System.out.print(" ; ");
// System.out.print(y);
// System.out.print(" ; Avec width de data = ");
// System.out.println(get_width());
data[dx][dy] =
m.data[dx-x][dy-y];
}
}
byte [][] get_matrix() {
return data;
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 10/03/97
// Derniere mise a jour : 1503/97
// Fichier : number_matrix.java
// -----------------------------------------------
// Objet : la classe number_matrix derivee de
// la classe matrix permet de generer
// des matrices simples pour gerer des
// nombres.
// -----------------------------------------------
import matrix;
class number_matrix extends matrix {
// matrice de points correspondant a un nombre
// i.e. une union de matrices de chiffres.
void number_matrix_init(digit_matrix dig1,digit_matrix dig2,digit_matrix dig3) {
init_area(dig1,0,2);
init_area(dig2,0,2+alphabet.DIGIT_WIDTH+alphabet.ESPACE-1);
init_area(dig3,0,2+2*alphabet.DIGIT_WIDTH+3*alphabet.ESPACE-1);
}
number_matrix(String s) {
set_size(alphabet.DIGIT_HEIGHT,2+alphabet.DIGIT_HEIGHT*3+3*alphabet.ESPACE);
number_matrix_init(new digit_matrix(s.charAt(0)),
new digit_matrix(s.charAt(1)),
new digit_matrix(s.charAt(3)));
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 05/03/97
// Derniere mise a jour : 09/03/97
// Fichier : nyse_window.java
// -----------------------------------------------
// Objet : interface graphique de visualisation
// de quotations boursieres en temps
// reel.
// -----------------------------------------------
import java.awt.*;
import alphabet;
import matrix;
import word_matrix;
class Nyse_window extends Frame {
// constructeur
public Nyse_window() {
Panel stocks_panel = new Panel();
setBackground(Color.black);
setLayout(new BorderLayout());
// creation de la barre de menu
MenuBar menu_bar = new MenuBar();
Menu menu = new Menu("Action");
menu.add(new MenuItem("Quit"));
menu_bar.add(menu);
setMenuBar(menu_bar);
add("South",stocks_panel);
// Introduction d'une zone de dessin.
stocks_panel.setLayout(new GridLayout(1,2));
stocks_panel.add(new stocks_canvas());
}
public boolean action(Event event, Object arg) {
// cette fonction permet d'intercepter les actions
// des composants UI.
// target est une variable d'instance de l'evenement
// l'operateur instanceof permet alors de trouver le type
// du composant UI qui a genere l'action.
if (event.target instanceof MenuItem) {
if (((String) arg).equals("Quit")) System.exit(0);
}
return true;
}
public boolean handleEvent(Event event) {
// handleEvent est la fonction de AWT (Abstract Windowing Toolkit)
// qui permet de gerer les evenements simples.
// Ici, la redefinition de la methode handleEvent sert
// a gerer le "DESTROY" (click sur la croix dans W95).
if (event.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(event);
}
public static void main(String args[]) {
Nyse_window nw = new Nyse_window();
nw.setTitle("NYSE");
nw.pack();
nw.show();
}
}
class stocks_canvas extends Canvas {
public void paint(Graphics g) {
int w = size().width;
int h = size().height;
g.setColor(Color.green);
Font f = new Font("Courier",Font.PLAIN,18);
// g.setFont(f);
// g.drawString("This is the point of no return.",90,40);
// character_matrix cm = new character_matrix('B');
// draw_mat(g,60,100,cm);
// number_matrix nm = new number_matrix("12.3");
// draw_mat(g,60,200,nm);
// word_matrix wm = new word_matrix("ZSL");
// draw_mat(g,60,100,wm);
// stock_matrix sm = new stock_matrix("KLM","45.7");
// draw_mat(g,50,100,sm);
scroll(g);
}
private void wait (int n) {
int i,j;
for (i=n; i>0; i--) {
j=i*i*i;
}
}
private void draw_point(Graphics g,int x,int y) {
g.fillOval(x,y,4,4);
}
private void draw_char(Graphics g,int x,int y,byte D[][],int Lx,int Ly) {
int dx,dy;
for (dy = 0; dy < Ly ; dy++) {
for (dx = 0; dx < Lx ; dx++) {
if (D[dy][dx]==1)
draw_point(g,x+dx*5,y+dy*6);
}
}
}
private void draw_mat(Graphics g,int x,int y,matrix m) {
int dx,dy;
int Lx = m.get_width();
int Ly = m.get_height();
// System.out.println("Test de nyse_window.draw_mat :");
// System.out.println("Lx = ");
// System.out.print(Lx);
// System.out.print(" et Ly = ");
// System.out.println(Ly);
byte D[][] = new byte[Lx][Ly];
D = m.get_matrix();
System.out.println("Checkup de la taille de la matrice m : ");
System.out.print(m.get_width());
System.out.print(" ; ");
System.out.println(m.get_height());
for (dy = 0; dy < Ly ; dy++) {
for (dx = 0; dx < Lx ; dx++) {
if (D[dx][dy]==1)
draw_point(g,y+dy*6,x+dx*5);
System.out.print("Dx = "); System.out.print(dx);
System.out.print(" et Dy = "); System.out.println(dy);
}
}
}
private void draw_slice(Graphics g,int x,int y,matrix m,int position){
int Lx = m.get_width();
int Ly = m.get_height();
int dy;
byte D[][] = new byte[Lx][Ly];
D = m.get_matrix();
try {
for (dy = 0; dy < Lx ; dy++) {
if (D[dy][position]==1) draw_point(g,x,y+dy*5);
}
} catch (Exception e) {
System.out.println("Erreur : bornes de tableau non valides - cf. draw_slice");
}
}
private void scroll (Graphics g) {
// cette methode est tres generale, elle
// englobe la creation des matrices des caracteres representant
// les synoptiques des actions et effectue le scrolling.
// l'algo de scrolling est le suivant :
// on introduit les nouveau affichage sur une tranche (slice) de l'ecran
// situee a droite de l'ecran. Par exemple si on veut afficher un 'B', la
// premiere tranche sera une barre verticale (la barre du 'B').
// ensuite on fait des copier/deplacer de la zone d'ecran concernee
// il faut en faire 5 car la largeur de la tranche est 5. Ensuite,
// on recommence a afficher une tranche (la tranche suivante).
// remarque : les methodes copier/deplacer fournies par java semble
// plus efficace que la gestion des deplacements "a la main". en effet,
// la construction des caracteres implique des tests sur des matrices de
// definition de police de caracteres, et ces tests sont couteux car imbiques
// dans des boucles qui font des appels nombreux aux routines graphiques.
word_matrix tab[]= new word_matrix[30];
number_matrix tab_n[] = new number_matrix[30];
String tab_evolution[] = new String[30];
int share,i_share; // iterateur sur les 30 actions du Dow-Jones
int dx;
update upd = new update();
share = upd.connect_to_server(); // on recupere le nombre d'actions et
// en meme temps on met a jour le model
for (i_share = 0; i_share < share ; i_share++) {
String name = upd.get_name(i_share);
tab[i_share]=new word_matrix(name);
String value = upd.get_value(i_share);
tab_n[i_share]=new number_matrix(value);
tab_evolution[i_share]=upd.get_evolution(i_share);
}
int depl; // iterateur pour deplacer 5 fois la zone de la tranche
int largeur_mot = tab[0].get_height(); // oui je sais !
for (;;) {
for (i_share = 0; i_share < share ; i_share++) {
for (dx=0; dx < largeur_mot; dx++) {
// affichage de la tranche
switch(tab_evolution[i_share].charAt(0)) {
case 'd' : g.setColor(Color.red); break;
case 'u' : g.setColor(Color.green); break;
default : g.setColor(Color.blue);
}
draw_slice(g,500,30,tab[i_share],dx);
draw_slice(g,500,72,tab_n[i_share],dx);
// copier/deplacer
// arguments :
// x,y : coordonnees du coin superieur gauche de la zone a copier
// l,h : largeur et hauteur de la zone a copier
// dx,dy : deplacements par rapport a la position d'origine.
for (depl=0; depl < 5 ; depl++) {
g.copyArea(10,30,510,80,-1,0);
wait(6000);
} // boucle de copier/deplacer
} // Boucle sur l'affichage d'une action
for (depl=0; depl < 15 ; depl++) { // boucle pour espacer les actions
g.copyArea(10,30,510,80,-1,0);
wait(6000);
}
} // boucle sur les 30 actions du Dow-Jones
share = upd.connect_to_server(); // on recupere le nombre d'actions et
// en meme temps on met a jour le model
for (i_share = 0; i_share < share ; i_share++) {
String name = upd.get_name(i_share);
tab[i_share]=new word_matrix(name);
String value = upd.get_value(i_share);
tab_n[i_share]=new number_matrix(value);
tab_evolution[i_share]=upd.get_evolution(i_share);
}
} // boucle permanente
}
private void delete_screen(Graphics g,int x,int y) {
g.clearRect(x,y,39,39);
}
public Dimension minimumSize() {
return new Dimension(530,120);
}
public Dimension preferredSize() {
return minimumSize();
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , PK
// Auteur : Nicolas Vu Huu
// -----------------------------------------------
// Date creation : 18/03/97
// Derniere mise a jour : 20/03/97
// Fichier : update.java
// -----------------------------------------------
// Objet : module permettant d'effectuer les
// mises a jour des donnees.
// (nom des actions + valeurs).
// -----------------------------------------------
import java.net.*;
import java.io.*;
class update {
// initalize data table
String[][] tab = new String [50][3];
String[] tmpTab = new String[50];
// initialize a coordinate table
int histCoordinates[] = new int [5];
// initialize a bar coordinate table
int barCoordinates[] = new int [61];
// initialize a stock index value table
double stockIndexValues[] = new double [50];
// init. a color code table
int colorCode[] = new int [50];
update() {
System.out.println("Constructeur class update. ok.");
}
public String get_name(int yPos) {
return tab[yPos][0];
}
public String get_value(int yPos) {
return tab[yPos][1];
}
public String get_evolution(int yPos) {
return tab[yPos][2];
}
private void cleanTables() {
int i, j;
int items = 0;
String null_data = "";
for (i=0;i
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , Pekka Liikanen
// Auteur : Pekka Lukanen
// -----------------------------------------------
// Date creation : 05/03/97
// Derniere mise a jour : 19/03/97
// Fichier : histogram.java
// -----------------------------------------------
// Objet : interface graphique de visualisation
// de quotations boursieres en temps
// reel.
// -----------------------------------------------
import java.awt.*;
import histogram_canvas;
class histogram extends Frame {
// constructeur
public histogram() {
Panel histogram_panel = new Panel();
setBackground(Color.white);
setLayout(new BorderLayout());
// creation de la barre de menu
MenuBar menu_bar = new MenuBar();
Menu menu = new Menu("Action");
menu.add(new MenuItem("Quit"));
menu_bar.add(menu);
setMenuBar(menu_bar);
add("South",histogram_panel);
// Introduction d'une zone de dessin.
histogram_panel.setLayout(new GridLayout(1,2));
histogram_panel.add(new histogram_canvas());
}
public boolean action(Event event, Object arg) {
// cette fonction permet d'intercepter les actions
// des composants UI.
// target est une variable d'instance de l'evenement
// l'operateur instanceof permet alors de trouver le type
// du composant UI qui a genere l'action.
if (event.target instanceof MenuItem) {
if (((String) arg).equals("Quit")) System.exit(0);
}
return true;
}
public boolean handleEvent(Event event) {
// handleEvent est la fonction de AWT (Abstract Windowing Toolkit)
// qui permet de gerer les evenements simples.
// Ici, la redefinition de la methode handleEvent sert
// a gerer le "DESTROY" (click sur la croix dans W95).
if (event.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(event);
}
public static void main(String args[]) {
histogram h = new histogram();
h.setTitle("NYSE/HISTOGRAM");
h.pack();
h.show();
// System.out.println("coucou");
}
}
// -----------------------------------------------
// Projet : NYSE on line.
// Module : JAVA / SAR 6
// -----------------------------------------------
// Binome : Alberto Gianetti, Nicolas Vu Huu , Pekka Liikanen
// -----------------------------------------------
// Date creation : 05/03/97
// Derniere mise a jour : 19/03/97
// Fichier : histogram_canvas.java
// -----------------------------------------------
import java.io.*;
import java.awt.*;
import java.net.*;
class histogram_canvas extends Canvas {
private Font font, barFont;
// initalize data table
String[][] tab = new String [50][3];
String[] tmpTab = new String[50];
// initialize a coordinate table
int histCoordinates[] = new int [5];
// initialize a bar coordinate table
int barCoordinates[] = new int [61];
// initialize a stock index value table
double stockIndexValues[] = new double [50];
// init. a color code table
int colorCode[] = new int [50];
public void paint(Graphics g) {
updateGraphics(g);
}
public void updateGraphics(Graphics g) {
cleanTables();
clearHistogram(g);
int numberOfBars = readDataFromServer();
if (numberOfBars == 0) {
System.out.println("NO DATA RECEIVED FROM SERVER.. WILL TRY AGAIN!");
repaint();
}
parseServerData(numberOfBars);
drawBoundaries(g,numberOfBars);
try {
Thread.sleep(10000);
}
catch (Exception e) {}
repaint();
}
private void drawBoundaries(Graphics g,int numberOfBars) {
// define coordinates for histogram
int histWidth = 0;
int histHeight = 0;
int xCorner = 0;
int yCorner = 0;
if (numberOfBars < 15) {
histWidth = 500;
histHeight = 200;
xCorner = 30;
yCorner = 100;
}
else {
xCorner = 30;
yCorner = 100;
histWidth = numberOfBars*33+(int)(xCorner*0.4);
histHeight = 200;
}
// draw backround for the histogram
g.drawRect(xCorner, yCorner, histWidth, histHeight);
g.drawRect(xCorner-1, yCorner-1, histWidth+2, histHeight+2);
g.drawRect(xCorner-2, yCorner-2, histWidth+4, histHeight+4);
g.drawRect(xCorner-3, yCorner-3, histWidth+6, histHeight+6);
g.drawRect(xCorner-4, yCorner-4, histWidth+8, histHeight+8);
g.setColor(Color.white);
g.fillRect(xCorner, yCorner, histWidth, histHeight);
g.setColor(Color.black);
// store histograms coordinates
histCoordinates[0] = xCorner;
histCoordinates[1] = yCorner;
histCoordinates[2] = histWidth;
histCoordinates[3] = histHeight;
drawTitle(g, numberOfBars);
// define coordinates for scale
int xScaleStart = xCorner;
int yScaleStart = yCorner + (int)(histHeight*0.1);
int xScaleEnd = xCorner + (int)(histWidth*0.01);
int yScaleEnd = yScaleStart;
// draw scale for the coordinates
for (int i=1;i<11;i++) {
g.drawLine(xScaleStart,yScaleStart,xScaleEnd,yScaleEnd);
yScaleStart = yCorner + (int)(histHeight*0.1)*i;
yScaleEnd = yScaleStart;
drawBars(g,numberOfBars);
}
}
private void drawBars(Graphics g, int numberOfBars) {
// change the font
barFont = new Font("Helvetica",Font.BOLD, 10);
g.setFont(barFont);
// define variables for this function
//int numberOfBars = 0;
double heightOfBars = 0;
String element;
int xCorner = histCoordinates[0];
int yCorner = histCoordinates[1];
int xBarTitle = histCoordinates[0]+(int)(histCoordinates[0]*0.4);
int yBarTitle = histCoordinates[1]+(int)(histCoordinates[1]*0.1);
int histWidth = histCoordinates[2];
int histHeight = histCoordinates[3];
int barWidth = 0;
double absoluteBarHeight = 0;
int relativeBarHeight = 0;
// define the width of each bar
barWidth = (int)(histWidth/numberOfBars)- (int)(histWidth/numberOfBars*0.18);
// g.drawString(" width = "+barWidth,50,100);
// access the data elements
int k = 0;
int l = 0;
for (int i=0;i highestValue)
highestValue = stockIndexValues[i];
}
// draw the graph with bars and index values
int xBarCoord = histCoordinates[0]+(int)(histCoordinates[0]*0.4);
int yBarCoord = yCorner + histHeight;
int xIndexCoord = xBarCoord;
int yIndexCoord = 0;
for (int i=0;i
//-*-C++-*-
/*
* DJServer.java
* Projet Internet 'Dow Jones'
* aeg, 10/3/97
*/
/*
* Message format is: CompanyCode;Index;Variation!
* Example : PAR;10.4;S!IHM;23.0;D!
* Objects:
* Server controller
* File parser
* Data sender
*/
/**
* DJServer:
* Dow Jones server controller. Reads CGI script time parameter and
* regulary sends Dow Jones indexes to Stdout. Companies data is read
* from a text file.
* The server should patch the returned data with the HTML tags:
* "\n
* Dow Jones whatever...\n
* Here is the right data...\n
* \n"
*/
class DJServer {
public static void main(String args[]) {
Communicator serverCom = new Communicator(); // Communication interface
Parser fileParser = new Parser(); // The Dow Jones data file parser
Formatter iformat = new Formatter(); // Format Company indexes
// Call the Dow Jones parser on data file
fileParser.OpenFile();
// Prepare a new data message buffer
StringBuffer message = new StringBuffer();
// Now read every company record and append it to message buffer
while( fileParser.ReadNextRecord() != -1 ) {
// prepare a new chunk of message
StringBuffer company_data =
new StringBuffer(fileParser.GetCompanyCode());
company_data.append(";");
company_data.append(iformat.format_index(fileParser.GetCompanyIndex()));
company_data.append(";");
company_data.append(fileParser.GetCompanyVariation());
company_data.append("\n");
// Concatenate new company data
message.append(company_data);
}
// File reading terminated, close file
fileParse.CloseFile();
// Finally send data to client
serverCom.data_message(message.toString());
}
}
/**
* Communicator:
* This class is responsible to send data to Stdout and wrap it with the
* HTML page tags. It receives the Dow Jones data from the djserver.
*/
class Communicator {
public void data_message(String s) {
// head();
System.out.println(s);
// tail();
}
public void error_message(String error) {
this.head();
System.out.println("Error !");
System.out.println(error);
this.tail();
}
private void head() {
// Content-type (HTTP) is sent by CGI script
System.out.println("");
System.out.println("Dow Jones indexes");
System.out.println("");
}
private void tail() {
System.out.println("");
}
}
/**
* Formatter :
* just offer a method to format the company parsed index on 5 characters:
* examples:
*
* 12.9 ==> 12.90
* 1.34 ==> 01.34
* 12.55 ==> 12.55
*/
class Formatter {
public String format_index(String s) {
if( s.length() == 5 ) // no format needed, already 5 ch.
return s;
// copy user string in local buffer
StringBuffer new_string = new StringBuffer(5);
new_string.append(s);
// format img part, first verify '.' is on
if( new_string.toString().indexOf('.') == -1 )
new_string.append(".0"); // add pseudo-real part
if( new_string.toString().indexOf('.') < 2 )
new_string.insert(0, '0');
// format real part
if( new_string.length() != 5 )
new_string.append("0");
return new_string.toString();
}
}
/*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.io.*;
class DataIOTest {
public static void main(String[] args) {
// writing part
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("invoice1.txt"));
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
int[] units = { 12, 8, 13, 29, 50 };
String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
for (int i = 0; i < prices.length; i ++) {
dos.writeDouble(prices[i]);
dos.writeChar('\t');
dos.writeInt(units[i]);
dos.writeChar('\t');
dos.writeChars(descs[i]);
dos.writeChar('\n');
}
dos.close();
} catch (IOException e) {
System.out.println("DataIOTest: " + e);
}
// reading part
try {
DataInputStream dis = new DataInputStream(new FileInputStream("invoice1.txt"));
double price;
int unit;
String desc;
double total = 0.0;
try {
while (true) {
price = dis.readDouble();
dis.readChar(); // throws out the tab
unit = dis.readInt();
dis.readChar(); // throws out the tab
desc = dis.readLine();
System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
total = total + unit * price;
}
} catch (EOFException e) {
}
System.out.println("For a TOTAL of: $" + total);
dis.close();
} catch (FileNotFoundException e) {
System.out.println("DataIOTest: " + e);
} catch (IOException e) {
System.out.println("DataIOTest: " + e);
}
}
}
//-*-C++-*-
/*
* Parser.java
* Projet Internet SAR6 'Dow Jones'
* aeg, 10/3/97
*/
import java.io.DataInputStream;
import java.util.StringTokenizer;
/**
* Parser:
* It reads Dow Jones indexes data file one line at time and set data
* available for one company only.
*/
class Parser {
// Default data file
public static final String DATA_FILE = "index.data";
// Parser specific data
DataInputStream fr; // handles data file
String line;
// Company specific data
String company_code;
float index; // 1.0, 2.3, ...
char variation; // s, u, d
byte last_updating; // range 1 - 60
/**
* OpenFile():
* Open data file for parsing
*/
public int OpenFile() {
try {
fr = new DataInputStream(new FileInputStream(DATA_FILE));
}
catch( FileNotFoundException e ) {
System.out.println("file does not exists");
return -1;
}
return 0;
}
/**
* CloseFile():
* Close the parsed file.
*/
public int CloseFile() {
try {
fr.close();
}
catch( IOException e ) {
System.out.println("can't close file");
return -1;
}
return 0;
}
/**
* ReadNextRecord(): reads one line in the Dow Jones data file skipping
* comments AND empty lines.
* It stores one company data in internal variables.
*/
public int ReadNextRecord() {
// read one line, verify EOF and skip comment lines
try {
do {
if( (line = fr.readLine()) == null )
return -1; // EOF
} while( line.length() == 0 || line.charAt(0) == '!' );
}
catch( IOException e ) {
System.out.println("Can't read line");
return -1;
}
// set company data fields in internal variables
SetInternalData(line);
return 0;
}
/**
* SetInternalData(): sets internal company values from parsed line in
* data file.
*/
private void SetInternalData(String s) {
StringTokenizer st = new StringTokenizer(s);
// read company code
company_code = st.nextToken();
// read index
index = Float.valueOf(st.nextToken()).floatValue();
// read variation
variation = st.nextToken().toLowerCase().charAt(0);
// read last time updating
last_updating = (byte)Integer.valueOf(st.nextToken()).intValue();
}
/**
* The following functions returns the data related to the current parsed
* line.
*/
public String GetCompanyCode() {
return company_code;
}
public String GetCompanyIndex() {
return Double.toString(index);
}
public char GetCompanyVariation() {
return variation;
}
public int GetCompanyLastUpdating() {
return last_updating;
}
}
//-*-C++-*-
/*
* djdeamon.java
* Projet Internet SAR6 'Dow Jones'
* aeg, 19/3/97
*/
import java.io.*;
import Randomiser;
/**
* This deamon process regullary updates the Dow Jones data file with company
* records: Company code, index and variation (Still, Up, Down). the time
* parameter is not yet implemented. The file format is documented in the
* Parser.java class file.
*/
class DJDeamon {
public static final byte COMPANIES_NUM = 10;
public static String COMPANIES_ID[] = {
"IBM", "AOL", "INT", "MCD", "PTC",
"UPS", "FED", "MOT", "ITT", "DFR"
};
public static void main(String argv[]) {
FileWriter fc = new FileWriter();
Randomiser myr = new Randomiser();
float new_index;
/**
* Create a new array of float (need to store last index for
* every company.
*/
float[] indexes_table = new float[COMPANIES_NUM];
// init initial values at 10.00
for( int i = 0; i < COMPANIES_NUM; i++ )
indexes_table[i] = 10;
while ( true ) { // start deamon loop
// open the Dow Jones file for updating
fc.OpenFile();
for( int i = 0; i < COMPANIES_NUM; i++ ) {
// verfiy if the company got a new index
if( myr.still() ) {
new_index = indexes_table[i]; // no variation, Still
} else // Up OR Down
// generate new index value and variation for this company
new_index = myr.new_index(indexes_table[i]);
// update the file
fc.WriteNextRecord(COMPANIES_ID[i], indexes_table[i], new_index);
// new index now is old (already updated)
indexes_table[i] = new_index;
}
// close the file and wait for the next updating
fc.CloseFile();
try {
Thread.sleep(8000);
}
catch( Throwable t ) { }
} // end of deamon loop
}
}
/**
* FileWriter
* Phisically updates the "index.data" file from deamon parameters.
*/
class FileWriter {
public static final String DATA_FILE = "../index.data";
private PrintStream fw;
/**
* OpenFile():
* Open data file for writing.
*/
public int OpenFile() {
try {
fw = new PrintStream(new FileOutputStream(DATA_FILE));
}
catch( FileNotFoundException e ) {
System.out.println("file does not exists");
return -1;
}
catch( IOException e ) {
System.out.println("Open error");
}
fw.println("! Dow Jones data file");
fw.println("! Automatically updated by the djdeamon process\n");
return 0;
}
/**
* CloseFile():
* Close the file.
*/
public int CloseFile() {
fw.close();
return 0;
}
/**
* Here the FileWriter receives the new Company data from the deamon and
* updates the file with a new record (company line).
* The Open/Close operation must be done on the outsider level by the
* deamon class.
*/
public int WriteNextRecord(String company, float old_index, float new_index) {
StringBuffer line = new StringBuffer(30);
String s_new_index = Float.toString(new_index);
// patch the string for... (see later on substring operation :-))))
if( s_new_index.length() < 5 && new_index != 10.0 ) {
System.out.println("modifying string: " + s_new_index);
s_new_index = s_new_index.concat("000");
System.out.println("...to string: " + s_new_index);
}
// set the index String with two digits after '.'
int point_index = s_new_index.indexOf('.');
System.out.println(s_new_index);
String s_index = s_new_index.substring(0, point_index + 3);
// build the current company data line
line.append(company); line.append(" ");
line.append(s_index); line.append(" ");
// System.out.println("old index = " + old_index +
// "new index = " + new_index +
// "string new index = " + s_index);
if( old_index == new_index ) {
line.append("Still");
System.out.println("Variation is Still");
}
else if( old_index < new_index ) {
line.append("Up");
System.out.println("Variation is Up");
}
else {
line.append("Down");
System.out.println("Variation is Down");
}
line.append(" ");
line.append("0"); // last time updating
fw.println(line.toString());
return 1;
}
}
//-*-C++-*-
/*
* randomiser.java
* Projet Internet SAR6 'Dow Jones'
* aeg, 19/3/97
*/
import java.util.Random;
class Randomiser {
Random rg = new Random();
public float new_index(float previous) {
float newd = previous;
do {
newd = (float)((rg.nextFloat() % 0.99) + Math.abs((rg.nextInt() % 99)));
} while( newd < (previous - 10.0) || newd > (previous + 10.0) ||
newd < 10.0);
return newd;
}
/**
* Say if the company got a new index or not
*/
public boolean still() {
if( (rg.nextInt() % 5) == 0 )
return true; // Up or down
return false; // no variation, Still
}
}
|  |
|  |
|