الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في Threads

بدأه اللهب الناري في 2 مارس 2011 · 13 رد · 1,090 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم

مرحباااا بكم جميعااا

لدي مشكلة بسيطة في 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;
        }
    }
}
#2

هل من مساعده لو تسمحوا ؟؟!!!

#3

استخدم paintComponent بدلاً من paint

واجعل أول سطر في الشيفرة الخاصة بهذه الدالة هي

super.paintComponent

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#4

اشكرك اخي علاء

سأفعل ذلك

تحياتي لك ...

#5

المشكله مازالت قائمة ... نفس المشكلة

هل هذا الكود صحيح ؟؟

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();

    }

تم تعديل هذه المشاركة بواسطة اللهب الناري في 3 مارس 2011 في 10:39

#6

ضع الشيفرة كمرفق

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#8

مشكلتك أن المعادلات تعمل كل على حدة في Thread مختلف

نفس المكون يعمل مرتين على كل خيط

وهذا بالتأكيد سيؤدي إلى أن أحد الخيطين لن يكون له أي فائدة

بما أنك تريد للمكونات components المتحركة أن تكون متتابعة فأنصحك أن تعمل في نفس الخيط

هناك حزمة package قمت بإنشاءها هدفها هو تحريك الكائنات المتحركة الحزمة AAniamtion حزمة الحركة الخاصة بـ swing

يمكنك الإطلاع عليها والاستفادة من طريقة التحريك فيها

كنت أريد أن أقترح عليك استخدامها لكنه يبدو لي وكأنه واجب جامعي

ولا أعرف إن كان هذا مسموحاً به أم لا

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#9

اشكرك جزيل الشكر اخي علاء على الافادة الرائعه - بارك الله فيك وزادك علما

نعم هذا واجب جامعي الهدف منه استخدام threads

وهذه هي المشكله التي اصادفها في استخدام threads في swing

كيف اجعل العمل في نفس الخيط كما اقترحت لي ؟؟

اعذرني على اسئلتي المتفرقة لأني جديد في استخدام threads

#10

ما يجب التحكم فيه هو الرسم في ال MathLearning.panel2، حيث يرسم فيه thread واحد على حدة، لذلك يجب أن تكون ال synchronization في هذا المستوى

في MathThread

	public void run() {
		System.out.println("Thread Started .. ");

		SwingUtilities.invokeLater(new UpdateGUI(score, " " + totalScore));

		synchronized (MathLearning.panel2) {
			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 .. ");
		}
	}

تم تعديل هذه المشاركة بواسطة herch في 4 مارس 2011 في 11:15

#11

أنت لديك مكون component متحرك في نفس الوقت

لذا كل ما عليك هو عمل while في الدالة run

ونقل مكان المكون إلى نقطة البداية كل مرة يصل إلى نقطة النهاية مع تغيير المعادلة (بمعنى آخر عمل العمليات الابتدائية initialization من جديد)

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#12
herch كتب:

ما يجب التحكم فيه هو الرسم في ال MathLearning.panel2، حيث يرسم فيه thread واحد على حدة، لذلك يجب أن تكون ال synchronization في هذا المستوى

في MathThread

	public void run() {
		System.out.println("Thread Started .. ");

		SwingUtilities.invokeLater(new UpdateGUI(score, " " + totalScore));

		synchronized (MathLearning.panel2) {
			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 .. ");
		}
	}

ساعمل ذلك اخي

اشكرك على المساعده

تحياتي لك

علاء الصالحي كتب:

أنت لديك مكون component متحرك في نفس الوقت

لذا كل ما عليك هو عمل while في الدالة run

ونقل مكان المكون إلى نقطة البداية كل مرة يصل إلى نقطة النهاية مع تغيير المعادلة (بمعنى آخر عمل العمليات الابتدائية initialization من جديد)

تحياتي

اتضحت الفكرة اخي

وساعمل بذلك

اشكرك جزيل الشكر دمت بود ومحبة

#13

اخواني عملت بكل ماطلبتوا مني

لكن المشكله هي : الثرد الاول يرسم في الفريم

الثرد الثاني لا يرسم شي

اعتقد المشكله في الرسم (رسم المعادلة)

:wacko:

#14

تم حل المشكلة ....

المشكلة كانت في

setVisible(false);

اشكر الجميع على المساعده :happy:

مواضيع مشابهة