Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6555545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:50:01+00:00 2026-05-25T12:50:01+00:00

So I have this code: package com.erikbalen.game.rpg; import com.erikbalen.platform.*; import javax.swing.JFrame; public class World

  • 0

So I have this code:

package com.erikbalen.game.rpg;
import com.erikbalen.platform.*;
import javax.swing.JFrame;

public class World extends Engine {

    public static void main(String[] args) {        

    Gui display = new Gui(/*takes a Player argument so i can get certain variables*/);
    display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    display.setSize(300,220);
    display.setVisible(true);

    Player player1 = new Dps("ebalen", display);
    Player player2 = new Healer("frankypanky", display);

    }

}

package com.erikbalen.game.rpg;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Gui extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = -384241835772507459L;
    private JLabel playerInfo;
    private JTextField textField;
    private final static String newline = "\n";
    private JTextArea feed;
    private JScrollPane scrollPane;
    private Player player;

    public Gui(Player currentPlayer) {
        super("Erik's RPG");
        this.player = currentPlayer;
        setLayout(new FlowLayout());

        playerInfo = new JLabel("<html>Health = " + currentPlayer.getHealth() + " | " + "Mana = " + currentPlayer.getMana() + "</html>");

        playerInfo.setBorder(BorderFactory.createTitledBorder(currentPlayer.getName()));

        textField = new JTextField(20);

        textField.addActionListener(this);

        feed = new JTextArea(5, 20);
        scrollPane = new JScrollPane(feed);
        feed.setEditable(false);

        add(playerInfo);
        add(feed);
        add(textField);
        add(scrollPane);

    }

    public void actionPerformed(ActionEvent textBox) {
         String text = textField.getText();
         this.player.chat(text);

    }

    public void printText(String text) {
        feed.append(text + "\n");
        feed.setCaretPosition(feed.getDocument().getLength());
    }

}

My problem is that class Gui takes a Player as an argument and Player takes Gui as an argument. How do I let both objects take each other as arguments? Feel free to tell me if my code is inefficient.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:50:01+00:00Added an answer on May 25, 2026 at 12:50 pm

    Well, ideally you should try to break the circular dependency, but otherwise, you could:

    • Create the GUI, create the players by passing in the GUI, then add the players to the GUI
    • Create the players, create the GUI by passing in the players, then set the GUI for the players
    • Create the GUI within the player constructor:

      Player(String name)
      {
          GUI gui = new GUi(this);
          ...
      }
      

    All of these are non-ideal:

    • You may well want your classes to be immutable, which makes the first two options nasty
    • Publishing the this reference before your constructor has finished executing has various issues, both in terms of thread safety and the memory model, as well as potentially allowing the GUI constructor to call back to the Player object before it’s fully initialized.

    This goes back to “try to break the dependency” – but if that’s really impossible, I would probably favour the first option, without knowing anything else. It makes sense to be able to add players to a game – it makes less sense to set the GUI for a player after the fact, IMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
i have this code package com.tct.soundTouch; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent;
I have this code: package com.problemio; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View;
I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code package com.cerbertek; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory;
Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
I have found this piece of code in the Haskell sendfile package: http://patch-tag.com/r/mae/sendfile/snapshot/current/content/pretty/src/Network/Socket/SendFile/Linux.hsc --
This is my code: package com.example.testcode; import android.app.ListFragment; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader;
I have this code: public class VoiceActivity extends Activity implements OnClickListener { private TextView
package com.parseador.prueba; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class main extends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.