Solved

How to convert java application using some data from a text file to an applet

Posted on 2007-11-14
6
322 Views
Last Modified: 2013-11-23
I've just started learning java, so I really need detailed information to understand. I made a java application that supposed to be placed on the website. When I finished someone told me that I can't put there application. It has to be an applet. How can I convert my application to an applet ? This is a quiz, questions and answers are contained in a text file. I need to put it on the website and don't know how to do that. Can you help me with the code please ? This is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;      //to handle RandomAccessFile
import javax.swing.border.*;

public class Quizza1 implements ActionListener{
      static FrameLayout fm;
      static int count=0;
      JDialog dialog;
      JComboBox l1;

            public void actionPerformed(ActionEvent a)
            {
                  count=Integer.parseInt(l1.getSelectedItem()+"");
                  fm.setQues(count);
                  dialog.dispose();
            }
            public Quizza1()
            {
              Dimension screenDim =Toolkit.getDefaultToolkit().getScreenSize();
                  dialog=new JDialog();
                  dialog.setTitle("No Of Questions");
                  dialog.setModal(true);
                  Container content=dialog.getContentPane();
                  dialog.setBounds((screenDim.width - 300) / 2,(screenDim.height - 100) / 2,300,100); // polozenie okienka wyb ilosci pytan
                  l1=new JComboBox();
                  JLabel label=new JLabel("Enter The Number Of Question/Session");
                  content.setLayout(new FlowLayout());
                  l1.addItem("5");
                     l1.addItem("10");
                    l1.addItem("15");  
                  JButton button=new JButton("OK");
                  button.addActionListener(this);
                  content.add(label);
                  content.add(l1);
                  content.add(button);
                  dialog.setVisible(true);            
            }

      public static void main(String args[]){

Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();                                    // ustala wymiary ekranu
            fm=new FrameLayout();                                                              
            fm.setSize(screenDim.width,screenDim.height);                                                           // rozmiar okna quizu
       









        Rectangle winDim = fm.getBounds();
        fm.setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);
            fm.setVisible(true);
            fm.setResizable(false);
            new Quizza1();

      /*****************************************************************
                  Handles events on Closing the Window
      ******************************************************************/
            fm.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JOptionPane op=new JOptionPane();
                  int aaa=op.showConfirmDialog(null,"DO U REALLY WANT TO EXIT","CONFIRM",JOptionPane.YES_NO_OPTION);      
                  if(aaa==0)
                  System.exit(0);
            }
            });
      }

}
      /**********************************************************************
            Creates the area where Questions are displayed
      ***********************************************************************/

class FrameLayout extends JFrame {
//      JScrollPane jsp=new JScrollPane();
      int count;



       
      public void setQues(int a)
      {
            count=a;
            QueNo.setText("Question No 1/"+count);
      }
      JTextArea TQuestion=new JTextArea();
      String Question[][] =new String[50][7];
      boolean Answer[][]=new boolean[50][6];
      Label QueNo =new Label();
      
      int maxQue=1;
      private MenuBar mainMenu;
      private Menu MFile;
      private MenuItem IStart;
      JCheckBox JAnswer[]=new JCheckBox[10];
      JButton JNext,JPrev,JGrade, button;
      static int QuestionNo=1;
      Questions Ques=new Questions();
      FrameLayout(){
            setTitle("Marta's simple quiz");      //title of the frame.
                   
            
            Container contentPane=getContentPane();





            TQuestion.setForeground(Color.white);

            TQuestion.setBackground(new Color(0,0,0));

                TQuestion.setFont(new Font("Seriff",Font.PLAIN,25));



            Question=Ques.getQuestions();
            Panel Top=new Panel();


            Top.setForeground(Color.white);
            Top.setBackground(new Color(0,0,0));
            Top.setLayout(new BorderLayout());
JButton button = new JButton();
button.setIcon(new ImageIcon("title.gif"));
button.setSize(0,0);
button.setContentAreaFilled(false);
Top.add(button,BorderLayout.NORTH);

            Top.add(QueNo,BorderLayout.WEST);            // bylo west
                Top.setFont(new Font("Seriff",Font.PLAIN,30)); // sama dorzucilam  
            
            TQuestion.setEditable(false);            //Questions are not Editable.
            JScrollPane jsp=new JScrollPane(TQuestion);            
            Top.add(jsp,BorderLayout.SOUTH);       // pokazuje nr pytania
            Panel center =new Panel();
            center.setLayout(new GridLayout(5,1)); // 5 pytan w jednym rzedzie

      /*********************************************************************
                        Creates the Options
      **********************************************************************/            

            for(int i=0;i<5;i++){
                  JAnswer[i] =new JCheckBox();
                  JAnswer[i].addItemListener(new ItemList());
                  JAnswer[i].setForeground(Color.black);
                  JAnswer[i].setBackground(new Color(225,221,217));
                  center.add(JAnswer[i]);
                  JAnswer[i].setFont(new Font("Seriff",Font.PLAIN,25)); // ust czcionki w odpowiedziach
            }
      /**********************************************************************
                        Creates the Buttons
      ***********************************************************************/

            Color fantastic=new Color(0,0,60); //belka ze strzalkami
            Panel bottom=new Panel();
            bottom.setLayout(new GridLayout(1,5));
            ImageIcon na=new ImageIcon("next111.gif");
            ImageIcon pa=new ImageIcon("prev111.gif");
                ImageIcon gr=new ImageIcon("grade111.gif");
            
                JNext=new JButton("Next",na);
            JNext.setBackground(fantastic);
            
                JPrev=new JButton("Previous",pa);
            JPrev.setEnabled(false);
            JPrev.setBackground(fantastic);
            
                JGrade =new JButton(gr);
            JGrade.setBackground(fantastic);
            
            bottom.add(JPrev);
            bottom.add(JNext);
            bottom.add(JGrade);
            bottom.setBackground(fantastic);            
            
            
            contentPane.add(Top,BorderLayout.NORTH);
//            center.setBackground(new Color(0,0,0));            
            contentPane.add(center,BorderLayout.CENTER);
            contentPane.add(bottom,BorderLayout.SOUTH);
            makeQueVis(1);
            
            ActionList a=new ActionList();
            a.setCount(count);
            JNext.addActionListener(a);
            JPrev.addActionListener(a);
            JGrade.addActionListener(a);
            }
      /***********************************************************************
                  Creates the Order of the Questions
       ***********************************************************************/

      void makeQueVis(int No){
            if(No>0 && No<51){
                  QueNo.setText("Question No " + No + "/"+count);
                  TQuestion.setText(Question[No-1][0]);
                  TQuestion.setRows(10);
                  if(maxQue<No){
                        maxQue=No;
                        for(int i=0;i<5;i++){
                              if(!Question[No-1][i+1].equals("")){
                                    JAnswer[i].setVisible(true);
                                    JAnswer[i].setSelected(false);
                                    JAnswer[i].setText(Question[No-1][i+1]);
                              }
                              else JAnswer[i].setVisible(false);
                        }
                        
                  }
                  else{
                        for(int i=0;i<5;i++){
                              if(!Question[No-1][i+1].equals("")){
                                    JAnswer[i].setVisible(true);
                                    JAnswer[i].setSelected(Answer[No-1][i]);
                                    JAnswer[i].setText(Question[No-1][i+1]);
                              }
                              else JAnswer[i].setVisible(false);
                        }
                        
                  }
            }
      }

      
      /**************************************************************************
                  Event Handling for the Questions
      ****************************************************************************/

class ActionList implements ActionListener{
      public void setCount(int aa)
      {
            count=aa;
      }
            public void actionPerformed(ActionEvent a){
                  if(a.getSource()==JNext){      
                        if(QuestionNo<50){
                              QuestionNo=QuestionNo+1;      //move to next question.
                              JNext.setEnabled(true);
                              JPrev.setEnabled(true);
                              if(QuestionNo>=count) JNext.setEnabled(false);//questions are ended.
                        }
                        makeQueVis(QuestionNo);
                  }
                  if(a.getSource()==JPrev){
                        if(QuestionNo>0){
                              QuestionNo=QuestionNo-1;      //move to previous question,
                              JNext.setEnabled(true);
                              JPrev.setEnabled(true);
                              if(QuestionNo<=1) JPrev.setEnabled(false);      //No questions .
                        }
                        makeQueVis(QuestionNo);
                  }
                  if(a.getSource()==JGrade){
                        Result fr=new Result(Quizza1.fm);      //calls Result class.
                  }
                  
            }
      }

      /****************************************************************************
                        Check the Answers here
      *****************************************************************************/
      class ItemList implements ItemListener{
            public void itemStateChanged(ItemEvent e)
            {
            JCheckBox jc=(JCheckBox)e.getItem();
            for(int i=0;i<5;i++)
      {
            if(e.getStateChange()==1)
            {
                  if(jc==JAnswer[i])
                  {
                        Answer[QuestionNo-1][i]=true;
                  }
            }
            
            else
            {
                  if(jc==JAnswer[i])
                  {
                        Answer[QuestionNo-1][i]=false;
                  }
            }
      }
            }
      }

}


      /*************************************************************************
                        Generate Questions Here
      **************************************************************************/
class Questions {
      RandomAccessFile file;
      String Question[][] =new String[50][7];
      Questions(){
            try{
                  file=new RandomAccessFile("sawal.txt","rw"); // zmienilam na plik html
            }
            catch(IOException e){System.out.println(e);}
      }

      String[][] getQuestions(){
            StringBuffer s1;
            try{
            for(int i=0;i<50;i++){
                  StringBuffer sb1=new StringBuffer();
                  b1: while(true){
                              String Temp=new String(file.readLine());
                              if(Temp.equals("##-----Question-----##")){
                                    int k;
                                    k=(int)Math.ceil(Math.random()*3);
                                    switch(k){
                                          case 2:
                                                break b1;
                                    }
                              }
                  }
                  while(true){
                        s1=new StringBuffer(file.readLine());
                        if(new String(s1).equals("##-----Question-----##")){
                              break;
                        }
                        else if(new String(s1).equals("##ans1##")){
                              Question[i][1]=file.readLine();
                        }
                        else if(new String(s1).equals("##ans2##")){
                              Question[i][2]=file.readLine();
                        }
                        else if(new String(s1).equals("##ans3##")){
                              Question[i][3]=file.readLine();
                        }
                        else if(new String(s1).equals("##ans4##")){
                              Question[i][4]=file.readLine();
                        }
                        else if(new String(s1).equals("##ans5##")){
                              Question[i][5]=file.readLine();
                        }
                        else if(new String(s1).equals("##correct##")){
                              Question[i][6]=file.readLine();
                        }
                        else{
                              sb1.append(s1);
                              sb1.append("\n");
                              Question[i][0]=new String(sb1);
                        }
                  }
            }
            }
            catch(IOException e){System.out.println(e);}
            return Question;

      }
}

      /******************************************************************************
                        Create the Result
      *******************************************************************************/
class Result extends JDialog {
      Label TotalQue =new Label("Total Question");
      Label TotalAns =new Label("Correct Answer");
      Label TotalWrg =new Label("In-Correct Answer");
      Label Per =new Label("Percentage Of Marks");
      Label Grade =new Label("Grade");
      JButton jb=new JButton("Ok-End Test");
      int correct;
      Result(final FrameLayout fm){
            setTitle("Result");
            setModal(true);
            for(int i=0;i<fm.maxQue;i++){
                  StringBuffer s=new StringBuffer();
                  for(int j=0;j<5;j++){
                        if(fm.Answer[i][j]==true){
                              s.append(j+1 + ",");
                        }
                  }
                  if(s.length()!=0){
                        s.deleteCharAt(s.length()-1);
                  }
                  if(fm.Question[i][6].equals(new String(s))){
                        correct=correct+1;
                  }
            }
            Container content=getContentPane();
            Panel con=new Panel();
            con.setLayout(new GridLayout(5,2));
            con.add(TotalQue);
            con.add(new Label("" + fm.maxQue));
            con.add(TotalAns);
            con.add(new Label("" + correct));
            con.add(TotalWrg);
            con.add(new Label((fm.maxQue-correct) + ""));
            con.add(Per);
            int l=0;
            l=correct*100/fm.maxQue;
            con.add(new Label(l+ " %"));
            con.add(Grade);
            if(l<71) con.add(new Label("Failed"));
            else con.add(new Label("Passed"));
            content.add(con,BorderLayout.NORTH);
            jb.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent a){
                        System.exit(0);
                  }
            });
            content.add(jb,BorderLayout.SOUTH);
            setSize(250,250);
        Dimension screenDim =
             Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle winDim =getBounds();
        setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);
            setVisible(true);
      }
}




      
      /******************************************************************
            Handles the Review Option in the Main Window
      *******************************************************************/

 class Final extends JDialog{
      private JButton Question[] =new JButton[50];
      FrameLayout cl=new FrameLayout();
      class ActionList implements ActionListener{
                  int i;
                  public void actionPerformed(ActionEvent a){
                        String s=a.getActionCommand();
                        if(s.length()==11){
                              s=s.substring(7,8);

                        }
                        else s=s.substring(7,9);
                        int i=new Integer(s).intValue();
                        cl.QuestionNo=i;
                        cl.makeQueVis(i);
                  }
                  ActionList(int i){
                        this.i=i;
                  }
      }
      Final(final FrameLayout cl,int count){
            setTitle("Review");
            setModal(true);
            this.cl=cl;
            int j=0;
            final Container panel=getContentPane();
            Panel pane=new Panel();
            if(cl.maxQue<5){
                  pane.setLayout(new GridLayout(2,5));
            }
            else if(cl.maxQue<20){
                  pane.setLayout(new GridLayout(4,5));
            }
            else if(cl.maxQue<30){
                  pane.setLayout(new GridLayout(6,5));
            }
            else if(cl.maxQue<50){
                  pane.setLayout(new GridLayout(8,5));
            }
            for(int i=0;i<cl.maxQue;i++){
                  j=i+1;
                  Question[i]=new JButton("Que No." + j +"/"+ count);
                  if(cl.Answer[i][5]==true){
                        Question[i].setBackground(Color.black);
                  }
                  Question[i].addActionListener(new ActionList(i));
                  pane.add(Question[i]);
            }
            Button ok=new Button("Ok");
            ok.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent a){
                        dispose();
                  }
            });
            panel.add(pane,BorderLayout.CENTER);
            panel.add(ok,BorderLayout.SOUTH);
            setSize(600,400);
        Dimension screenDim =
             Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle winDim = getBounds();
        setLocation((screenDim.width - winDim.width) / 2,
            (screenDim.height - winDim.height) / 2);
            setVisible(true);
            addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e){
                        }
            });
      }
}



*********** And this is fragment of the text file called Sawal.txt:
##-----Question-----##
Which state has a climate suitable for growing citrus fruits?

##ans1##
Maine

##ans2##
California

##ans3##
Texas

##ans4##
Montana

##ans5##
Arizona

##correct##
2
0
Comment
Question by:Maribell
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 500 total points
ID: 20280411
0
 

Author Comment

by:Maribell
ID: 20280569
Thanks for reply. My application reads a text file but the problem is that I don't know how to convert above application to an applet that it could be placed od the website.
0
 

Author Comment

by:Maribell
ID: 20285689
Does anyone know how to put this application and a text file on the website? I really need help. I made a mistake that I created an application. This is for my assignment, I suppose to create an applet that has to be put on the website. Unfortunatelly, because I'm beginner, I didn't notice the difference between an applet and the application. Now I have to submit it by Friday and it's loo late to create a completely new applet. I don't want to fail. Can anyone help me, please ? :(
0
 

Expert Comment

by:CharleneS77
ID: 20296489
Did you take the time to look at the links posted by rama?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now