سؤال

وضمن هذا البرنامج، ونحن بحاجة إلى إنشاء شبكة 8X8 من الحاجيات "LifeCell". وقال إن المدرب لم يذكر أن الحاجيات كان لا بد كائن من Shape ذلك ذهبت إلى الأمام واستخدام الفئة GridLayout. الطبقة GridLayout يعمل بشكل جيد (وكذلك أنا أعلم، وحيث لا يوجد المساعدات البصرية للتأكيد.) والهدف من هذا البرنامج هو للعب لعبة الحياة حيث يمكن للمستخدم النقر على واحدة من الحاجيات LifeCell وتبديل بين الدول كونها "على قيد الحياة" أو "ميتا.

وسؤالي يعتمد بشكل كبير على الحصول على الخلايا المراد رسمها. يمكن أن يكون مشكلة مع قانون بلدي، لكنني لست متأكدا 100٪.

Program2.java

public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.

public Program2() {
    // The usual boilerplate constructor that pastes the main
    // panel into a frame and displays the frame. It should
    // invoke the "init" method before packing the frame
    frame = new JFrame("LIFECELL!");
    frame.setContentPane(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.init();
    frame.pack();
    frame.setVisible(true);
}
    public void init() {
    // Create the user interface on the main panel. Construct
    // the LifeCell widgets, add them to the panel, and store
    // them in the two-dimensional array "board". Create the
    // "next" button that will show the next generation.
    LifeCell[][] board = new LifeCell[8][8];
    this.setPreferredSize(new Dimension(600, 600));
    this.setBackground(Color.white);
    this.setLayout(new GridLayout(8, 8));
    // here is where I initialize the LifeCell widgets
    for (int u = 0; u < 8; u++) {
        for (int r = 0; r < 8; r++) {
            board[u][r] = new LifeCell(board, u, r);
            this.add(board[u][r]);
            this.setVisible(true);

        }
    }

LifeCell.java

 public class LifeCell extends JPanel implements MouseListener {
   private LifeCell[][] board; // A reference to the board array.
   private boolean alive;      // Stores the state of the cell.
   private int row, col;       // Position of the cell on the board.
   private int count;          // Stores number of living neighbors.

   public LifeCell(LifeCell[][] b, int r, int c) {
       // Initialize the life cell as dead.  Store the reference
       // to the board array and the board position passed as
       // arguments.  Initialize the neighbor count to zero.
       // Register the cell as listener to its own mouse events.
       this.board = b;
       this.row = r;
       this.col = c;
       this.alive = false;
       this.count = 0;
       addMouseListener(this);
   }   

وهنا هو الأسلوب paintComponent:

   public void paintComponent(Graphics gr) {
       // Paint the cell.  The cell must be painted differently
       // when alive than when dead, so the user can clearly see
       // the state of the cell.
           Graphics2D g = (Graphics2D) gr;
           super.paintComponent(gr);
           g.setPaint(Color.BLUE);
   }

وأنا لست بحاجة إلى الحل الصحيح لإصلاح ذلك، ولكن أنا في نهاية الذكاء في محاولة للحصول على عمل.

وشكرا.

وتحرير:

وأضفت المزيد من جزء من الطبقة Program2.java، يمكنني التحقق مرة أخرى غدا وأنا متجه إلى السرير، وأنا أقدر كل مساعدة رجال.

وتحرير # 2:

وبلدي الارتباك الحقيقي يحصل على عندما كنت ملء إطار بلدي مع GridLayout 8X8 كل الفردية "خلية" لعدم وجود أفضل الكلمات هي من نوع LifeCell. كيف يمكن أن أرسم كل LifeCell ألوان مختلفة؟ إذا كان هذا يجعل من أي معنى على الإطلاق ليا رفاق، ويمكنني أن محاولة تعديله بقدر ما أستطيع. وcamickr، وسوف ننظر في هذا الموقع، شكرا لك.

ويمكن العثور على تعيين هنا لتجنب أي وكل الارتباك فيما يتعلق سؤالي و / أو التعليمات البرمجية المتكررة.

هل كانت مفيدة؟

المحلول

وأنت في الطريق الصحيح.

إذا كنت تريد استخدام عنصر القائمة (مثل JPanel، JLabel، JButton الخ) انها أفضل بكثير أنك تحترم ما يقوم به المكون بالفعل، ومجرد parametrize ما هو مطلوب.

وحتى في قضيتك كنت تستخدم JPanel، هذا (وJComponents غيرها) لها خاصية background أنه يمكنك تغيير. لذا، بدلا من محاولة رسم المكون نفسك (وهو ما فشل الآن) مجرد مجموعة تلك القيمة وترك الطلاء الطلاء نفسها.

ويمكنك إضافة "getLifeColor" التي تعود ألوان مختلفة تبعا للحالة الخلية:

   private Color getLifeColor() {
       return this.alive?liveColor:deadColor;
   } 

وبعد ذلك فقط يكون الخلية اللوحة الخلفية مع هذا اللون:

  public void paintComponent(Graphics gr) {
       setBackground( getLifeColor() );
       super.paintComponent( gr );
  }

وبعد ذلك عليك أن تعيين حالة الخلية للعيش أو ميتا، وسوف تظهر المكون مع اللون المطابق:

هنا النفس القصير يتضمن المثال الصحيح (SSCCE) من قانون قمت بنشرها + والعيش / لون ميت الاستخدام. أعتقد أنك يمكن أن يستمر من هناك.

نصائح أخرى

وJPanel لا يوجد الحجم الافتراضي المفضل أو المحتوى المرئي. ستحتاج إلى إضافة نوع من عنصر مرئية (على سبيل المثال JLabel) أو إعطائها حجم المفضل.

وبالإضافة إلى ذلك، يجب أن تعمل التخطيط الخاص بك إذا كنت قد إعداده على النحو التالي:

JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
    for (int j = 0; j < 8; j++)
        cp.add(new JLabel(i + "-" + j));
frame.pack();
frame.setVisible(true);

وماذا لديك حتى طريقة paintComponent () لLifeCell الخاصة بك؟ ليست هناك حاجة للقيام اللوحة المخصصة. يمكنك تغيير لون الخلفية من أي مكون باستخدام:

setBackground( Color.BLUE ) 

وبخلاف ذلك سؤالك لا معنى له بالنسبة لي. أولا تصرح تحتاج إلى استخدام كائن الشكل، ولكن لا أرى كائن الشكل في أي مكان في التعليمات البرمجية، لذلك لماذا الخلط بين مسألة بذكر ذلك؟

وأنا حقا لا أفهم سؤالك وليس لدينا ما يكفي من التعليمات البرمجية لتقديم أي اقتراحات حقيقية.

إذا كنت تحتاج إلى مزيد من المساعدة مشاركتك SSCCE تظهر المشكلة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top