Смекни!
smekni.com

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

}

});

}

public static void nextTest(){

words.nextTest();

test.rePaint();

}

public static void showHelp() {

java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

try {

java.net.URI uri = new java.net.URI( "file://"+workdir+helppage );

desktop.browse( uri );

}

catch ( Exception er ) {

// System.err.println( er.getMessage() );

}

}

}

Лістинг Test.java:

package mahno.masha.smallenglish;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.BorderLayout;

import javax.swing.SwingConstants;

import javax.swing.SwingUtilities;

import java.awt.Point;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JFrame;

import javax.swing.JDialog;

import javax.swing.Timer;

import java.awt.GridBagLayout;

import java.awt.Dimension;

import javax.swing.JButton;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagConstraints;

import java.awt.Color;

import java.awt.ComponentOrientation;

class Test {

final String question = "_"; // @jve:decl-index=0:

private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="10,10"

private JPanel jContentPane = null;

private JMenuBar jJMenuBar = null;

private JMenu fileMenu = null;

private JMenu helpMenu = null;

private JMenuItem exitMenuItem = null;

private JMenuItem aboutMenuItem = null;

private JDialog aboutDialog = null;

private JPanel aboutContentPane = null;

private JLabel aboutVersionLabel = null;

private JPanel top = null;

private JPanel bottom = null;

private JPanel left = null;

private JPanel right = null;

private JPanel screen = null;

private JButton button = null;

private JPanel pole = null;

private int lSize = 50;

private JPanel jPanel = null;

private Polekeys polekey[] = new Polekeys[Main.max_length]; // @jve:decl-index=0:

private Timer timer;

final Color bg = new Color(0x5B9DED);

final Color bn = new Color(0xE6F0FB);

boolean iskey = false;

boolean isend = false;

boolean cursor = true;

private JMenuItem instructionMenuItem = null;

public void rePaint() {

pole.repaint();

screen.repaint();

}

public void setFocus(){

pole.requestFocus();

}

/**

* This method initializes jFrame

*

* @return javax.swing.JFrame

*/

JFrame getJFrame() {

if (jFrame == null) {

jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jFrame.setPreferredSize(new Dimension(820, 760));

jFrame.setResizable(false);

jFrame.setJMenuBar(getJJMenuBar());

jFrame.setSize(820, 760);

jFrame.setContentPane(getJContentPane());

jFrame.setTitle("SmallEnglish");

}

return jFrame;

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.setPreferredSize(new Dimension(20, 110));

jContentPane.add(getTop(), BorderLayout.NORTH);

jContentPane.add(getBottom(), BorderLayout.SOUTH);

jContentPane.add(getLeft(), BorderLayout.WEST);

jContentPane.add(getRight(), BorderLayout.EAST);

jContentPane.add(getScreen(), BorderLayout.CENTER);

jContentPane.setFocusable(false);

}

return jContentPane;

}

/**

* This method initializes jJMenuBar

*

* @return javax.swing.JMenuBar

*/

private JMenuBar getJJMenuBar() {

if (jJMenuBar == null) {

jJMenuBar = new JMenuBar();

jJMenuBar.add(getFileMenu());

jJMenuBar.add(getHelpMenu());

}

return jJMenuBar;

}

/**

* This method initializes jMenu

*

* @return javax.swing.JMenu

*/

private JMenu getFileMenu() {

if (fileMenu == null) {

fileMenu = new JMenu();

fileMenu.setText("Файл");

fileMenu.add(getExitMenuItem());

}

return fileMenu;

}

/**

* This method initializes jMenu

*

* @return javax.swing.JMenu

*/

private JMenu getHelpMenu() {

if (helpMenu == null) {

helpMenu = new JMenu();

helpMenu.setText("Допомога");

helpMenu.add(getInstructionMenuItem());

helpMenu.add(getAboutMenuItem());

}

return helpMenu;

}

/**

* This method initializes jMenuItem

*

* @return javax.swing.JMenuItem

*/

private JMenuItem getExitMenuItem() {

if (exitMenuItem == null) {

exitMenuItem = new JMenuItem();

exitMenuItem.setText("Вихід");

exitMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

});

}

return exitMenuItem;

}

/**

* This method initializes jMenuItem

*

* @return javax.swing.JMenuItem

*/

private JMenuItem getAboutMenuItem() {

if (aboutMenuItem == null) {

aboutMenuItem = new JMenuItem();

aboutMenuItem.setText("Про програму");

aboutMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JDialog aboutDialog = getAboutDialog();

aboutDialog.pack();

Point loc = getJFrame().getLocation();

loc.translate(20, 20);

aboutDialog.setLocation(loc);

aboutDialog.setVisible(true);

}

});

}

return aboutMenuItem;

}

/**

* This method initializes aboutDialog

*

* @return javax.swing.JDialog

*/

private JDialog getAboutDialog() {

if (aboutDialog == null) {

aboutDialog = new JDialog(getJFrame(), true);

aboutDialog.setTitle("Про програму");

aboutDialog.setContentPane(getAboutContentPane());

}

return aboutDialog;

}

/**

* This method initializes aboutContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getAboutContentPane() {

if (aboutContentPane == null) {

aboutContentPane = new JPanel();

aboutContentPane.setLayout(new BorderLayout());

aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);

}

return aboutContentPane;

}

/**

* This method initializes aboutVersionLabel

*

* @return javax.swing.JLabel

*/

private JLabel getAboutVersionLabel() {

if (aboutVersionLabel == null) {

aboutVersionLabel = new JLabel();

aboutVersionLabel.setText("smallEnglish v.0.6\n by Masha\n mailto:masha.tar.gz@gmail.com");

aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);

}

return aboutVersionLabel;

}

/**

* This method initializes top

*

* @return javax.swing.JPanel

*/

private JPanel getTop() {

if (top == null) {

top = new JPanel();

top.setLayout(new GridBagLayout());

top.setPreferredSize(new Dimension(0, 10));

top.setBackground(bg);

top.setFocusable(false);

}

return top;

}

/**

* This method initializes bottom

*

* @return javax.swing.JPanel

*/

private JPanel getBottom() {

if (bottom == null) {

GridBagConstraints gridBagConstraints11 = new GridBagConstraints();

gridBagConstraints11.gridx = 0;

gridBagConstraints11.gridy = 1;

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.fill = GridBagConstraints.VERTICAL;

gridBagConstraints1.gridy = 0;

gridBagConstraints1.weightx = 1.0;

gridBagConstraints1.gridx = 0;

bottom = new JPanel();

bottom.setLayout(new BorderLayout());

bottom.setPreferredSize(new Dimension(820, 110));

bottom.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

bottom.setBackground(bg);

bottom.add(getPole(), BorderLayout.CENTER);

bottom.add(getJPanel(), BorderLayout.SOUTH);

bottom.setFocusable(false);

}

return bottom;

}

/**

* This method initializes left

*

* @return javax.swing.JPanel

*/

private JPanel getLeft() {

if (left == null) {

left = new JPanel();

left.setLayout(new GridBagLayout());

left.setPreferredSize(new Dimension(10, 0));

left.setBackground(bg);

left.setFocusable(false);

}

return left;

}

/**

* This method initializes right

*

* @return javax.swing.JPanel

*/

private JPanel getRight() {

if (right == null) {

right = new JPanel();

right.setLayout(new GridBagLayout());

right.setPreferredSize(new Dimension(10, 0));

right.setBackground(bg);

right.setFocusable(false);

}

return right;

}

/**

* This method initializes screen

*

* @return javax.swing.JPanel

*/

private JPanel getScreen() {

if (screen == null) {

screen = new JPanel(){

private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D gr = (Graphics2D) g;

gr.setColor(Color.GRAY);

gr.fillRect(0, 0, 800, 600);

Main.words.getImage(gr);

if(Main.words.isStarted()){

Main.words.sndIcon(gr);

gr.setColor(Color.gray);

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

gr.drawString("Тест №"+Main.words.getCur()+" з "+Main.words.getN(), 11,21 );

gr.setColor(Color.blue);

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

gr.drawString("Тест №"+Main.words.getCur()+" з "+Main.words.getN(), 10,20 );

}

/* if(iskey){

gr.setColor(Color.white);

//gr.fillOval(5, 210, 780, 150);

gr.fillRoundRect(20, 260, 750, 50, 10, 10);

gr.setColor(Color.BLACK);

gr.drawRoundRect(20, 260, 750, 50, 10, 10);

*/

Main.words.drawRus(gr);

if(isend){

// ffg nf

gr.setColor(Color.white);

gr.fillRect(0, 0, 800, 600);

Main.words.getSummaryG(gr);

}

};

};

screen.setLayout(new GridBagLayout());

screen.setBackground(bg);

screen.setPreferredSize(new Dimension(800, 600));

screen.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

if(e.getX()>660 && e.getX()<760 && e.getY()>460 && e.getY()<560 && Main.words.isWav()){

Main.words.Say();

}

}

});

screen.setFocusable(false);

//img = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);

}

return screen;

}

/**

* This method initializes button

*

* @return javax.swing.JButton

*/

private JButton getButton() {

if (button == null) {

button = new JButton();

button.setText("Розпочати тест");

button.setPreferredSize(new Dimension(200, 30));

button.setBackground(bn);

button.setForeground(Color.black);

button.setFont(new Font("Dialog", Font.BOLD, 18));

button.setHorizontalAlignment(SwingConstants.CENTER);

button.setFocusable(false);

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

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

doClick();

}

});

}

return button;

}

public void Click(){

doClick();

}

private void doClick(){

if(!isend){

if(!Main.words.isStarted()){

button.setText("Дати підповідь");

} else {

doSumary();

}

Main.nextTest();

initPole();

if(!Main.words.isStarted()){

button.setText("Вийти");

isend = true;

Main.words.doEnding();

}

} else {

System.exit(0);

}

}

/**

* This method initializes pole

*

* @return javax.swing.JPanel

*/

private JPanel getPole() {

if (pole == null) {

pole = new JPanel(){

/**

*

*/

private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D gr = (Graphics2D) g;

gr.setColor(bg);

gr.fillRect(0, 0, 820, lSize*2);

if(Main.words.isStarted()){

doPaint(gr);

}

}

private void doPaint(Graphics2D gr) {

int x = 0;

int y = 0;

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

x = (i * (lSize+5))+20;

y = 16;

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

polekey[i].x1=x;

polekey[i].x2=x+lSize;

if(!polekey[i].is){

gr.setColor(Color.LIGHT_GRAY);

gr.fillRect(x,y,lSize,lSize);

gr.setColor(Color.blue);

gr.drawRect(x,y,lSize,lSize);

gr.setColor(Color.black);

gr.drawString(polekey[i].getChr(), x+5, y+lSize-4);

} else

{

gr.setColor(Color.DARK_GRAY);

gr.setFont(new Font(Font.SERIF, Font.PLAIN, 12));

gr.drawString(Integer.toString(polekey[i].index+1), x+((int)(lSize/2))-2, 12);

gr.setColor(Color.white);

gr.fillRect(x,y,lSize,lSize);

gr.setColor(Color.blue);

gr.drawRect(x,y,lSize,lSize);

gr.setColor(Color.black);

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

if(Polekeys.cur==i && iskey){

if(cursor){gr.drawString("|", x+3, y+lSize-9);} else {

gr.drawString(polekey[i].ch, x+5, y+lSize-4);}

} else {

gr.drawString(polekey[i].ch, x+5, y+lSize-4);

}

}

}

};

};

pole.setLayout(new GridBagLayout());

pole.setPreferredSize(new Dimension(lSize, 800));

pole.setFocusable(true);

pole.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent e) {

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

if(polekey[i].x1<e.getX() && polekey[i].x2>e.getX() && polekey[i].is){

setKey(true);

doLclick(i);

}

pole.requestFocus();

}

}

});

pole.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent e) {

String s = String.valueOf(e.getKeyChar()).toUpperCase();

if(e.getKeyCode()==KeyEvent.VK_ESCAPE){

setKey(false);

return;

}

if(e.getKeyChar()=='&bsol;n'){

setKey(false);

doClick();

return;

}

if(Main.words.isStarted()){

if(iskey && s.matches("[A-Z]")){

polekey[Polekeys.cur].ch = s;

//Polekeys.cur= -1;

//button.setEnabled(true);

if(Polekeys.curind<Polekeys.mindex) {

Polekeys.curind++;

} else {

setKey(false);

Polekeys.curind=0;

}

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

if(polekey[j].index==Polekeys.curind){

doLclick(j);

return;

}

}

rePaint();

return;

}

if(s.matches("[1-9]")){

int i=Integer.parseInt(s);

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

if(polekey[j].index==(i-1)){

setKey(true);

doLclick(j);

return;

}

}

}

if(e.getKeyCode()==KeyEvent.VK_RIGHT | e.getKeyCode()==KeyEvent.VK_TAB){

setKey(true);

if(Polekeys.curind<Polekeys.mindex) {

Polekeys.curind++;

} else {

Polekeys.curind=0;

}

//System.out.println(Polekeys.curind+" "+Polekeys.mindex);

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

if(polekey[j].index==Polekeys.curind){

doLclick(j);

return;

}

}

}

if(e.getKeyCode()==KeyEvent.VK_LEFT | e.getKeyCode()==KeyEvent.VK_BACK_SPACE){

setKey(true);

if(Polekeys.curind>0) {

Polekeys.curind--;

} else {

Polekeys.curind=Polekeys.mindex;

}

//System.out.println(Polekeys.curind+" "+Polekeys.mindex);

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

if(polekey[j].index==Polekeys.curind){

doLclick(j);

return;

}

}

}

}

}

});

}

timer =new Timer(500, new java.awt.event.ActionListener() {

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

if(cursor) {

cursor=false;

} else {

cursor=true;

}

pole.repaint();

}

});

timer.stop();

return pole;