Exemplo NetQuizXlet

Esse exemplo exercita o uso das seguintes funcionalidades:

  • Ciclo de vida de Xlet pelo pacote javax.tv.xlet
  • Pintura na tela com Graphics do pacote com.sun.dtv.lwuit..
  • Uso de Socket
import java.awt.Color;

import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import javax.tv.xlet.XletStateChangeException;

import com.sun.dtv.lwuit.Form;
import com.sun.dtv.lwuit.Graphics;
import com.sun.dtv.lwuit.Image;

public class NetQuizXlet extends Form implements Xlet {

    private static final String QUESTION_BEST_IMAGE = "best_question.png";
    private static final String QUESTION_WORST_IMAGE = "worst_question.png";
    private static final String RESULT_IMAGE = "result.png";
    private static final int SCREEN_BEST = 0;
    private static final int SCREEN_WORST = 1;
    private static final int SCREEN_RESULT = 2;

    private XletContext context;
    private Image bestImage = null;
    private Image worstImage = null;
    private Image resultImage = null;
    private int screen = 0;
    private Quiz quiz = null;
    private Graphics myGraphics = null;
    public void initXlet(XletContext ctx) throws XletStateChangeException {

        this.context = ctx;
        try {
            this.quiz = new NetQuiz();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        this.screen = SCREEN_BEST;
        try {
            this.bestImage = Image.createImage(QUESTION_BEST_IMAGE);
            this.worstImage = Image.createImage(QUESTION_WORST_IMAGE);
            this.resultImage = Image.createImage(RESULT_IMAGE);
        } catch (Exception e) {
        }
        setTitle("NetQuizXlet");
    }

    public void startXlet() throws XletStateChangeException {
        show();
    }

    public void paint(Graphics g) {

        super.paint(g);
        myGraphics = g;

        if (this.screen == SCREEN_BEST) {
            g.drawImage(this.bestImage, 10, 10);
        } else if (this.screen == SCREEN_WORST) {
            g.drawImage(this.worstImage, 10, 10);
        } else if (this.screen == SCREEN_RESULT) {
            g.drawImage(this.resultImage, 10, 10);

            final int MAX_WIDTH = 50;
            final int HEIGHT = 15;

            int goodSaude = this.quiz.getNumberOfBestVotes(Quiz.SAUDE);
            int badSaude = this.quiz.getNumberOfWorstVotes(Quiz.SAUDE);
            int votesSaude = goodSaude + badSaude;

            if (votesSaude > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(158 - (int) (MAX_WIDTH * ((float) goodSaude / votesSaude)),
                        93, (int) (MAX_WIDTH * ((float) goodSaude / votesSaude)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(215, 93, (int) (MAX_WIDTH * ((float) badSaude / votesSaude)), HEIGHT);
            }

            int goodPoliciamento = this.quiz.getNumberOfBestVotes(Quiz.POLICIAMENTO);
            int badPoliciamento = this.quiz.getNumberOfWorstVotes(Quiz.POLICIAMENTO);
            int votesPoliciamento = goodPoliciamento + badPoliciamento;

            if (votesPoliciamento > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(128 - (int) (MAX_WIDTH * ((float) goodPoliciamento / votesPoliciamento)),
                        129, (int) (MAX_WIDTH * ((float) goodPoliciamento / votesPoliciamento)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(243, 129, (int) (MAX_WIDTH * ((float) badPoliciamento / votesPoliciamento)), HEIGHT);
            }

            int goodCalcamento = this.quiz.getNumberOfBestVotes(Quiz.CALCAMENTO);
            int badCalcamento = this.quiz.getNumberOfWorstVotes(Quiz.CALCAMENTO);
            int votesCalcamento = goodCalcamento + badCalcamento;

            if (votesCalcamento > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(130 - (int) (MAX_WIDTH * ((float) goodCalcamento / votesCalcamento)),
                        165, (int) (MAX_WIDTH * ((float) goodCalcamento / votesCalcamento)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(238, 165, (int) (MAX_WIDTH * ((float) badCalcamento / votesCalcamento)), HEIGHT);
            }

            int goodTransporte = this.quiz.getNumberOfBestVotes(Quiz.TRANSPORTE);
            int badTransporte = this.quiz.getNumberOfWorstVotes(Quiz.TRANSPORTE);
            int votesTransporte = goodTransporte + badTransporte;

            if (votesTransporte > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(134 - (int) (MAX_WIDTH * ((float) goodTransporte / votesTransporte)),
                        200, (int) (MAX_WIDTH * ((float) goodTransporte / votesTransporte)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(235, 200, (int) (MAX_WIDTH * ((float) badTransporte / votesTransporte)), HEIGHT);
            }

            int goodIluminacao = this.quiz.getNumberOfBestVotes(Quiz.ILUMINCACAO);
            int badIluminacao = this.quiz.getNumberOfWorstVotes(Quiz.ILUMINCACAO);
            int votesIluminacao = goodIluminacao + badIluminacao;

            if (votesIluminacao > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(134 - (int) (MAX_WIDTH * ((float) goodIluminacao / votesIluminacao)),
                        235, (int) (MAX_WIDTH * ((float) goodIluminacao / votesIluminacao)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(235, 235, (int) (MAX_WIDTH * ((float) badIluminacao / votesIluminacao)), HEIGHT);
            }

            int goodLazer = this.quiz.getNumberOfBestVotes(Quiz.LAZER);
            int badLazer = this.quiz.getNumberOfWorstVotes(Quiz.LAZER);
            int votesLazer = goodLazer + badLazer;

            if (votesLazer > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(126 - (int) (MAX_WIDTH * ((float) goodLazer / votesLazer)),
                        271, (int) (MAX_WIDTH * ((float) goodLazer / votesLazer)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(243, 271, (int) (MAX_WIDTH * ((float) badLazer / votesLazer)), HEIGHT);
            }

            int goodSaneamento = this.quiz.getNumberOfBestVotes(Quiz.SANEAMENTO);
            int badSaneamento = this.quiz.getNumberOfWorstVotes(Quiz.SANEAMENTO);
            int votesSaneamento = goodSaneamento + badSaneamento;

            if (votesSaneamento > 0) {

                g.setColor(Color.GREEN);
                g.fillRect(130 - (int) (MAX_WIDTH * ((float) goodSaneamento / votesSaneamento)),
                        305, (int) (MAX_WIDTH * ((float) goodSaneamento / votesSaneamento)), HEIGHT);
                g.setColor(Color.RED);
                g.fillRect(238, 305, (int) (MAX_WIDTH * ((float) badSaneamento / votesSaneamento)), HEIGHT);
            }
        }
    }

    public void pauseXlet() {
    }

    public void destroyXlet(boolean unconditional)
            throws XletStateChangeException {
        context.notifyDestroyed();
    }

    public void keyPressed(int code) {
        System.out.println("Key: " + code);
        if (this.screen == SCREEN_BEST) {
            this.quiz.voteBest(code);
            this.screen = SCREEN_WORST;
        } else if (this.screen == SCREEN_WORST) {
            this.quiz.voteWorst(code);
            this.screen = SCREEN_RESULT;
        }
        this.repaint();
    }
}

QuizServer.zip (14.8 kB) Álan Livio, 18/10/2012 12:49

lesson41.zip (428.7 kB) Álan Livio, 18/10/2012 14:20