/*
 * Class that create threads and thier methods;
 * generate an equation and drow it in the frame,
 * then check the answer.
 */

package assignment1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MathThread extends JComponent implements Runnable{
    private int totalScore;
    private int x, y, sign;
    private int YAlign;
    private int result;
    private String calculationNumber;
    private JLabel score;
    //private Thread thread;

    // Constructor of The Class MathThread
    public MathThread(JLabel sr)
    {
            YAlign = 10;
        totalScore = 00;
             score = sr;

        
       //thread = new Thread(this);
       //thread.start();

        // Generate Random Numbers and Create Random Equation ... 
        createEquation();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        
        Font font1 = new Font("Times New Roman", Font.PLAIN, 30);

        g.setFont(font1);

        g.setColor(Color.blue);
        g.drawRect(170, YAlign, 75, 40);
        //g.draw3DRect(170, 10, 75, 40, true);

        g.setColor(Color.red);
        g.drawString(" "+ x + " " + calculationNumber + " " + y + " ", 170, (YAlign + 30));

        //System.out.println("" + totalScore++);
    }

    public synchronized void run()
    {
        System.out.println("Thread Started .. ");
                
        SwingUtilities.invokeLater(new UpdateGUI(score, " " + totalScore));
        
        MathLearning.panel2.add(this);

        while(YAlign <= (210))
        {
            YAlign = YAlign + 10;

            try
            {
                Thread.sleep(300);
            }
            catch(InterruptedException exc)
            {
                // do something about it ...
            }

            repaint();
        }

        setVisible(false);  // hide the equation ...
        MathLearning.panel2.repaint();

        System.out.println("Thread Finished .. ");
    }

    public void createEquation()
    {
        // Rabdom Numbers x, y and sign(+ / -)
           x = (int)(10.0 * Math.random());
           y = (int)(10.0 * Math.random());
        sign = (int)(2.00 * Math.random());

        if(sign == 0)
        {
            calculationNumber = "+";
                       result = x + y;
        }
        else
        {
            while(y > x)
            {
                x = (int)(10.0 * Math.random());
                y = (int)(10.0 * Math.random());
            }

            calculationNumber = "-";
                       result = x - y;
        }
    }
}
