Смекни!
smekni.com

Створення навчальної програми по вивченню іноземної мови (стр. 8 из 10)

}

private void doLclick(int i){

Polekeys.cur=polekey[i].n;

// System.out.println(polekey[i].n);

//iskey=true;

//button.setEnabled(false);

rePaint();

}

private void initPole(){

if(Main.words.isStarted()){

int in = 0;

Polekeys.mindex=-1;

boolean fi = true;

Polekeys.c = Main.words.getCurword();

for(int i=0;i<Polekeys.length();i++){

polekey[i]=new Polekeys();

polekey[i].n=i;

if(!Polekeys.getIndexchr(i).matches(question)){

polekey[i].is=false;

} else {

polekey[i].is=true;

polekey[i].index=in;

if(fi) {

Polekeys.curind=in;

fi=false;

setKey(true);

Polekeys.cur=i;

}

Polekeys.mindex++;

in++;

}

}

}

}

/**

* This method initializes jPanel

*

* @return javax.swing.JPanel

*/

private JPanel getJPanel() {

if (jPanel == null) {

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 1;

gridBagConstraints.gridy = 0;

jPanel = new JPanel();

jPanel.setLayout(new GridBagLayout());

jPanel.setPreferredSize(new Dimension(820, 40));

jPanel.setBackground(bg);

jPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGH);

jPanel.add(getButton(), gridBagConstraints);

}

return jPanel;

}

/**

* This method initializes instructionMenuItem

*

* @return javax.swing.JMenuItem

*/

private JMenuItem getInstructionMenuItem() {

if (instructionMenuItem == null) {

instructionMenuItem = new JMenuItem();

instructionMenuItem.setText("Інструкція");

instructionMenuItem.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

Main.showHelp();

}

});

}

return instructionMenuItem;

}

/**

* Launches this application

*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

Test application = new Test();

application.getJFrame().setVisible(true);

}

});

}

void setKey(boolean b){

iskey=b;

if(b){

timer.start();

} else {

timer.stop();

}

pole.repaint();

}

void doSumary(){

int g = 0;

int b = 0;

String word = "";

for(int i=0;i<Polekeys.length();i++){

if(polekey[i].is){

word=word+polekey[i].ch;

if(polekey[i].ch.matches(Main.words.getOrigchr(i))){

g++;

}

b++;

} else {

word=word+Polekeys.getIndexchr(i);

}

}

Main.words.setShots(g, b);

Main.words.setDone(word);

}

}

class Polekeys {

static public int cur = -1;

static public String c;

static public int mindex = 0;

static public int curind;

public int x1;

public int x2;

public int n;

public boolean is = false;

public String ch = "?";

public int index = -1;

public String getChr(){

return getIndexchr(n);

}

static public String getIndexchr(int i){

return c.substring(i,i+1);

}

static int length(){

int l;

l=c.length();

if(l>Main.max_length) {

l=Main.max_length;

}

return l;

}

}

Лістинг Timer.java:

package mahno.masha.smallenglish;

class Timer extends Thread {

final static int sec=1000;

int ntimes = 0;

int curtime = 0;

int cur_test = -1;

Timer(int c,int n){

ntimes = n;

curtime = n;

cur_test = c;

setTime();

}

public void run() {

while(curtime>0 && Main.words.getCurent()==cur_test){

try {

Thread.sleep(sec);

Step();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(Main.words.getCurent()==cur_test) Done();

}

void setTime(){

Main.words.setTime(curtime);

}

void Step() {

curtime--;

setTime();

Main.test.rePaint();

}

void Done(){

Main.test.Click();

}

}

Лістинг Wave.java:

package mahno.masha.smallenglish;

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.FloatControl;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.UnsupportedAudioFileException;

public class Wave extends Thread {

private String filename;

private Position curPosition;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

enum Position {

LEFT, RIGHT, NORMAL

};

public Wave(String wavfile) {

filename = wavfile;

curPosition = Position.NORMAL;

}

public Wave(String wavfile, Position p) {

filename = wavfile;

curPosition = p;

}

public void run() {

File soundFile = new File(filename);

if (!soundFile.exists()) {

System.err.println("Wave file not found: " + filename);

return;

}

AudioInputStream audioInputStream = null;

try {

audioInputStream = AudioSystem.getAudioInputStream(soundFile);

} catch (UnsupportedAudioFileException e1) {

e1.printStackTrace();

return;

} catch (IOException e1) {

e1.printStackTrace();

return;

}

AudioFormat format = audioInputStream.getFormat();

SourceDataLine auline = null;

DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try {

auline = (SourceDataLine) AudioSystem.getLine(info);

auline.open(format);

} catch (LineUnavailableException e) {

e.printStackTrace();

return;

} catch (Exception e) {

e.printStackTrace();

return;

}

if (auline.isControlSupported(FloatControl.Type.PAN)) {

FloatControl pan = (FloatControl) auline

.getControl(FloatControl.Type.PAN);

if (curPosition == Position.RIGHT)

pan.setValue(1.0f);

else if (curPosition == Position.LEFT)

pan.setValue(-1.0f);

}

auline.start();

int nBytesRead = 0;

byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try {

while (nBytesRead != -1) {

nBytesRead = audioInputStream.read(abData, 0, abData.length);

if (nBytesRead >= 0)

auline.write(abData, 0, nBytesRead);

}

} catch (IOException e) {

e.printStackTrace();

return;

} finally {

auline.drain();

auline.close();

}

}

}

Лістинг Words.java:

package mahno.masha.smallenglish;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.Image;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.math.BigDecimal;

import java.util.Date;

import javax.imageio.ImageIO;

class Words {

final static int max = 100;

final static String imgdir = "images/";

final static String snddir = "sounds/";

Timer timer;

static int n = 0;

static int orig;

static int cur_test = -1;

static String workdir;

static String ldir;

int all_clear = 0;

int all = 0;

String name;

String ftxt;

String[] word_rus = new String[max];

String[] word_orig = new String[max];

String[] word_test = new String[max];

String[] word_done = new String[max];

String[] image = new String[max];

String[] wav = new String[max];

int[] time = new int[max];

int[] clear_shots = new int[max];

int[] bad_shots = new int[max];

final static String defimg = "system/logo.jpg";

final static String defwav = "system/none.wav";

final static String lisimg = "system/listen.jpg";

boolean bwav=false;

int cur_time;

Image img;

Image listen;

Words(String f,String l,String n,String t){

workdir = f;

ldir = l;

ftxt=t;

name=n;

loadLImg();

readFiles();

loadImg(defimg);

}

public int getTime() {

return cur_time;

}

public void setTime(int t) {

cur_time=t;

}

public void setDone(String s){

word_done[cur_test]=s;

}

public void getImage(Graphics2D gr){

gr.drawImage(img, 0, 0, 800,600, null);

}

public void sndIcon(Graphics2D gr){

if(isWav()){

gr.setColor(Color.gray);

gr.fillRoundRect(664, 464, 100, 100, 5, 5);

gr.setColor(Color.white);

gr.fillRoundRect(660, 460, 100, 100, 5, 5);

gr.setColor(Color.black);

gr.drawRoundRect(660, 460, 100, 100, 5, 5);

gr.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));

gr.drawString("listen...",690,545 );

gr.drawImage(listen, 665, 465, 90,90, null);

}

}

public void drawRus(Graphics2D gr){

if(isStarted()){

gr.setColor(Color.gray);

gr.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 80));

gr.drawString(word_rus[cur_test], 28,543 );

gr.setColor(Color.black);

gr.drawString(word_rus[cur_test], 24,539 );

gr.drawString(word_rus[cur_test], 26,541 );

gr.setColor(Color.white);

gr.drawString(word_rus[cur_test], 25,540 );

if(time[cur_test]>0){

gr.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));

gr.setColor(Color.black);

gr.drawString(String.valueOf(cur_time)+" секунд", 651,31);

gr.setColor(Color.white);

gr.drawString(String.valueOf(cur_time)+" секунд", 649,29);

gr.setColor(Color.red);

gr.drawString(String.valueOf(cur_time)+" секунд", 650,30);

}

}

}

int getProcent() {

double pr;

pr = (100.00 / all) * all_clear;

BigDecimal x = new BigDecimal(pr);

return x.setScale(0, BigDecimal.ROUND_HALF_DOWN).intValue();

}

int getBale() {

float procent;

procent = getProcent();

if(procent<50){

return 2;

} else

if (procent<=75) {

return 3;

} else

if (procent<=95){

return 4;

} else

if (procent>95) {

return 5;

} else {

return -1;

}

}

public void getSummaryG(Graphics2D gr){

String s[]={"Результати тестування "+name+" (урок "+ldir+"):&bsol;n","Загальне число балів "+all_clear+" из "+all+".&bsol;n ","Оцінка: "+getBale()+" ("+getProcent()+"% успіху)&bsol;n"};

gr.setColor(Color.black);

gr.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 22));

gr.drawString(s[0], 20,25);

gr.setFont(new Font(Font.SANS_SERIF, 0, 25));

gr.drawString(s[1], 20,50);

gr.setColor(Color.red);

gr.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));

gr.drawString(s[2], 20,80);

int sizef=20;

gr.setColor(Color.black);

gr.setFont(new Font(Font.SERIF, Font.BOLD, sizef));

String th[]={"№","Вірне слово","Введене слово"};

gr.drawString(th[0],20,120);

gr.drawString(th[1],20+(sizef*2),120);

gr.drawString(th[2],20+(sizef*16),120);

gr.setColor(Color.gray);

gr.drawRect(18, (120-sizef)+1, (sizef*2)-2, sizef);

gr.drawRect(16+(sizef*2), (120-sizef)+1, (sizef*16)-(sizef*2), sizef);

gr.drawRect(16+(sizef*16), (120-sizef)+1, (sizef*16)+18, sizef);

for(int i=0;i<n;i++){

gr.setColor(Color.black);

gr.setFont(new Font(Font.SERIF, 0, sizef-2));

String td[]={Integer.toString(i+1),word_orig[i],word_done[i]};

gr.drawString(td[0],20,120+((i+1)*sizef)-1);

gr.drawString(td[1],20+(sizef*2),120+((i+1)*sizef)-1);

gr.drawString(td[2],20+(sizef*16),120+((i+1)*sizef)-1);

gr.setColor(Color.gray);

gr.drawRect(18, (i*sizef)+120+1, (sizef*2)-2, sizef);

gr.drawRect(16+(sizef*2), (i*sizef)+120+1, (sizef*16)-(sizef*2), sizef);

gr.drawRect(16+(sizef*16), (i*sizef)+120+1, (sizef*16)+18, sizef);

}

}

public void doEnding(){

writeFile(getSummary());

}

String getSummary(){

Date d = new Date();

String c = d.toString()+"&bsol;n";

String s[]={"Результати тестування "+name+" (урок "+ldir+"):&bsol;n","Загальне число балів "+all_clear+" из "+all+".&bsol;n ","Оцінка: "+getBale()+" ("+getProcent()+"% успіху)&bsol;n"};

c=s[0]+"&bsol;n"+s[1]+"&bsol;n"+s[2];

for(int i=0;i<n;i++){

c=c+"Тест №"+(i+1)+" слово "+word_orig[i]+" написано як "+word_done[i]+"&bsol;n";

}

c = c+"--------------------------------------&bsol;n";

return c;

}

public void setShots(int g, int b){

clear_shots[cur_test] = g;

bad_shots[cur_test] = b;

all_clear = all_clear + g;

all = all + b;

}

public int getCur(){

return cur_test+1;

}

public int getCurent(){

return cur_test;

}

public int getN(){

return n;

}

public boolean isStarted(){

if(cur_test>-1){

return true;

}

else {

return false;

}

}

public void nextTest(){

if(cur_test<0){

cur_test=0;

Next();

} else {

if(cur_test<n-1){

cur_test++;

Next();

} else {

cur_test=-1;

}

}

}

void Next() {

loadImgCur();

bwav=checkWav();

newTimer();

Main.test.setFocus();

}

void newTimer() {

if(time[cur_test]>0){

Timer timer = new Timer(cur_test,time[cur_test]);

timer.start();

}

}

void loadImg(String f){

try {

img = ImageIO.read(new File(workdir+f));

//img.getGraphics().

} catch (IOException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

System.out.println("Can't load image "+workdir+f);

}

}

void loadLImg(){

try {

listen = ImageIO.read(new File(workdir+lisimg));

//img.getGraphics().

} catch (IOException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

System.out.println("Can't load image "+workdir+lisimg);

}

}

void loadImgCur(){

loadImg(image[cur_test]);

}

public String getOrigchr(int i){

return word_orig[cur_test].substring(i,i+1);

}

public String getCurword(){

return word_test[cur_test];

}

void readFiles(){

File f= new File(workdir+ldir+ftxt);

String[] sbuff;

n=0;

try {

BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(f), Main.iocharset));

try {

String line = null;

while (( line = input.readLine()) != null){

sbuff = line.split("[ &bsol;";,&bsol;t]+");

// Слово з пропущеними літерами

try {

word_test[n] = sbuff[1].toUpperCase();

} catch(ArrayIndexOutOfBoundsException e){

word_test[n]=sbuff[0].toUpperCase();

}

// Переклад слова

try {

word_rus[n] = sbuff[2];

} catch(ArrayIndexOutOfBoundsException e){

word_rus[n]="";

}

// Картинка

try{

image[n] = ldir+imgdir+sbuff[0]+".jpg";

} catch(ArrayIndexOutOfBoundsException e){

image[n]=defimg;

}

// Звук (вимова)

try{

wav[n] = ldir+snddir+sbuff[0]+".wav";

} catch(ArrayIndexOutOfBoundsException e){

wav[n]=defwav;

}

// Час на тест

try{

time[n] = Integer.parseInt(sbuff[3]);

} catch(ArrayIndexOutOfBoundsException e){

time[n]= 0;

}

// Слово

try {

word_orig[n] = sbuff[0].toUpperCase();

} finally {

n++;

}

}

}

finally {

input.close();

}

}

catch (IOException ex){

ex.printStackTrace();

}

}

void writeFile(String s){

File f = new File(workdir+"results.txt");

PrintWriter writer = null;

try {

writer = new PrintWriter(new BufferedWriter(new FileWriter(f,true)));

writer.println(s);

} catch(IOException e){

}

finally {