Programme:Pkg client

Un article de UrdleWiki.

Jump to: navigation, search

QCM_client.java

package pkg_client;
import java.applet.Applet;
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.net.*;
 
 
public abstract class QCM_client extends Applet {
 
private static final long serialVersionUID = -7397539736573059655L;
 
private pkg_interface.CS_Interface b;
private pkg_interface.SessionID sessionID;
 
public QCM_client() {
}
 
public void destroy() {
try
{
b.endSession(sessionID);
} catch(RemoteException e) {};
b=null;
 
try{finalize();}catch(Throwable e) {};
System.out.println("Closing");
}
 
private void connect() {
b=null;
 
while (b==null)
{
try {
URL url = this.getDocumentBase();
String host=url.getHost();
Registry registry = LocateRegistry.getRegistry(host);
 
b = (pkg_interface.CS_Interface) registry.lookup("QCM_server");
} catch (RemoteException e) { //System.err.println(e.toString());
} catch (NotBoundException e) { //System.err.println(e.toString());
}
;
if (b==null) {
try{
Thread.sleep(1000);
} catch (InterruptedException e) {System.err.println(e.toString());};
}
}
}
 
protected void startSession(pkg_interface.questionnaries.Generic q) {
connect();
while (sessionID==null)
{
try {
sessionID = b.startSession(q);
} catch (RemoteException re) {
connect();
}
}
}
 
protected boolean answer(pkg_interface.Answer a) {
while(true) {
try {
return b.answer(sessionID,a);
} catch (RemoteException re) {
connect();
}
}
}
 
protected pkg_interface.Answer getCorrectAnswer() {
while(true) {
try {
return b.getCorrectAnswer(sessionID);
} catch (RemoteException re) {
connect();
}
}
}
 
protected pkg_interface.Question getNextQuestion() {
while(true) {
try {
return b.getNextQuestion(sessionID);
} catch (RemoteException re) {
connect();
}
}
}
 
protected pkg_interface.Score getScore() {
while(true) {
try {
return b.getScore(sessionID);
} catch (RemoteException re) {
connect();
}
}
}
}

DoubleProgressBar.java

package pkg_client;
 
import java.awt.Color;
import java.awt.HeadlessException;
 
public class DoubleProgressBar extends java.awt.Component {
 
private static final long serialVersionUID = 1837195749959919489L;
private int totalSize=60;
private int bar1Size=0;
private int bar2Size=0;
private Color bar1Color=Color.GREEN;
private Color bar2Color=Color.RED;
private String label="test";
 
public DoubleProgressBar(String arg0) throws HeadlessException {
label=arg0;
//setMaximumSize(new java.awt.Dimension(1000,1000));
//setMinimumSize(new java.awt.Dimension(10,10));
setPreferredSize(new java.awt.Dimension(100,20));
 
}
 
public void setTotalSize(int s) {
totalSize=s;
updatePercentLabel();
repaint();
}
 
public void setValues(int total, int first, int second) {
totalSize=total;
bar1Size=first;
bar2Size=second;
updatePercentLabel();
repaint();
}
 
private void updatePercentLabel() {
double sum=bar1Size+bar2Size;
double part=0.0;
 
if (sum==0)
{
part=0.0;
} else {
part=bar1Size/sum;
}
int percent=(int)(part*100);
label=percent+"% correct";
}
 
public void setFirstBar(int s) {
bar1Size=s;
updatePercentLabel();
repaint();
}
 
public void setSecondBar(int s) {
bar2Size=s;
updatePercentLabel();
repaint();
}
 
public void setBarColors(Color c1, Color c2)
{
bar1Color=c1;
bar2Color=c2;
repaint();
}
 
public void setLabel(String s)
{
label=s;
repaint();
}
 
public void paint(java.awt.Graphics g)
{
g.setPaintMode();
 
double maxSize=Math.max(totalSize,bar1Size+bar2Size);
int p1=(int)(getWidth()*(bar1Size/maxSize));
int p2=(int)(getWidth()*(bar2Size/maxSize));
 
g.setColor(bar1Color);
g.fillRect(0,0,p1,this.getHeight());
g.setColor(bar2Color);
g.fillRect(p1,0,p2,this.getHeight());
 
g.setColor(Color.BLACK);
java.awt.FontMetrics metric=g.getFontMetrics();
int sx=metric.stringWidth(label);
int sy=metric.getAscent()-metric.getDescent();
 
int px=(this.getWidth()-sx)/2;
int py=(this.getHeight()+sy)/2;
 
g.drawString(label,px,py);
}
}


QCM_client_KJfr.java

package pkg_client;
 
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
 
import pkg_interface.Answer;
import pkg_interface.Question;
import pkg_interface.Score;
import pkg_interface.questionnaries.KJ_fr;
 
public class QCM_client_KJfr extends QCM_client implements ActionListener {
private static final long serialVersionUID = 1103443651057845485L;
 
Button question;
Label question_comment=null;
ArrayList<Button> answers;
boolean waitNextQuestion=true; //if true, then the next click shows a new question
boolean noMoreQuestions=false;
boolean showQuestionComment=true;
DoubleProgressBar bar;
 
public QCM_client_KJfr() {
super();
 
Panel globalPanel=new Panel();
add(globalPanel);
 
Dimension dim=new Dimension(600,300);
globalPanel.setMaximumSize(dim);
globalPanel.setMinimumSize(dim);
globalPanel.setPreferredSize(dim);
 
bar=new DoubleProgressBar("--");
Panel qcmPanel=new Panel();
 
{ //Setting globalPanel layout
globalPanel.setBackground(Color.white);
 
java.awt.GridBagLayout gbL=new java.awt.GridBagLayout();
globalPanel.setLayout(gbL);
java.awt.GridBagConstraints gbC=new java.awt.GridBagConstraints();
gbC.fill=GridBagConstraints.HORIZONTAL;
gbC.fill=GridBagConstraints.BOTH;
gbC.weightx=1.0;
 
gbC.gridy=1;
gbC.weighty=0.1;
globalPanel.add(bar);
gbL.setConstraints(bar, gbC);
 
gbC.gridy=2;
gbC.weighty=0.9;
globalPanel.add(qcmPanel);
gbL.setConstraints(qcmPanel, gbC);
}
 
Panel panelQuestion=new Panel();
Panel panelAnswers=new Panel();
 
{ //Setting qcmPanel layout
qcmPanel.setBackground(Color.white);
java.awt.GridBagLayout gbL=new java.awt.GridBagLayout();
qcmPanel.setLayout(gbL);
java.awt.GridBagConstraints gbC=new java.awt.GridBagConstraints();
gbC.weighty=1.0;
gbC.fill=GridBagConstraints.BOTH;
 
gbC.gridx=1;
gbC.weightx=0.4;
qcmPanel.add(panelQuestion,gbC);
 
gbC.gridx=2;
gbC.weightx=0.6;
qcmPanel.add(panelAnswers,gbC);
}
 
question = new Button("Go");
question_comment = new Label("furigana",Label.CENTER);
{ //Setting panelQuestion Layout
question.addActionListener(this);
question.setBackground(Color.white);
 
java.awt.GridBagLayout gbL=new java.awt.GridBagLayout();
panelQuestion.setLayout(gbL);
java.awt.GridBagConstraints gbC=new java.awt.GridBagConstraints();
gbC.weightx=1.0;
gbC.fill=GridBagConstraints.BOTH;
 
gbC.gridy=1;
gbC.weighty=0.95;
panelQuestion.add(question,gbC);
 
gbC.gridy=2;
gbC.weighty=0.05;
panelQuestion.add(question_comment,gbC);
}
 
{ //Setting panelAnswers layout
panelAnswers.setLayout(new GridLayout(5,1));
answers=new ArrayList<Button>();
for (int i=0;i<5;i++)
{
Button a=new Button();
answers.add(a);
panelAnswers.add(a);
a.addActionListener(this);
a.setBackground(Color.white);
a.setVisible(false);
}
}
this.validate();
}
 
public void init() {
startSession(new KJ_fr(0,60));
this.setSize(600,305);
}
 
private void nextQuestion()
{
Question q=getNextQuestion();
if (q==null)
{
q=new Question("finished","finished",new ArrayList<String>());
noMoreQuestions=true;
 
Score s=getScore();
bar.setValues(s.totalQuestions,s.correctAnswers,s.wrongAnswers);
}
 
if (q!=null)
{
java.awt.Font font=question.getFont();
java.awt.Font fontQ=font.deriveFont(36.0f);
question.setFont(fontQ);
question.setLabel(q.question);
question_comment.setText(q.question_comment);
 
Iterator<Button> it=answers.iterator();
Iterator<String> qt=q.answers.iterator();
 
while (it.hasNext())
{
Button a=it.next();
if (qt.hasNext())
{
a.setLabel(qt.next());
a.setBackground(Color.WHITE);
a.setVisible(true);
} else {
a.setVisible(false);
}
}
waitNextQuestion=false;
Score s=getScore();
bar.setValues(s.totalQuestions,s.correctAnswers,s.wrongAnswers);
}
repaint();
}
 
public void actionPerformed(ActionEvent e) {
Object s=e.getSource();
 
if (!noMoreQuestions)
{
if (s==question)
{
nextQuestion();
} else {
Iterator<Button> it=answers.iterator();
 
while (it.hasNext())
{
Button a=it.next();
if (s==a)
{
if (waitNextQuestion)
{
nextQuestion();
} else {
boolean correct=answer(new Answer(a.getLabel()));
if (correct)
{
a.setBackground(Color.GREEN);
} else {
a.setBackground(Color.RED);
Answer goodAnswer=this.getCorrectAnswer();
Iterator<Button> jt=answers.iterator();
 
while (jt.hasNext())
{
Button b=jt.next();
if (b.getLabel().equals(goodAnswer.str))
{
b.setBackground(Color.GREEN);
}
}
}
waitNextQuestion=true;
}
}
}
}
}
}
}
développement