在JPanel之间传递数据

时间:2013-05-15 13:37:59

标签: java swing jpanel paintcomponent

我正在用Java制作俄罗斯方块,并希望让游戏在左侧进行游戏,并在右侧进行评分,按钮和nextPiece,如下所示:

enter image description here

您会注意到游戏面板上的分数正在更新,但分数面板(右侧)的分数不是。

在游戏面板上,我有得分和等级的全局变量:private int level, totalScore;,它们被初始化为0.

这在我的paint component():

        g.setColor(Color.RED);
        g.drawString("Level: " + level, this.getWidth()/2+110, this.getHeight()/2-200);
        g.drawString("Score: " + totalScore, this.getWidth()/2+110, this.getHeight()/2-170);

然后我在游戏面板中有这个代码来计算等级和得分:

public void changeLevel () {
    int max = (level+1)*100;
    if (totalScore >= max) {
        System.out.println(max + "reached... next level");
        level++;
        totalScore = 0;
        timer();
    }
}

public int tallyScore(int totalLines) {
    int score = 0;
    switch (totalLines) {
        case 1: score = 40 * (level + 1);
                break;
        case 2: score = 100 * (level + 1);
                break;
        case 3: score = 300 * (level + 1);
                break;
        case 4: score = 1200 * (level + 1); 
                break;
        default: break;
    }
    return score;
}

//loop through all rows starting at bottom (12 rows)
public void checkBottomFull() {
    int lines = 0;
    for(int row = totalRows-1; row > 0; row--) {
        while (isFull(row)) {       
            lines++;
            clearRow(row);   
        }
    }
    totalScore += tallyScore(lines);    
    //check if level needs to be changed based on current score...
    changeLevel();
    //reset lines after score has been incremented
    lines=0;
}

由于我希望分数面板显示分数,我在Game Panel中有这两种方法可以返回全局变量:

public int getScore() {
    return totalScore;
}
public int getLevel() {
    return level;
}

在我的分数面板中paintComponent()我有board.getLevel()board.getScore()board类是游戏面板)所以我可以将游戏面板分数提供给分数面板。

    g.setColor(Color.BLACK);
    g.drawString("Level: " + board.getLevel(), this.getWidth()/2, this.getHeight()/2-130);
    g.drawString("Score: " + board.getScore(), this.getWidth()/2, this.getHeight()/2-100);

然而,正如您从图片中看到的那样,这些分数没有更新。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

您需要分开关注点以便共享它们。考虑为构成GUI基础的逻辑和数据创建一个类,并假设您将此类称为Model类。然后你可以给它一个级别和一个得分字段,并使它们成为“绑定属性”,这意味着其他类可以监听这些字段的更改。我通常通过给我的Model一个SwingPropertyChangeSupport对象并给它一个addPropertyChangeListener(PropertyChangeListener listener)和一个removePropertyChangeListener(PropertyChangeListener listener)来做这个,然后通过调用PropertyChangeSupport的fire来通知所有已注册的PropertyChangeListener。如,

import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;

public class Model {
   public static final String SCORE = "score";
   public static final String LEVEL = "level";
   private SwingPropertyChangeSupport pcSupport = 
         new SwingPropertyChangeSupport(this);
   private int score;
   private int level;

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

   public int getScore() {
      return score;
   }

   public void setScore(int score) {
      int oldValue = this.score;
      int newValue = score;

      this.score = score;
      pcSupport.firePropertyChange(SCORE, oldValue, newValue);
   }

   public int getLevel() {
      return level;
   }

   public void setLevel(int level) {
      int oldValue = this.level;
      int newValue = level;

      this.level = level;
      pcSupport.firePropertyChange(LEVEL, oldValue, newValue);
   }

}

然后,任何希望收听值变化的GUI或视图组件都可以这样做。如果你正在研究MVC结构,下面的课程将“View”和“Control”结合起来:

import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class UseModelGui {

   private static void createAndShowGui() {
      Panel1 panel1 = new Panel1();
      Panel2 panel2 = new Panel2();
      Model model = new Model();

      panel1.setModel(model);
      panel2.setModel(model);

      JFrame frame = new JFrame("UseModelGui");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(new FlowLayout());
      frame.getContentPane().add(panel1);
      frame.getContentPane().add(panel2);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Panel1 extends JPanel {
   private JTextField scoreField = new JTextField(2);
   private JTextField levelField = new JTextField(2);

   public Panel1() {
      scoreField.setFocusable(false);
      scoreField.setEditable(false);
      levelField.setFocusable(false);
      levelField.setEditable(false);

      add(new JLabel("score:"));
      add(scoreField);
      add(new JLabel("Level:"));
      add(levelField);
      setBorder(BorderFactory.createTitledBorder("Check Values"));
   }

   public void setModel(Model model) {
      model.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if (Model.LEVEL.equals(pcEvt.getPropertyName())) {
               String level = pcEvt.getNewValue().toString();
               levelField.setText(level);
            } else if (Model.SCORE.equals(pcEvt.getPropertyName())) {
               String score = pcEvt.getNewValue().toString();
               scoreField.setText(score);
            }

         }
      });
   }

}

class Panel2 extends JPanel {
   private JSpinner scoreSpinner = new JSpinner(new SpinnerNumberModel(0, 0,
         20, 1));
   private JSpinner levelSpinner = new JSpinner(new SpinnerNumberModel(0, 0,
         10, 1));
   private Model model;

   public Panel2() {
      add(new JLabel("score:"));
      add(scoreSpinner);
      add(new JLabel("Level:"));
      add(levelSpinner);
      setBorder(BorderFactory.createTitledBorder("Set Values"));

      scoreSpinner.addChangeListener(new ChangeListener() {

         @Override
         public void stateChanged(ChangeEvent evt) {
            int score = ((Integer) scoreSpinner.getValue()).intValue();
            if (model != null) {
               model.setScore(score);
            }
         }
      });
      levelSpinner.addChangeListener(new ChangeListener() {

         @Override
         public void stateChanged(ChangeEvent evt) {
            int level = ((Integer) levelSpinner.getValue()).intValue();
            if (model != null) {
               model.setLevel(level);
            }
         }
      });
   }

   public void setModel(Model model) {
      this.model = model;
   }

}

这样做的好处是Panel1不了解Panel2,而Model也不知道。所有Panel1都知道模型是否发生变化。所有Panel2都知道它正在改变Model的状态。所有模型都知道它的状态可以改变,并且可以监听它的值。

你是对的,在这个简单的例子中,这是过度杀戮,但是一旦你开始有复杂的数据和状态,这是有道理的,并且变得非常有帮助。

答案 1 :(得分:3)

您应该将业务逻辑与演示文稿分开。请阅读:Model View Controller模式。