1

I'm a student programming a frogger game, when the frog collides with an object or reaches the end zone either the score is incremented or lives decremented and the frog returned to the start position. this section works and the decrement and increment work when outputting them to the console, I try to pass the variable to the other jpanel and display it there but it doesnt update and display the variable in the textField.

Game Panel

    public GamePanel() {
    super();
    setFocusable(true);
    addKeyListener(new KeyList());
    System.out.println("GAME PANE FOCUS:" + this.isFocusOwner());

    scores.setVisible(true);
    lives = p.STARTLIVES;
    scores.setCurrentLives(lives);
    txtTest.setText("Hello");
    txtTest.setVisible(true);
    add(scores,new AbsoluteConstraints(0,550,50,800));
    Boolean displayable = scores.isDisplayable();
    System.out.println("Displayable" + displayable);

    scores.setEnabled(false);
    scores.revalidate();
    scores.repaint();
    scores.setVisible(true);

    System.out.println("Displayable" + displayable);



   car1.start();
   car2.start();
   car3.start();
   car4.start();
   Log1.start();
   Log2.start();
   Log3.start();
   Log4.start();
   Log5.start();
   Log6.start();
   Log7.start();
   Log8.start();



    //check for collisions
}

final public void winZone(Rectangle object){

    if(myFrog.frogArea().intersects(object)){
        currentScore = currentScore + 100;
        System.out.println("current Score " + currentScore);
        p.setScore(currentScore);
        scores.myScore().setText("hello");

        myFrog.lostLife();
    }

scores panel

public class jplScores extends JPanel {
 Properties p = new Properties();
 int currentLives;
 int i;

/** Creates new form jplScores */
public jplScores() {
    initComponents();




}
public void setCurrentLives(int Lives){
 currentLives = Lives;
}
public String getCurrentLives(){
    String L = Integer.toString(currentLives);
    return L;
}


public JTextField myScore(){
    return txtScore;
}

Currently it will display the jpanel from the frame that they are both in but i have tried to make it so its a panel within a panel but i cant get the panel to display from within the game panel.

Any help would be great thanks

    public FroggerGame() {

    initComponents();
    setFocusable(true);
    //repaint();
   // p.setHeight(550);
   // p.setWidth(800);
   // p.setLives(3);
   // p.setScore(0);
    PANELHEIGHT = p.getHeight();
    PANELWIDTH = p.getWidth();
    welcomePanel();

    /*
    Toolkit tool = Toolkit.getDefaultToolkit();
    imgBackground = tool.getImage(imageBackground);
    background = new ImageIcon(imgBackground);
    //setDefaultCloseOperation(EXIT_ON_CLOSE);
    */

    jps.myScore().addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
           // txtScorePropertyChange(evt);

    jps.myScore().setText(Integer.toString(gp.currentScore()));
    System.out.println(Integer.toString(gp.currentScore()));

    jps.getScore(gp.currentScore());
    System.out.println(" main score " + gp.currentScore());
        }
    });


}

....

private void btnEnterActionPerformed(ActionEvent evt) {

    welcomePanel.setVisible(false);
    getContentPane().setLayout(new AbsoluteLayout());
    getContentPane().add(gp,new AbsoluteConstraints(0,0,800,550));
    getContentPane().add(jps,new AbsoluteConstraints(0,550,800,100));

    //gp.setSize(800, 550);
   // gp.setBounds(0, 0, 800, 550);
    gp.setVisible(true);
    gp.requestFocusInWindow();

    jps.setVisible(true);

gp is the game panel and jps is the score panel.

1 Answers1

1

This really has little to do with "panels" or Swing GUI coding and all to do with the basic OOPS issue of passing information from one class to another. One way to solve this is to give your Scores panel a public method, say

public void changeScore(int value) {
   // in here add the new value to the currently 
   // displayed value and display the new value.
}

Then the main class, the one with a Scores panel reference, you can call this method, passing in 1 if score is to increase or -1 if it is to decrease.

I think of this as the "push" solution, where one class pushes information into the other class. Another way to solve this is via listeners where the Scores class listens to critical properties of the other class and then changes its own score when an appropriate event occurs, and this often involves using PropertyChangeListeners or other more Swing-specific listener classes. This is sometimes a nicer solution, but I consider it a slightly more advanced concept.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I have tried the above public method and this work when the panel is first displayed this displays the value when it is first called, but fails to update after that. What listener would you advise using as the action is dependant on the user colliding with something now by a key press or mouse click. I will try your "push" solution in the morning, as i may have missed something. many thanks for your reply – Nathan Donald Spence Apr 22 '12 at 23:52
  • @Nathan: the most general-purpose listener would probably be a PropertyChangeListener. – Hovercraft Full Of Eels Apr 23 '12 at 02:01
  • i tried the property change listener but this doesn't seem to work only recognizes the initial change ive added some more code of when i add the panels to the frame and the Listener. its 1 frame class and two seperate panels and i want data from one panel to appear in the other in realtime as its part of the game. many thanks again – Nathan Donald Spence Apr 23 '12 at 14:11
  • I think the problem is that im creating an instance of the class in the frame so if i try the push method i have to create a new instance in the panel, which wont change the other instance – Nathan Donald Spence Apr 23 '12 at 14:14
  • @Nathan: for the PropertyChangeListener to work, *YOU* have to fire the PropertyChangeSupport when you want to notify the listeners. There are several good tutorials on this that you can find on Google. Also, there's no need to "create a new instance in the panel", you simply pass in a reference of the object that you want to communicate with. This has nothing to do with with "creating an instance of the class in the frame", and again is pure basic Java and OOPs. – Hovercraft Full Of Eels Apr 23 '12 at 14:52
  • For instance, please look at my code in this question here regarding PropertyChangeListener: [swing-gui-doesnt-wait-for-user-input](http://stackoverflow.com/a/6935937/522444) – Hovercraft Full Of Eels Apr 23 '12 at 14:55
  • And for an example of getting a reference to another class, please look at my answer here: [how-to-repaint-out-of-focus-dialog-without-gaining-its-focus](http://stackoverflow.com/a/9433997/522444) – Hovercraft Full Of Eels Apr 23 '12 at 14:57
  • thankyou so much completely overlooked the fact i could pass reference of the object! superb help thankyou so much, for your time and patients. the propertyChangeListener and the other link you posted were really helpful too! thanks again!! – Nathan Donald Spence Apr 23 '12 at 23:30