Jump to content

Several instances of one jFrame Form?

Claryn

Hi.

 

I have a program where I have made a jFrame Form called cahPlayer in Netbeans. The form is supposed to be an individual game-window for several players on the same computer. The jFrame Form will stay the same for all players, but they will have their own text displayed in some jTextFields in the form.

 

Now, I want to have x players. I want to make x instances of the Form (x amounts of windows), and have them all have unique buttons and jTextFields. Is there a way of doing this? 

Basically making the jFrame Form into an object? 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, you can do this!

 

Basically... You have a class which you have all the code for the game...

 

then in the section of code where you create an instance of the game/window... add a for loop for how ever many windows u want...

 

And you will need to create a new thread for each of the windows

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, BlueDragon said:

Yes, you can do this!

 

Basically... You have a class which you have all the code for the game...

 

then in the section of code where you create an instance of the game/window... add a for loop for how ever many windows u want...

 

And you will need to create a new thread for each of the windows

What do you mean by creating a new thread for each window?

 

Will I then be able to change variables in each window?
Like window1.jTextField1.setText() etc?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Claryn said:

What do you mean by creating a new thread for each window?

 

Will I then be able to change variables in each window?
Like window1.jTextField1.setText() etc?

I'm not exactly sure what a JFrame form is, likely because I've never used Netbeans, but I'll assume it's just a JFrame for a specific purpose.

 

Whether or not you can do what you described depends on if jTextField1 is visible. Is jTextField1 private? protected? package-protected? or public? If the access level is too restrictive for your main class to call it, then you need to have setter methods which are public. For example, 

public class JFrameForm1 extends JFrame {

	private JTextField jTextField1 = new JTextField(40);	//This field is private, so it can't be directly accessed by other classes

	public void setJTextField1(String text) {				//This is a setter method which allows other classes
		jTextField1.setText(text);					//to modify a restricted variable
	}
}

public class JFrameForm2 extends JFrame {
	JTextField jTextField1 = new JTextField(40);			//This field is package-protected (default access), so it can
}									//be accessed by Main.java if Main.java is in the same package

public class Main {

	public static void main(String[] args) {
		JFrameForm1 form1 = new JFrameForm();
		form1.setJTextField1("Hello");						//The JTextField cannot be accessed directly,
											//so a setter method is used.
		JFrameForm2 form2 = new JFrameForm();
		form2.jTextField1.setText("Hello");					//The JTextField can be accessed directly
	}
}

Sorry for the wacky comment formatting. The code editor apparently has its quirks.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Claryn said:

What do you mean by creating a new thread for each window?

 

Will I then be able to change variables in each window?
Like window1.jTextField1.setText() etc?

Yes

 

Here's some example code

 

Main method/class:

package com.bluedragon.helping;

public class Main {

	public static void main(String[] args) {

		// We loop for the amount of windows we want to make.
		for (int i = 0; i < 5; i++) {
			// This is just so that we can parse the variable into an enclosed
			// class
			final int windowNum = i;

			// Crate a new thread for each window
			new Thread(Integer.toString(i)) {
				public void run() {
					// Crate a new instance of the window
					new Window(windowNum);
				}
			}.start();
		}
	}
}

 

The class which contains the window:

package com.bluedragon.helping;

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

// This class is where the windows start for the game
public class Window extends JFrame {

	private static final long serialVersionUID = 1L;

	public boolean running = false;

	// Objects for the window
	private JPanel contentPane;
	private JLabel label;

	// Class constructor
	public Window(int windownumber) {
		// First call create window method so that we crate the window
		createWindow(Integer.toString(windownumber));

		// Now launch the logic
		logic(windownumber);
	}

	// This is the logic of the game
	public void logic(int windownumber) {
		running = true;

		// To show that each window is independent of each other we will change
		// the text once a second
		double lastTime = System.currentTimeMillis();
		double currentTime;

		int seconds = 0;

		while (running) {
			currentTime = System.currentTimeMillis();

			if (currentTime - lastTime >= 1000) {
				label.setText("Window: " + windownumber + " Seconds: "
						+ ++seconds);
				lastTime = currentTime;
			}
		}
	}

	// This method will crate the window
	public void createWindow(String title) {
		// Setting up some parameters for the JFrame
		setLocationRelativeTo(null);
		// Add a window listener so that we close the window when the user
		// presses the x button
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				// We set running to false to stop the while loop otherwise the
				// thread will not die
				running = false;
				// This closes the window without closing the whole application
				dispose();
			}
		});
		setTitle(title);
		setSize(400, 400);

		contentPane = new JPanel();
		contentPane.setLayout(new GridLayout(1, 1));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		label = new JLabel("This is the label which can be changed",
				JLabel.CENTER);
		contentPane.add(label);

		setVisible(true);
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I am sorry, but neither of you got my problem, I think. @BlueDragon @Philosobyte

 

I have made a jFrame Form in NetBeans. I have set up the design for it with the drag and drop functionality of NetBeans. It contains 11 labels, 12 buttons and 11 textfields. The class contains over 3000 lines of auto-generated code from Netbeans.

The jFrame Form is called playerUI(.class)

 

When the game starts, you are prompted with a small window that asks for an integer input of how many players there are.

 

This is what I want:

When the input is received, the appropriate amount of playerUI windows are supposed to pop up. They are all identical, except for 1 label that is changed (Player1, Player2, Player 3 etc.). 

I assume I can make these instances by simply doing 

playerUI player1UI = new playerUI; for all the players. 

 

Then, I need to have control over all the buttons and textfields in the individual windows, from another class (all buttons and textfields are public). Can I then simply use player1UI.jTextField1.setText(); etc?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Claryn said:

snippitey snip

One way you can solve that is by searching the auto-generated code for instances of "JTextField" and seeing what kind of variable it is.

 

Other than that, the only way someone could answer that is if they've tried it before. Most people here use IntelliJ instead of Netbeans, so yeah. 

 

Maybe I'm still not understanding you, but why don't you try it? How much work is it to create a new Test project, create a small JFrame Form with a text field, and see if you can access the JTextField when the JFrame Form is stored as a variable? Especially if you create two instances of the JFrame Form, as you described. Maybe the entire test project would be about 30 lines of code and take twenty minutes of experimentation?

Link to comment
Share on other sites

Link to post
Share on other sites

Missed the part about buttons and textfields being public. I really need to start reading these posts more closely. Yes, you can simply use player1UI.TextField1.setText();

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, Philosobyte said:

Missed the part about buttons and textfields being public. I really need to start reading these posts more closely. Yes, you can simply use player1UI.TextField1.setText();

Alright so the declaration of the jFrame cahPlayer.java instances went fine, I think:

 

        if (numPlayers == 3)
        {
            cahPlayer player1 = new cahPlayer();
            cahPlayer player2 = new cahPlayer();
            cahPlayer player3 = new cahPlayer();
        }
        else if (numPlayers == 4)
        {
            cahPlayer player1 = new cahPlayer();
            cahPlayer player2 = new cahPlayer();
            cahPlayer player3 = new cahPlayer();
            cahPlayer player4 = new cahPlayer();
        }
        else if (numPlayers == 5)
        {
            cahPlayer player1 = new cahPlayer();
            cahPlayer player2 = new cahPlayer();
            cahPlayer player3 = new cahPlayer();
            cahPlayer player4 = new cahPlayer();
            cahPlayer player5 = new cahPlayer();
        }
        else if (numPlayers == 6)
        {
            cahPlayer player1 = new cahPlayer();
            cahPlayer player2 = new cahPlayer();
            cahPlayer player3 = new cahPlayer();
            cahPlayer player4 = new cahPlayer();
            cahPlayer player5 = new cahPlayer();
            cahPlayer player6 = new cahPlayer();
        }

This is done in the startNewGame() method in the class that contains all of the game-logic.

Now, from the main game window, the cahUI.java jFrame I want to change the jTextField called PBC, located in the cahPlayer jFrame, and inset a String.

So I have tried this:
 

        cahPlayer.player1.PBC.setText(blackCard.getText());

 in the cahUI.java jFrame. (blackCard.getText() is just a String from a file handled in another method).

 

But I get the error:

 

"Can't find symbol:

symbol: variable player 1

location: class cahPlayer"

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Claryn said:

snip

 

I tried declaring the instance of the jFrame in the method that tries to change the PBC textfield:

 

cahPlayer s = new cahPlayer();
s.PBC.setText(blackCard.getText);

 This does not show any errors (don't know if it will work or not. Can't really run the program atm).

 

I assume that the method does not recognize the instances that was made in the startNewGame() method in the other class? Any way to solve that?

 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Claryn said:

I am sorry, but neither of you got my problem, I think. @BlueDragon @Philosobyte

 

I have made a jFrame Form in NetBeans. I have set up the design for it with the drag and drop functionality of NetBeans. It contains 11 labels, 12 buttons and 11 textfields. The class contains over 3000 lines of auto-generated code from Netbeans.

The jFrame Form is called playerUI(.class)

 

When the game starts, you are prompted with a small window that asks for an integer input of how many players there are.

 

This is what I want:

When the input is received, the appropriate amount of playerUI windows are supposed to pop up. They are all identical, except for 1 label that is changed (Player1, Player2, Player 3 etc.). 

I assume I can make these instances by simply doing 

playerUI player1UI = new playerUI; for all the players. 

 

Then, I need to have control over all the buttons and textfields in the individual windows, from another class (all buttons and textfields are public). Can I then simply use player1UI.jTextField1.setText(); etc?

Hmmm... I think I understand what you want.

 

Would it be possible for me to have all your code to come up with a solution? 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, BlueDragon said:

Hmmm... I think I understand what you want.

 

Would it be possible for me to have all your code to come up with a solution? 

Not so sure if that is a very good idea.

 

I have over 10 classes, 7000 lines of code (mostly generated by NeBeans) and most of it is not working atm. because I am reusing a lot of code form an earlier project.

 

So I'll give you what you need in pastebins:
 

http://pastebin.com/dfnquJu4
This is playGame.java. It is the class where I make the instances of the jFrame cahPlayers and call methods to deal cards. These methods are in cahPlayer as you can see in the code. P1C1 is a jTextField that holds the text of the card for Player1. P2C2 is for Player2 etc.  (The cards are Strings of texts taken from a file directory, and plotted into two Linked Lists.). It is not finished, but I have shown the dealing of the first card to 4 players. 

 

http://pastebin.com/skwT76wk

This is cahPlayer.java. It is the jFrame form that I want several instances of. 99% of the code here is generated when I made the UI with the drag-and-drop module of NetBeans. It is marked where it is generated. 
The first few methods are the P1C1, P2C1 methods etc. 

Here I want to change the same  jTextField for the different instances, player1, player2, player3 and player4. 

 

That is what you would need to get my problem. 

 

In addition, I am unsure how to actually display all 4 windows for the players. I assume that player1.setVisible(true); should be enough?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Claryn said:

snippity snipsies

Two problems:

 

1. Variables are only visible within the code block which they were declared in. Therefore, in your series of if statements, each cahPlayer (player1, player2, player3, etc.) can only be used and seen within the if block. That's why they have to be declared outside of the if block, like this:

public void example1(int numPlayers) 
{
	if (numPlayers == 3)
	{
		cahPlayer player1 = new cahPlayer();
		cahPlayer player2 = new cahPlayer();
		cahPlayer player3 = new cahPlayer();
	}

	player1.doSomething();		//this will fail because we're attempting to call player1 out of its block. 
}

public void example2(int numPlayers) 
{
	cahPlayer player1;
	cahPlayer player2;
	cahPlayer player3;

	if (numPlayers == 3) 
	{
		player1 = new cahPlayer();
		player2 = new cahPlayer();
		player3 = new cahPlayer();
	}

	player1.doSomeAction();		//this will be successful because we're accessing player1 from its block.
}

 

2. When you use cahPlayer.player1.PBC.setText(), you're assuming that cahPlayer owns the variable player1. The format is Class.staticVariable.doSomething() or object.instanceVariable.doSomething(); Both of these statements are basically saying "the Class owns the staticVariable" or "the object owns the instance variable"

However, since you're declaring player1 inside of a method, it's a local variable and it's not owned by an object or class. Therefore, you would have to put only player1.PBC.setText() instead of cahPlayer.player1.PBC.setText();

 

These two reasons are why you're getting errors. I haven't looked at the pastebin code yet.

 

Also, keep in mind that all classes should be capitalized. So it should really be CahPlayer instead of cahPlayer. 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Philosobyte said:

Two problems:

 

1. Variables are only visible within the code block which they were declared in. Therefore, in your series of if statements, each cahPlayer (player1, player2, player3, etc.) can only be used and seen within the if block. That's why they have to be declared outside of the if block, like this:


public void example1(int numPlayers) 
{
	if (numPlayers == 3)
	{
		cahPlayer player1 = new cahPlayer();
		cahPlayer player2 = new cahPlayer();
		cahPlayer player3 = new cahPlayer();
	}

	player1.doSomething();		//this will fail because we're attempting to call player1 out of its block. 
}

public void example2(int numPlayers) 
{
	cahPlayer player1;
	cahPlayer player2;
	cahPlayer player3;

	if (numPlayers == 3) 
	{
		player1 = new cahPlayer();
		player2 = new cahPlayer();
		player3 = new cahPlayer();
	}

	player1.doSomeAction();		//this will be successful because we're accessing player1 from its block.
}

 

2. When you use cahPlayer.player1.PBC.setText(), you're assuming that cahPlayer owns the variable player1. The format is Class.staticVariable.doSomething() or object.instanceVariable.doSomething(); Both of these statements are basically saying "the Class owns the staticVariable" or "the object owns the instance variable"

However, since you're declaring player1 inside of a method, it's a local variable and it's not owned by an object or class. Therefore, you would have to put only player1.PBC.setText(); 

 

These two reasons are why you're getting errors. I haven't looked at the pastebin code yet.

I made some changes to the pastebins, but it is explained.

 

How should I then declare the objects? I need to be able to change the instance-variables in a number of different methods. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Claryn said:

I made some changes to the pastebins, but it is explained.

 

How should I then declare the objects? I need to be able to change the instance-variables in a number of different methods. 

It could be solved if you declared all the objects inside of a class, but outside of a method. Like so:

public class PlayGame {

	public static cahPlayer player1;
	public static cahPlayer player2;
	public static cahPlayer player3;
	public static cahPlayer player4;
	public static cahPlayer player5;
	public static cahPlayer player6;

    public static void startNewGame(int numPlayers)
    {
 
        if (numPlayers == 3)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
        }
		/* etcetera, skipping 4 and 5 */
        else if (numPlayers == 6)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
            player5 = new cahPlayer();
            player6 = new cahPlayer();
        }
	}
}

In this case, each player is a class variable because each player is static. This means that the class PlayGame owns the players. Since PlayGame owns the players, you can access each player within PlayGame by simply calling "player1" or "player2," but if you're outside of PlayGame you access them by calling "PlayGame.player1" or "PlayGame.player2" etc. This setup should work. 

 

Have you learned arraylists yet? If not, ignore this next section. If you have learned arraylists, then this would be a very good time to use them to make sure that you can do more than six players without repeating a bunch of unnecessary code. For example, the following is a replacement for the above code which does the same thing, but is much more flexible with how many players you can input: 

public class PlayGame {

	public static ArrayList<cahPlayer> list = new ArrayList<>();

	public static void startNewGame(int numPlayers) 
    	{
		for(int i = 0; i < numPlayers; i++) 
        	{
        	list.add(new cahPlayer());
        	}
    	}

	//if I want to access a cahPlayer's variables, I can just do:
	public void example(int indexOfPlayer) 
    	{
		list.get(indexOfPlayer).textField1.setText("etc.");
	}
    
//An alternative to using lists is to use a map, if you want to give a name to each player and be able to access each player by name. But you might not have learned maps yet, so I'll hold off on recommending them. 
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Philosobyte said:

It could be solved if you declared all the objects inside of a class, but outside of a method. Like so:


public class PlayGame {

	public static cahPlayer player1;
	public static cahPlayer player2;
	public static cahPlayer player3;
	public static cahPlayer player4;
	public static cahPlayer player5;
	public static cahPlayer player6;

    public static void startNewGame(int numPlayers)
    {
 
        if (numPlayers == 3)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
        }
		/* etcetera, skipping 4 and 5 */
        else if (numPlayers == 6)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
            player5 = new cahPlayer();
            player6 = new cahPlayer();
        }
	}
}

In this case, each player is a class variable because each player is static. This means that the class PlayGame owns the players. Since PlayGame owns the players, you can access each player within PlayGame by simply calling "player1" or "player2," but if you're outside of PlayGame you access them by calling "PlayGame.player1" or "PlayGame.player2" etc. This setup should work. 

 

Have you learned arraylists yet? If not, ignore this next section. If you have learned arraylists, then this would be a very good time to use them to make sure that you can do more than six players without repeating a bunch of unnecessary code. For example, the following is a replacement for the above code which does the same thing, but is much more flexible with how many players you can input: 


public class PlayGame {

	public static ArrayList<cahPlayer> list = new ArrayList<>();

	public static void startNewGame(int numPlayers) 
    	{
		for(int i = 0; i < numPlayers; i++) 
        	{
        	list.add(new cahPlayer());
        	}
    	}

	//if I want to access a cahPlayer's variables, I can just do:
	public void example(int indexOfPlayer) 
    	{
		list.get(indexOfPlayer).textField1.setText("etc.");
	}
    
//An alternative to using lists is to use a map, if you want to give a name to each player and be able to access each player by name. But you might not have learned maps yet, so I'll hold off on recommending them. 
}

Couldn't of said it better my self :) With the arraylist, I would personally have a variable in the cahPlayer class which would be used so that you could identify which class belongs to which player :)

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Philosobyte said:

It could be solved if you declared all the objects inside of a class, but outside of a method. Like so:


public class PlayGame {

	public static cahPlayer player1;
	public static cahPlayer player2;
	public static cahPlayer player3;
	public static cahPlayer player4;
	public static cahPlayer player5;
	public static cahPlayer player6;

    public static void startNewGame(int numPlayers)
    {
 
        if (numPlayers == 3)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
        }
		/* etcetera, skipping 4 and 5 */
        else if (numPlayers == 6)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
            player5 = new cahPlayer();
            player6 = new cahPlayer();
        }
	}
}

In this case, each player is a class variable because each player is static. This means that the class PlayGame owns the players. Since PlayGame owns the players, you can access each player within PlayGame by simply calling "player1" or "player2," but if you're outside of PlayGame you access them by calling "PlayGame.player1" or "PlayGame.player2" etc. This setup should work. 

 

Have you learned arraylists yet? If not, ignore this next section. If you have learned arraylists, then this would be a very good time to use them to make sure that you can do more than six players without repeating a bunch of unnecessary code. For example, the following is a replacement for the above code which does the same thing, but is much more flexible with how many players you can input: 


public class PlayGame {

	public static ArrayList<cahPlayer> list = new ArrayList<>();

	public static void startNewGame(int numPlayers) 
    	{
		for(int i = 0; i < numPlayers; i++) 
        	{
        	list.add(new cahPlayer());
        	}
    	}

	//if I want to access a cahPlayer's variables, I can just do:
	public void example(int indexOfPlayer) 
    	{
		list.get(indexOfPlayer).textField1.setText("etc.");
	}
    
//An alternative to using lists is to use a map, if you want to give a name to each player and be able to access each player by name. But you might not have learned maps yet, so I'll hold off on recommending them. 
}

 

Alright I will give this a shot. Thank you very much!
 

I have learned about arrays, but never used ArrayList, as it is not part of my syllabus. We simply fill arrays with for-loops and so on. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Philosobyte said:

 

snip

 

I believe what you meant with the ArrayList is that there is a system for not dealing with this:
http://pastebin.com/yR0V0LVU

 

I always thought that if I could make it so that all I do in a loop, is change the name of the variable.

Like player1++ makes player2. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

Because of the instanced jFrames (which should work, I just can't test it because the program won't run due to a lot of unfinished code), I have some problems with one of my validation algorithms.

It is a card game where the role as the dealer is passed on to a new, random player each turn. That player is not allowed to play a card that round.

So I have made a small method that I used in my old version of the program, and now I did some modifications for it to make sense to you:
 

    private void PC1SelectActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        if (!cahUI.currentDealer.getText().equals("Player 1")) //Need to validate that THIS player is not the dealer.
        {
            cahUI.selectedCards[0] = PC1.getText();
            this.PC1.setText(dealWhiteCards.getCard());

        }
        else
            JOptionPane.showMessageDialog(null, "You are the dealer!", "Error", JOptionPane.ERROR_MESSAGE);
    } 

This is in the cahPlayer.java jFrame. 

 

There is a jTextField in cahUI.java that displays the current dealer. 

So when the a player tries to select a card, in this case PC1 (card1), the PC1Select jButton check if the player is the dealer or not. 

The problem now is that I used to simply do a string.equals() check, but now it won't work because of the instanced jFrames. 

 

Do you have any suggestions for what to do?
 

I also realize now that it was stupid to add that if-statement on all the 10 Select buttons for the 10 cards. I can do one at the start of each turn for each player, and then just change a boolean variable so the error-message is displayed instead. 

 

Also, I assume that the line: this.PC1.setText(dealWhiteCards.getCard()); is the way to go with instanced jFrames?

 

Edit:
Tags so you get a notification @BlueDragon @Philosobyte

Edited by Claryn

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Claryn said:

Also, I assume that the line: this.PC1.setText(dealWhiteCards.getCard()); is the way to go with instanced jFrames?

Let me try to explain as best I can...

 

You do not need to use "this"... because every variable within the class belongs to that class. You only need to use "this" when you have a variable within a method that is called the same as a global variable.... for example if I had a constructor like this:

 

public classs Myclass{
 
  int id;
  
  public Myclass(int id){
    // I have to use the "this" keyword so that we access the variable that is outside the method.
    this.id = id;
  }
  
}

 

However if we wanted to access the id from a different method we do not have to use "this"...

public classs Myclass{
 
  int id;
  
  public Myclass(int id){
    // I have to use the "this" keyword so that we access the variable that is outside the method.
    this.id = id;
  }

	public void printId{
		// As you can see... we use the id variable however we do not use the "this" keyword because there are no variables in the method which have teh same name as the id
		System.out.println(id);
	}
  
}

@Claryn

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, BlueDragon said:

Let me try to explain as best I can...

 

You do not need to use "this"... because every variable within the class belongs to that class. You only need to use "this" when you have a variable within a method that is called the same as a global variable.... for example if I had a constructor like this:

 


public classs Myclass{
 
  int id;
  
  public Myclass(int id){
    // I have to use the "this" keyword so that we access the variable that is outside the method.
    this.id = id;
  }
  
}

 

However if we wanted to access the id from a different method we do not have to use "this"...


public classs Myclass{
 
  int id;
  
  public Myclass(int id){
    // I have to use the "this" keyword so that we access the variable that is outside the method.
    this.id = id;
  }

	public void printId{
		// As you can see... we use the id variable however we do not use the "this" keyword because there are no variables in the method which have teh same name as the id
		System.out.println(id);
	}
  
}

 

Alright thank you. That clarified the somewhat confusion concept of this in programming.

 

Do you have any suggestions to how to solve the dealer-check?

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Claryn said:

Alright thank you. That clarified the somewhat confusion concept of this in programming.

 

Do you have any suggestions to how to solve the dealer-check?

Am I right in thinking that you have a class that handles the logic behind the game and a class that handles the GUI's for each player?

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, BlueDragon said:

Am I right in thinking that you have a class that handles the logic behind the game and a class that handles the GUI's for each player?

I have a main game window, cahUI.java, the player's window cahPlayer.java (instanced), and then I have a playGame.java class that handles most of the logic, yes. 
However, this part is in the cahPlayer jFrame class, since it is checking whenever the buttons are pressed to select a card that the player wants to play. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Claryn said:

I have a main game window, cahUI.java, the player's window cahPlayer.java (instanced), and then I have a playGame.java class that handles most of the logic, yes. 
However, this part is in the cahPlayer jFrame class, since it is checking whenever the buttons are pressed to select a card that the player wants to play. 

Are you crating the instances of cahPlayer in the playGame class?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, BlueDragon said:

Are you crating the instances of cahPlayer in the playGame class?

Yes. Just like this:
 

public class playGame {
    
    public static cahPlayer player1;
    public static cahPlayer player2;
    public static cahPlayer player3;
    public static cahPlayer player4;
    public static cahPlayer player5;
    public static cahPlayer player6;
    
    
    public static void startNewGame(int numPlayers)
    {

        if (numPlayers == 3)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
        }
        else if (numPlayers == 4)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
        }
        else if (numPlayers == 5)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
            player5 = new cahPlayer();
        }
        else if (numPlayers == 6)
        {
            player1 = new cahPlayer();
            player2 = new cahPlayer();
            player3 = new cahPlayer();
            player4 = new cahPlayer();
            player5 = new cahPlayer();
            player6 = new cahPlayer();
        }

 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×