Wox-xion Dev wiki

xvoe:packages:ui:core_ex

Exemple d'application : Grapher

Le projet sert d'exemple à l'utilisation de xvoe.

Il consiste en une classe principale instantiant l'application et lui ajoutant un objet Grapher.

L'objet Grapher est un simple composant graphique traçant une courbe en continu à l'écran. Il n'a pas d'intérêt si ce n'est qu'il montre la facilité d'exploitation des composants graphiques.

Classe principale grapher.Client

/*
 *  Client has been created on
 *  24 mars 2008, 15:42:18 by his author,
 *  Alexandre Kaspar
 */
package grapher;
 
import wx.xvoe.core.Application;
import wx.xvoe.persistance.Config;
import wx.xvoe.persistance.Logger;
import static wx.xvoe.debug.trace;
 
/**
 * Application de démarrage du client
 * 
 * @author Alexandre Kaspar
 */
public class Client {
 
    /**
     * Fonction première de démarrage
     * de l'application générale
     * 
     * @param args les arguments de lignes de commande
     */
    public static void main(String[] args) {
        try {
            if (Config.exists()) {
                Config.initialize();
            }
            Logger.initialize();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // nouvelle application de base
        Application app = new Application();
        app.init();
        // grapher
        Grapher graf = new Grapher();
        graf.x = 200;
        graf.y = 300;
        app.addChild(graf);
        // démarrage
        app.start();
    }
}

Classe grapher.Grapher

/*
 *  Grapher has been created on
 *  30 avr. 2008, 16:31:53 by his author,
 *  Alexandre Kaspar
 */
 
package grapher;
 
import java.awt.Color;
import wx.xvoe.graphics.GraphicSession;
import wx.xvoe.ui.framework.Component;
 
/**
 * Composant graphique affichant un graphe
 * 
 * @author Alexandre Kaspar
 */
public class Grapher extends Component{
    /**
     * Dernières valeurs
     */
    int lastX = 0, lastY = 0;
    /**
     * Largeur en pixels d'une unité sur le graphe
     */
    double unit = 50.0D;
    /**
     * Crée un grapheur
     */
    Grapher(){
        lastY = value(lastX);
    }
 
    /**
     * Crée le repère
     * 
     * @param gs la session graphique
     */
    public void init(GraphicSession gs){
        gs.setColor(Color.lightGray);
        gs.draw(gs.x -10, gs.y, gs.x + 410, gs.y);
        gs.draw(gs.x, gs.y - 200, gs.x, gs.y + 20);
    }
 
    /**
     * Dessine le graphe par morceaux
     * 
     * @param gs la session graphique
     */
    @Override
    public void drawComponent(GraphicSession gs){
        init(gs);
        //
        int c = 5;
        while(c-- > 0)
            drawSegment(gs);
    }
 
    /**
     * Dessine un segment du graphe
     * 
     * @param gs la session graphique
     */
    public void drawSegment(GraphicSession gs){
        gs.setColor(Color.black);
        int newY = value(lastX+1);
        if(Math.abs(lastY - newY) > 10000){
            gs.setColor(Color.cyan);
        }
        gs.draw(gs.x + lastX, gs.y + lastY, gs.x + lastX+1, gs.y + newY);
        lastY = newY;
        if(++lastX > 400){
            lastX = 1;
            lastY = value(1);
        }
    }
 
    /**
     * Renvoie l'ordonnée correspondant à une abcisse
     * 
     * @param _x l'abcisse à l'écran
     * @return l'ordonnée à l'écran
     */
    public int value(int _x){
        double x = _x/unit;
        double y = func(x);
        return - (int) Math.round(y * unit);
    }
 
    /**
     * Fonction affichée par le graphe
     * 
     * @param x l'abcisse du graphe
     * @return l'ordonnée du graphe
     */
    public double func(double x){
        return 1.0D / (x - x * Math.log(x));
    }
}

Résultat

Sans fichier de configuration, à l'exécution, on obtient normalement :

Grapheur

Télécharger

Les fichiers sont soumis aux mêmes droits que les librairies xvoe utilisées.

xvoe/packages/ui/core_ex.txt · Dernière modification: 2011/09/04 23:35 (modification externe)