الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

The ExamFramework 1.0

مغلق
بدأه mhewedy في 15 يونيو 2007 · 15 رد · 1,738 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته

أنا ، بفضل الله أولا و أخيرا ولا حول و لا قوة لي إلا به ، قمت بعمل framework ( لا أعرف ترجمه لها ) للإختبارات

فقط كل ماعليك هو أن تضع الاختبار اللي عاوز الطلاب يمتحنوه في صورة ملف xml ليه شكل معين - الشكل سهل جدا - و بس ، الطالب هيمتحن نفسه و يعرف إجابته لواحده .

أنا عامله open source يعني الكل يقدر يعدل فيه و يطوره ( :P أنا هعمل معاكم زي ما الشركات الكبيره بيعمل مع مجتمع ال open source ) ،،،

المهم الframework ده شغال تمام التمام و أي حد ممكن يستفيد منه ، بس ناقص حاجه واحده بس ، أحطع عداد تنازلي بالوقت الباقي ( دي مهمتكو بقي يا أبطال )

و أدي المصدر :


* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/

package client;

public class Answer {

//private boolean correct;
private char id;
private String value;
/*
public void setCorrect(boolean correct) {
this.correct = correct;
}

public boolean isCorrect() {
return correct;
}
*/
public void setId(char id) {
this.id = id;
}

public char getId() {
return id;
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
/**


* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/

package client;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import java.util.*;


public class MyHandler extends DefaultHandler {

private static List<Question> list;
// private int questionNo;
private Question q;
private char rightAns = '';
private boolean start = false;


public MyHandler(List<Question> list) {
this.list = list;
}

public void startElement(String nameSpace, String localName, String QName,
Attributes att) throws SAXException {

if (!localName.equals("questions")) {
start = true;
if (localName.equals("question")) {
q = new Question();
q.setNumber(Integer.parseInt(att.getValue(0)));
rightAns = att.getValue(1).charAt(0);
q.setCorrectAnswer(rightAns);
} else {
Answer answer = new Answer();
answer.setId(att.getValue(0).charAt(0));
/*if (rightAns == answer.getId())
answer.setCorrect(true);*/
q.addAnswer(answer);
}
}
}

public void endElement(String nameSapce, String localName,
String QName) throws SAXException {
try {
if (!localName.equals("questions")) {
if (/*q.getAnswers().get( 0).getValue() != null*/ localName.equals("question")) {

list.add(q);
q = null;
}
}
} catch (Exception e) {
// e.printStackTrace();
}
}

public void characters(char[] ch, int start,
int length) throws SAXException {
StringBuilder sb = new StringBuilder();

for (int i = start; i < start + length; i++) {
sb.append(ch);
}
String s = sb.toString();

try {
if (this.start) {
if (q.getValue() == null)
q.setValue(s);
else {
for (int i = 0; i < q.getAnswers().size(); i++) {
if (q.getAnswers().get(i).getValue() == null) {
q.getAnswers().get(i).setValue(s);
break;
}
}
}
}

} catch (Exception e) {
// e.printStackTrace();
}
}

public List<Question> getList() {
return list;
}

public static Question getQuestion (int index){
return list.get(index);
}
}
/**


* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/

package client;

import java.util.*;

public class Question {

private int number;
private String value;
private List<Answer> list;
private char correctAnswer;

public char getCorrectAnswer (){
return correctAnswer;
}

public void setCorrectAnswer ( char ca){
correctAnswer = ca;
}

public Question() {
list = new ArrayList<Answer>();
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public void addAnswer ( Answer answer ){
list.add(answer);
}

public List<Answer> getAnswers (){
return list;
}
}
/**


* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/


package client;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class Test extends JApplet implements ActionListener {

private JButton nxtBtn = new JButton("next");
private JButton prvBtn = new JButton("previous");
private JButton endBtn = new JButton("End");
// array of radio buttons to handle the answers
private JRadioButton[] radio = null;
private ButtonGroup group;

private JTextArea area;
private Container c;
private Panel p;

private java.util.List<Question> lst; // is a list of all questions in the xml file,
//of course with thier answers .

Map<Integer, String> map = new HashMap<Integer, String>(); // v.i.p map, it contains the user answer
//for each qustion
private char userAnswer; // hold the user answer

private int count = 0; // count of questions
private int score = 0; // score of the user
private boolean paint;

/*
* the init method of the applet
*/
public void init() {
try {
parse();

} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}

initComponent();
}
/*
* initialize the UI component
*/
public void initComponent() {
Container c = getContentPane();
this.c = c;
c.setLayout(new BorderLayout());
Panel downPnl = new Panel();
c.add(downPnl, BorderLayout.SOUTH);
downPnl.setLayout(new BorderLayout());
Panel radioPnl = new Panel();
radioPnl.setLayout(new FlowLayout());
downPnl.add(radioPnl, BorderLayout.CENTER);
p = radioPnl; // <======================
Panel navgatPnl = new Panel();
downPnl.add(navgatPnl, BorderLayout.SOUTH);
navgatPnl.setLayout(new BorderLayout());
Panel btnPnl = new Panel();
navgatPnl.add(btnPnl, BorderLayout.EAST);
btnPnl.setLayout(new GridLayout());
btnPnl.add(prvBtn);
btnPnl.add(nxtBtn);
btnPnl.add(endBtn);

nxtBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedNxtBtn(e);
}
});

prvBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedPrvBtn(e);
}
});

endBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedEndBtn(e);
}
});

displayAnswers(radioPnl, '$');
displayQuestion(c);

}


/*
* display questions , invoked every time the user presses next or previous
*/
public void displayQuestion(Container c) {
area = new JTextArea();

c.add(area, BorderLayout.CENTER);

StringBuilder question = new StringBuilder();
question.append(lst.get(count).getValue() + "\n\n");

for (int i = 0; i < lst.get(count).getAnswers().size(); i++) {
question.append(lst.get(count).getAnswers().get(i).getId() + "-" +
lst.get(count).getAnswers().get(i).getValue() +
"\n");
}

area.setText(question.toString());
area.setEditable(false);
}

/*
* display answers for the question , invoked every time the user presses next or previous
*/
public void displayAnswers(Panel p, char ch) {
int size = lst.get(count).getAnswers().size();
//System.out.println(size);
p.removeAll();
p.repaint();

radio = new JRadioButton[size];
group = new ButtonGroup();

for (int i = 0; i < size; i++) {
radio =
new JRadioButton(String.valueOf(lst.get(count).getAnswers().get(i).getId()));
radio.setActionCommand(new Character(lst.get(count).getAnswers().get(i).getId()).toString());

radio.addActionListener(this);
if (ch != '$') {
if (radio.getText().charAt(0) == ch)
radio.setSelected(true);
}
group.add(radio);
p.add(radio);
}
}


/*
* This method is the handlr for the buttton next
*/

public void actionPerformedNxtBtn(ActionEvent e) {
if (count < lst.size() - 1) {

count++;
displayQuestion(c);

//map.get(count).setSelected(true);
try {
char ch = map.get(count).charAt(0);
displayAnswers(p, ch);

} catch (NullPointerException ex) {
displayAnswers(p, '$');
}

}else
actionPerformedEndBtn(e);
}
/*
* This method is the handlr for the buttton previous
*/

public void actionPerformedPrvBtn(ActionEvent e) {
if (count > 0) {
count--;
displayQuestion(c);

try {
char ch = map.get(count).charAt(0);
displayAnswers(p, ch);

} catch (NullPointerException ex) {
displayAnswers(p, '$');
}

}
}
/*
* this method is the handler for the button End
*/

public void actionPerformedEndBtn(ActionEvent e) {
for (int i = 0; i < map.size(); i++) {
Question ques = MyHandler.getQuestion(i);
if (ques.getCorrectAnswer() == map.get(i).charAt(0)) {
score++;
}

}
c.removeAll();
c.repaint();
paint = true;
repaint();
}

public void paint(Graphics g){
if (paint){
//g.setFont(new Font ("sans", Font.BOLD, 20));
g.drawString ("You get : " + score + " from "+ lst.size(), 100, 100);
}
repaint();
}

/*
* is the method that do the parse by calling the Handler MyHandler
*/

public void parse() throws IOException, SAXException {

FileInputStream file = new FileInputStream(new File("data.xml"));
InputSource insrc = new InputSource(file);

XMLReader reader =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

java.util.List<Question> list = new ArrayList<Question>();
MyHandler hndlr = new MyHandler(list);

reader.setContentHandler(hndlr);

reader.parse(insrc);
lst = list;
}

/*
* radio buttons action method :
*/

public void actionPerformed(ActionEvent e) {
userAnswer = e.getActionCommand().charAt(0);
JRadioButton rb = (JRadioButton)e.getSource();
map.put(count, rb.getActionCommand());
}


}
/**


* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*
*
* This class needs your efforts
*/


package client;

import javax.swing.*;

public class TheTimer extends JLabel {

int time;

public TheTimer() {

}

}
/**

و ده مثال علي ملف الاسئله :


<questions>

<question no="1" correctAnswerId="a">
Can we extend an abstract class ?
<answer id="a"> yes </answer>
<answer id="b"> no, we can not </answer>
</question>

<question no="2" correctAnswerId="b">
can we instantiate a abastract class ?
<answer id="a"> yes </answer>
<answer id="b"> no </answer>
</question>

<question no="3" correctAnswerId="a">
What is your name ?
<answer id="a"> Omar </answer>
<answer id="b"> wael </answer>
<answer id="c"> mostafa </answer>
</question>

<question no="4" correctAnswerId="a">
How old are you ?
<answer id="a"> 20 </answer>
<answer id="c"> 40 </answer>
</question>

<question no="5" correctAnswerId="c">
what is the number of your building ?
<answer id="a"> 4 </answer>
<answer id="b"> 8 </answer>
<answer id="c"> 1 </answer>
<answer id="d"> 3 </answer>
</question>

<question no="6" correctAnswerId="b">
what does steal means in .net ?
<answer id="a"> the class can not be instantiated </answer>
<answer id="b"> the class can not be inhereted </answer>
</question>

</questions>
<?xml version="1.0" encoding="UTF-8" ?>

الشرح :

البرنامج يقوم بقراءق ملف xml و من ثم عرضه للممتحن و تسجيل إجاباته و عرضها في النهايه

الفئه Answer يمثل إجابه من الإجابات

الفئه Question تمثل السؤال

الفئه MyHandler هي خاصه بعمليه ال parse

الفئه Test هي بمثابة ال Main class في المشروع

لحاد كده كله تماتم ، المشروع شغال حلو

الفئه TheTimer تحت التطوير،

والمراد منها إظهار الوقت المتبقي من الامتحان، فكرت أن أعملها JLabel و أضعها علي ال Frame المصنوع في الفئه Test و لكني لم أنفذ، أرجو التطويير

**** ملحوظه هذا المشروع جزء من مشروع تحرجي الذي لم يسلم بعد *******

يوجد ملف zip مهم لجعل هذا المشروع يعمل ، سو أرفقه في المرفقات، كل ماعليك هو أن تفكه ثم تضعه في ال classpath بتاعك

في رعاية الله

extrace_me.zip

#2

مفيش حد عاوز يشترك !!!

:s

#3

لماذا استخدمت ملف xml بدل الداتابيز،

ولماذا لا تكون الواجهة jsp

#4

بالنسبه للداتابيز ===> أعتقد إستخدام ملفات ال xml يفوقها بمراحل ( الإنتقاليه - المعياريه ، ..............)

بالنسبه لواجهة ال jsp ، أنا لم أحتاج لل jsp في شئ ، فلم لا أستخدم مكونات ال swing الغنيه ?

#5

<quote>

لماذا استخدمت ملف xml بدل الداتابيز

</quote>

أأعتقد بذلك أصبح يمكن تشغيل البرنامج على أي نظام .. و لا داعي لتثبيت أي داتا بيز ,,, فقط قم بعمل ملف xml بواسطة أي note pade و تشوف النتيجة ...

SCJP 5.0

#6

أخي الكريم

لقد حاولت تشغيل برنامجك

لكن ظهر لي الخطأ التالي

java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

at org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:189)

at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:180)

at client.Test.parse(Test.java:236)

at client.Test.init(Test.java:52)

at sun.applet.AppletPanel.run(AppletPanel.java:378)

at java.lang.Thread.run(Thread.java:595)

java.lang.NullPointerException

at client.Test.displayAnswers(Test.java:135)

at client.Test.initComponent(Test.java:104)

at client.Test.init(Test.java:60)

at sun.applet.AppletPanel.run(AppletPanel.java:378)

at java.lang.Thread.run(Thread.java:595)

فهل هناك باكيج للبارسر

أم أن هذه الباكج تقوم باللازم

وشكراً

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#7

أهلاً ،

السوينق ثقيل على المستخدم وكذلك يحتاج على الاقل لوجود jre وحجمها تقريباً 100 ميقا لتشغيل البرنامج

الويب ابلكيشن أفضل من السوينق بحيث لايحتاج المستخدم إلا إلى براوزر لتشغيل برنامجك ...

حقيقه موضوع مميز وعقليه مميزه

#8

الأخ alaadiaa

ال بارسر بالمرفقات

الأخ عيسي:

حقا رأي أعتز به :D

بالنسبه لإختيار البريمج : نعم كما قلت ، لكن ربما كانت خبرتي لا تكفي لفعله بال jsp

#9

أخي مبتدئ جداً

لقد قمت بإضافة الباكج الموجودة في المرفقات مسبقاً

لو لاحظت أن المشكلة تتكلم عن org.apache.xerces.parsers.SAXParser

ولا أدري ما السبب؟

أنا استخدم eclipse 3.1 ,jdk 1.6

شكراً على اهتمامك أخي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#10

هل وضعته في ال classpath ؟؟

#11

أخي لقد قمت بإضافة الباكيج إلى برنامجي

فهل من الضروري إضافته إلى class path

وكيف أضيفه

شكراً

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#12

نعم أخي، يجب إضافته لل كلاس باس

ممكن تحطه هنا :

jdk/jre/lib/ext

#13

ممكن توضيح بسيط

هل الملف اللي باعمله run

هو ال test.java ؟؟؟

ولا كيف أعمل تشغيل للبرنامج

أنا حاليا أخذت الكود كما هو لكي أرى شكله

وأفكر في تحيوله إلة web application

لكن فقط دلني على البداية

شكرا

#14

الأخ معتز :

نعم test.java هو ال applet المسؤل عن التشغيل

اقتباس
وأفكر في تحيوله إلة web application

بالتوفيق ؛؛؛

اقتباس
البرنامج يقوم بقراءق ملف xml و من ثم عرضه للممتحن و تسجيل إجاباته و عرضها في النهايه

الفئه Answer يمثل إجابه من الإجابات

الفئه Question تمثل السؤال

الفئه MyHandler هي خاصه بعمليه ال parse

الفئه Test هي بمثابة ال Main class في المشروع

دوس على قد ما تقدر ، بس أنا أسف إن التعليقات موش كفايه،

الفئات Answer و Question موش محتاجين تعليقات ، فهما فئتان مباشرتان،

يتم معالجة ملف ال xml بإستخدام طريقة ال SAX و هي تتبع ال observer pattern ، و لذا الفئه MyHandler هي التي تقوم بعملية ال parse

/**
*	In the name of ALLAH, The gracious the merciful
*	This is the examframework version 1.0 by mohammed hewedy
*	you can use, modify and redistribute it as you like, no copyright or left owned by me
*	I hope you can understand it with its little comments
*/

package client;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import java.util.*;


public class MyHandler extends DefaultHandler {

	private static List<Question> list;
   // private int questionNo;
	private Question q;
	private char rightAns = '';
	private boolean start = false;


	public MyHandler(List<Question> list) {
		this.list = list;
	}

	public void startElement(String nameSpace, String localName, String QName,
							 Attributes att) throws SAXException {

		if (!localName.equals("questions")) {
			start = true;
			if (localName.equals("question")) {
				q = new Question();
				q.setNumber(Integer.parseInt(att.getValue(0)));
				rightAns = att.getValue(1).charAt(0);
				q.setCorrectAnswer(rightAns);
			} else {
				Answer answer = new Answer();
				answer.setId(att.getValue(0).charAt(0));
				/*if (rightAns == answer.getId())
					answer.setCorrect(true);*/
				q.addAnswer(answer);
			}
		}
	}

	public void endElement(String nameSapce, String localName,
						   String QName) throws SAXException {
		try {
			if (!localName.equals("questions")) {
				if (/*q.getAnswers().get( 0).getValue() != null*/ localName.equals("question")) {

					list.add(q);
					q = null;
				}
			}
		} catch (Exception e) {
		   // e.printStackTrace();
		}
	}

	public void characters(char[] ch, int start,
						   int length) throws SAXException {
		StringBuilder sb = new StringBuilder();

		for (int i = start; i < start + length; i++) {
			sb.append(ch);
		}
		String s = sb.toString();

		try {
			if (this.start) {
				if (q.getValue() == null)
					q.setValue(s);
				else {
					for (int i = 0; i < q.getAnswers().size(); i++) {
						if (q.getAnswers().get(i).getValue() == null) {
							q.getAnswers().get(i).setValue(s);
							break;
						}
					}
				}
			}

		} catch (Exception e) {
		  //  e.printStackTrace();
		}
	}

	public List<Question> getList() {
		return list;
	}

	public static Question getQuestion (int index){
		return list.get(index);
	}
}

أما عن تحويله ، فكل ما عليك هو تحويل الأبلت Test ل jsp/servlet

و الفئه Test عليها بعض التعليقات التي ممكن تساعدك

بالتوفيق، ولو في كود لم تستطع فهمه ( لسوء إسلوبي مثلا ) فلا تتردد في الإرسال لي،،، هنا أو على الخاص

و ربنا معاك

#15

أخي مبتدئ جافا

تم إضافة مؤقت باستخدام تعدد الخيوط multithreading

وهذه هي الفئة Test

لا أعلم كيف تريد أن يكون برنامجك لهذا لم أضف أزرار أو ماشابه

كل ما أضفته هو عداد تنازلي لعدد الثواني المعطاة للامتحان

/**
*	In the name of ALLAH, The gracious the merciful
*	This is the examframework version 1.0 by mohammed hewedy
*	you can use, modify and redistribute it as you like, no copyright or left owned by me
*	I hope you can understand it with its little comments
*/


package client;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class Test extends JApplet implements ActionListener {

	private JButton nxtBtn = new JButton("next");
	private JButton prvBtn = new JButton("previous");
	private JButton endBtn = new JButton("End");
	// array of radio buttons to handle the answers
	private JRadioButton[] radio = null;
	private ButtonGroup group;

	private JTextArea area;
	private Container c;
	private Panel p;
	private Label forTime=new Label();

	private java.util.List<Question> lst; // is a list of all questions in the xml file,
											//of course with thier answers .

	Map<Integer, String> map = new HashMap<Integer, String>();  // v.i.p map, it contains the user answer
																//for each qustion
	private char userAnswer; // hold the user answer
	private int time;//hold time in secons
	private int count = 0; // count of questions
	private int score = 0; // score of the user
	private boolean paint;
	private long remaningTime=60000;//the time still before finish
	/*
	 *  the init method of the applet
	 */
	public void init() {
		try {
			parse();
			Thread th=new Thread(new Runnable(){
				public void run(){
					while ( remaningTime != 0 )
					{
						setRemaningTime ( "" + remaningTime );
						try
						{
							Thread.sleep ( 1000 );
						}
						catch ( InterruptedException ex )
						{
							ex.printStackTrace ();
						}
						remaningTime --;
					}
					//here do waht you do when time finish
					//before student finish sol.
				}
				private void setRemaningTime(String string) {
					forTime.setText(string);
				}
			});
			th.start();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

		initComponent();
	}
	/*
	 *  initialize the UI component
	 */
	public void initComponent() {
		Container c = getContentPane();
		this.c = c;
		c.setLayout(new BorderLayout());
		Panel downPnl = new Panel();
		c.add(downPnl, BorderLayout.SOUTH);
		downPnl.setLayout(new BorderLayout());
		Panel radioPnl = new Panel();
		radioPnl.setLayout(new FlowLayout());
		downPnl.add(radioPnl, BorderLayout.CENTER);
		p = radioPnl; // <======================
		Panel navgatPnl = new Panel();
		downPnl.add(navgatPnl, BorderLayout.SOUTH);
		navgatPnl.setLayout(new BorderLayout());
		Panel btnPnl = new Panel();
		navgatPnl.add(btnPnl, BorderLayout.EAST);
		btnPnl.setLayout(new GridLayout());
		btnPnl.add(prvBtn);
		btnPnl.add(nxtBtn);
		btnPnl.add(endBtn);
		btnPnl.add(forTime);

		nxtBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						actionPerformedNxtBtn(e);
					}
				});

		prvBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						actionPerformedPrvBtn(e);
					}
				});

		endBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						actionPerformedEndBtn(e);
					}
				});

		displayAnswers(radioPnl, '$');
		displayQuestion(c);

	}


	/*
	 *  display questions , invoked every time the user presses next or previous
	 */
	public void displayQuestion(Container c) {
		area = new JTextArea();

		c.add(area, BorderLayout.CENTER);

		StringBuilder question = new StringBuilder();
		question.append(lst.get(count).getValue() + "\n\n");

		for (int i = 0; i < lst.get(count).getAnswers().size(); i++) {
			question.append(lst.get(count).getAnswers().get(i).getId() + "-" +
							lst.get(count).getAnswers().get(i).getValue() +
							"\n");
		}

		area.setText(question.toString());
		area.setEditable(false);
	}

	/*
	 *  display answers for the question , invoked every time the user presses next or previous
	 */
	public void displayAnswers(Panel p, char ch) {
		int size = lst.get(count).getAnswers().size();
		//System.out.println(size);
		p.removeAll();
		p.repaint();

		radio = new JRadioButton[size];
		group = new ButtonGroup();

		for (int i = 0; i < size; i++) {
			radio =
					new JRadioButton(String.valueOf(lst.get(count).getAnswers().get(i).getId()));
			radio.setActionCommand(new Character(lst.get(count).getAnswers().get(i).getId()).toString());

			radio.addActionListener(this);
			if (ch != '$') {
				if (radio.getText().charAt(0) == ch)
					radio.setSelected(true);
			}
			group.add(radio);
			p.add(radio);
		}
	}


	/*
	 *  This method is the handlr for the buttton next
	 */

	public void actionPerformedNxtBtn(ActionEvent e) {
		if (count < lst.size() - 1) {

			count++;
			displayQuestion(c);

			//map.get(count).setSelected(true);
			try {
				char ch = map.get(count).charAt(0);
				displayAnswers(p, ch);

			} catch (NullPointerException ex) {
				displayAnswers(p, '$');
			}

		}else
			actionPerformedEndBtn(e);
	}
	/*
	 *  This method is the handlr for the buttton previous
	 */

	public void actionPerformedPrvBtn(ActionEvent e) {
		if (count > 0) {
			count--;
			displayQuestion(c);

			try {
				char ch = map.get(count).charAt(0);
				displayAnswers(p, ch);

			} catch (NullPointerException ex) {
				displayAnswers(p, '$');
			}

		}
	}
	/*
	 * this method is the handler for the button End
	 */

	public void actionPerformedEndBtn(ActionEvent e) {
		for (int i = 0; i < map.size(); i++) {
			Question ques = MyHandler.getQuestion(i);
			if (ques.getCorrectAnswer() == map.get(i).charAt(0)) {
				score++;
			}

		}
		c.removeAll();
		c.repaint();
		paint = true;
		repaint();
	}

	public void paint(Graphics g){
		if (paint){
			//g.setFont(new Font ("sans", Font.BOLD, 20));
			g.drawString ("You get : " + score + " from "+ lst.size(), 100, 100);
		}
		repaint();
	}

	/*
	 *  is the method that do the parse by calling the Handler MyHandler
	 */

	public void parse() throws IOException, SAXException {

		FileInputStream file = new FileInputStream(new File("E:\\project\\examFramework\\client\\data.xml"));
		InputSource insrc = new InputSource(file);

		XMLReader reader =
			XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

		java.util.List<Question> list = new ArrayList<Question>();
		MyHandler hndlr = new MyHandler(list);

		reader.setContentHandler(hndlr);

		reader.parse(insrc);
		lst = list;
	}

	/*
	 * radio buttons action method :
	*/

	public void actionPerformed(ActionEvent e) {
		userAnswer = e.getActionCommand().charAt(0);
		JRadioButton rb = (JRadioButton)e.getSource();
		map.put(count, rb.getActionCommand());
	}

}

الله معك

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#16

جزاك الله خير أخي ، حقا مجهود رائع

إيه الحلاوه دي :D

نرجو من الإخوه مزيد من التعديل

هذا الموضوع مغلق.

مواضيع مشابهة