Thursday, April 14, 2016

List Memorizer

In this program, a number of items (input from user) are inserted into a list in random order. What the user has to do is to arrange the items in the correct order. The setDragEnabled() method is used here so that the content of the list can be transferred to text fields.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.datatransfer.*;

public class ListMemorizer extends JFrame{
    private JPanel mpanel=null;
    private JPanel panswer=null;
    private JPanel presult=null;
    private JTextArea talist=null;
    private ArrayList<String> arlist=null;
    private ArrayList<Integer> arlist2=null;
    private DefaultListModel listmdl=null;
    private JList list=null;
    private JButton bstart=null;
    private JTextField tfanswer[]=null;
    private JLabel lresult=null;
    
    public ListMemorizer(){
        super("List Memorizer");
        setSize(450,600);
        
        talist=new JTextArea();
        talist.setPreferredSize(new Dimension(400,200));
        talist.setLineWrap(true);
        listmdl=new DefaultListModel();
        list=new JList(listmdl);
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        list.setDragEnabled(true);
        JScrollPane spane = new JScrollPane(list);
        spane.setPreferredSize(new Dimension(400,100));
        bstart=new JButton("Start");
        bstart.addActionListener(new ButtonHandler());
        
        mpanel=new JPanel();
        mpanel.setLayout(new FlowLayout());
        mpanel.add(talist);
        mpanel.add(bstart);
        mpanel.add(spane);
        presult=new JPanel();
        presult.setPreferredSize(new Dimension(400,20));
        lresult=new JLabel();
        presult.add(lresult);
        mpanel.add(presult);
        panswer=new JPanel();
        panswer.setPreferredSize(new Dimension(400,200));
        panswer.setLayout(new FlowLayout());
        mpanel.add(panswer);
        add(mpanel);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String stemp=talist.getText();
            if(stemp==null){
                return;
            }
            
            int counter=talist.getLineCount();
        
            StringTokenizer stoken=new StringTokenizer(stemp);        
            arlist=new ArrayList<String>();
        
            for(int i=0;i<counter;i++){
                arlist.add(i,stoken.nextToken("\n"));
            }
            
            arlist2=new ArrayList<Integer>();
            Random rand=new Random();
            
            while(true){
                if(arlist2.size()==arlist.size()){
                    break;
                }
                int x=rand.nextInt(arlist.size());
                if(!arlist2.contains(x)){
                    arlist2.add(x);
                }
            }
            
            for(int i=0;i<arlist2.size();i++){
                listmdl.addElement(arlist.get(arlist2.get(i)));
            }
            
            talist.setText("");
            
            tfanswer=new JTextField[counter];
            
            for(int i=0;i<counter;i++){
                tfanswer[i]=new JTextField();
                tfanswer[i].setColumns(20);
                tfanswer[i].getDocument().addDocumentListener(new ListenText());
                panswer.add(tfanswer[i]);
            }
            mpanel.doLayout();
        }
    }
    
    class ListenText implements DocumentListener{
        public void changedUpdate(DocumentEvent e) {            
        }
        public void removeUpdate(DocumentEvent e) {    
        }
        public void insertUpdate(DocumentEvent e) {
            boolean answer=true;

            for(int i=0;i<tfanswer.length;i++){
                if(!tfanswer[i].getText().equals(arlist.get(i))){
                    answer=false;
                }
            }
            
            if(answer==false){
                return;
            }else{
                lresult.setText("OK");
            }
        }
    }    
    
    public static void main(String[] args){
        new ListMemorizer();
    }
}

No comments:

Post a Comment