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

التعامل مع ال List

بدأه mr.beshoy في 23 أبريل 2010 · 5 رد · 733 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

من فترة قريبة كنت أعمل فى لعبة tetries و لظروف توقفت . و اليوم مع رجوعى لها بدأ التالى :

--------------------------------------------------------------------------------------------------------------------

من المعروف أن الشكل الذى ينزل رأسياً لإسفل عندما يصل لنهاية ال panel يجب أن يظهر شكل آخر أو نفس الشكل السابق و ينزل راسياً أيضاً .

أنى فعلت ذلك ، و لكن المشكلة تكمن فى : بمجرد نزول الشكل لإسفل و يظهر الشكل الإخر أو نفس السابق يختفى الأول .

فقررت عمل

 list <List<JPanel

- حتى أحتفظ بكل الأشكال - كالطريقة التالية :

   private void move_Square_Vertical() {
        clearPanel();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;
        } else {

            currentRow = 0;  // أقصدبذلك حينما تصل للصف الإخير أبدأ مرة أخرى من قيمة ال row =0 , ال col=4
            currentColumn = 4;
        }
        for(JPanel[] p:panel){
//  الخطأ هنا             list.add(panel);
        }
    }

و لكن بيعترض .

فلجأت لعمل Arraylist و أضيف كل الأشكال إليه . لكن دون جدوى

    private void move_Square_Vertical() {
        clearPanel();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;
        } else {
            arrayList.add(panel);  // أقصد بها  إضافة الشكل المرسوم للarraylist حتى لا يتم حذفه
            currentRow = 0;  // أقصدبذلك حينما تصل للصف الإخير أبدأ مرة أخرى من قيمة ال row =0 , ال col=4
            currentColumn = 4;
        }
    }

الكود كامل مرفق .

-----------------------------------

شكراً،،،،،،،،،،،

TestrissGamee.rar

تم تعديل هذه المشاركة بواسطة mr.beshoy في 23 أبريل 2010 في 05:35

#2

فكرت فى طريقة أخرى للتعامل مع ال List و للأسف لم تنفع ، و هى :

----------------------------------------------------------------------------------------

بما أن ال panel التى نرسم عليها الأشكال الملونة عبارة عن 2 ddimensional array

لذا فقكرت فى عمل List داخل List كالتالى :

    private List<List<JPanel>> list = new ArrayList<List<JPanel>>();

ثم عمل foreach لملئ ال list بقيم الصفوف و الأعمدة .

for (JPanel[] p : panel) {
                list.add(p);
            }

الغريب إنه فى JPanel[] p : panel لا يعترض حيث أن ال panel مصفوفة ثنائية و ال p مصفوفة أحادية لماذا؟!!!!

و يعترض عندما أُدخِل قيم ال p داخل ال List

private void move_Square_Vertical() {
        clearPanel();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;
        } else {

        for (JPanel[] p : panel) {
            list.add(p);
        }
            currentRow = 0;  // أقصدبذلك حينما تصل للصف الإخير أبدأ مرة أخرى من قيمة ال row =0 , ال col=4
            currentColumn = 4;
        }

    }

-------------------------------

شكراً مقدماً

#3

للأسف عملت محاولة أخرى و لكنها لم تنجح .

عملت List بالشكل التالى :

 private List list = new ArrayList();

ثم عندما يصل الشكل لإخر ال panel يخزنه فى ال list حتى لا يزول :

  private void move_Square_Vertical() {
        clearPanel();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;

        } else {
            list.add(panel[currentRow][currentColumn]);
            list.add(panel[currentRow][currentColumn + 1]);
            list.add(panel[currentRow + 1][currentColumn]);
            list.add(panel[currentRow + 1][currentColumn + 1]);
            currentRow = 0;  // أقصدبذلك حينما تصل للصف الإخير أبدأ مرة أخرى من قيمة ال row =0 , ال col=4
            currentColumn = 4;


        }
    }

لكن رغم إضافة الشكل كله مازال يختفى عند يبدأ الشكل الآخر فى النزول .

الكود مرفق .

---------------------------------------

من يعرف يفيدنا فى هذا الأمر . شكراً ،،،،

TestrissGamee.rar

#5

أولاً أسف على تأخيرى فى الرد . و شكراً لتعبك .

------------------------------------------------------------------------------

لكن عندى تساؤل :

فى هذه السطور :

Point[] p = {new Point(currentRow, currentColumn), new Point(currentRow, currentColumn + 1),
                new Point(currentRow + 1, currentColumn), new Point(currentRow + 1, currentColumn + 1)};
            list.add(p);

يتم تخزين نقط ال Panel داخل ال List

و بعدها نجد :

   currentRow = 0;
            currentColumn = 4;

فهل يبدأ فى تنفيذ هذين السطرين و فى نفس الوقت ينفذ الدالة drawPreviousSquares() التى تقوم برسم الشكل ؟

------------------------------------------------------------------------------------------------------------------------------------

المشكلة الأخرى : عندما ينزل شكل آخر يجب أن يكون لونه مختلف عن الاول لكن لا يحدث ذاك فقررت :

أن أجعل ال سطر[code] randomcolor=randomColor() ;

فى الدالة هذه

------------------------

 private void move_Square_Vertical() {
        clearPanel();
        randomColor = randomColor();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;

        } else {
            Point[] p = {new Point(currentRow, currentColumn), new Point(currentRow, currentColumn + 1),
                new Point(currentRow + 1, currentColumn), new Point(currentRow + 1, currentColumn + 1)};
            list.add(p);
            currentRow = 0;
            currentColumn = 4;
        }
    }

حتى يتغير اللون عند رسم كل شكل جديد و حذفته من ال constructor و لكن دون جدوى .. لماذا؟ّ

-----------------------------------------------------------------------------------------------------------------------

شكراً لتعبك مرة ثانية

تم تعديل هذه المشاركة بواسطة mr.beshoy في 23 أبريل 2010 في 23:34

#6

مازالت الوان الأشكال لا تتغير . أقصد عندم ينزل الاول و يبدأ اشكل الثانى فى الظهور يجب أن يكون لونه فى الغالب مختلف عن الاول و ليس كلهم لون واحد

شكراً

TestrissGamee.rar

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