بسم الله الرحمن الرحيم
مرحباااا بكم جميعااا
لدي مشكلة بسيطة في Threads وارجو المساعده ....
البرنامج عبارة عن برنامج يعرض معادلات رياضية بسيطه متحركة في فرايم باستخدام threads كل معادلة تنزل على حده
وكل معادله لها مده او الى ان تصل الى آخر الفرايم وتختفي عندها تبدأ المعادلة الثانيه وهكذا .....
المشكله هنا ان المعادلة الاولى تبدأ وتنطبع في الفرايم لكن المعادلات الاخرى (او بالاحرى ال thread الي يأتي بعده) لا يطبع المعادله (لا يدخل الى paint method)
وهنا الكود لـ المين كلاس والثريد كلاس
Main Class Code
import java.awt.*;
import javax.swing.*;
public class MathLearning extends JFrame {
// Variables
private Container container;
private JLabel scoreLabel, score;
public static JPanel ContainerPanel, panel1, panel2, panel3;
public MathLearning()
{
super("Mathematics Learning Game");
//Set new Font..
Font font1 = new Font("Times New Roman", Font.PLAIN, 20),
font2 = new Font("Cambria", Font.PLAIN, 20);
// Create the Container
container = getContentPane();
// Create Panels and Set Thier Background
ContainerPanel = new JPanel(new BorderLayout(1,1));
panel1 = new JPanel(new FlowLayout());
panel2 = new JPanel(new GridLayout(1,1));
panel3 = new JPanel();
panel1.setBackground(Color.getHSBColor(0.1f, 0.3f, 0.8f));
panel2.setBackground(Color.getHSBColor(0.1f, 0.3f, 0.8f));
panel1.setBorder(BorderFactory.createLineBorder(Color.WHITE));
panel2.setBorder(BorderFactory.createLineBorder(Color.WHITE));
// Label
scoreLabel = new JLabel("Total Score: ");
score = new JLabel();
scoreLabel.setFont(font2);
scoreLabel.setForeground(Color.DARK_GRAY);
score.setFont(font2);
score.setForeground(Color.WHITE);
// Add All Components to The Container
AddCompnents();
// Start The Game by Creating Threads
startGame();
// set size and location of he window
setLocation(400, 150);
setSize(400, 400);
setVisible(true);
}
public void AddCompnents()
{
container.add(ContainerPanel);
// Panel 1: Top Info Panel(total score, ...).
panel1.add(scoreLabel);
panel1.add(score);
// Panel 2: Equation Panel(set from inside the thread).
// Container Panel: adds all Panels to The Frame.
ContainerPanel.add(panel1, BorderLayout.PAGE_START);
ContainerPanel.add(panel2);
}
public void startGame()
{
MathThread Equation1 = new MathThread(score);
//MathThread Equation2 = new MathThread(score);
Thread eq1 = new Thread(Equation1);
eq1.start();
Thread eq2 = new Thread(Equation1);
eq2.start();
}
public static void main(String[] args) {
MathLearning application = new MathLearning();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}Thread Class Code
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 paint(Graphics 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 synchronized 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;
}
}
}