Jump to content

GUI Help


imancity

Recommended Posts

Hey guys, so I followed Explv's GUI tutorial to try my hand at adding a GUI for the firs time. So far there's a dropdown box and the user chooses an option. It's saved as a string though. This must be fairly simple but I can't seem to figure it out; I need the option they choose to correspond to an integer value in my script to decide what object an action is being done towards, ie: what ore is being mined.

 

Thanks! I can clear it up more if it doesn't make sense.

Link to comment
Share on other sites

Under ideal circumstances you would have enum types for each of the ores and you would instead of switching between strings in the combobox be dealing with objects. However if this is not the case you can always whip out the classic switch statement and deal with it that way. While it's not ideal or tidy, it should be easy for you to understand and should get the job done and should require minimal structure changes

 

edit: hav a quick read of this: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Edited by Apaec
  • Like 1
Link to comment
Share on other sites

Hey guys, so I followed Explv's GUI tutorial to try my hand at adding a GUI for the firs time. So far there's a dropdown box and the user chooses an option. It's saved as a string though. This must be fairly simple but I can't seem to figure it out; I need the option they choose to correspond to an integer value in my script to decide what object an action is being done towards, ie: what ore is being mined.

 

Thanks! I can clear it up more if it doesn't make sense.

 

Nevermind, I did not realize you meant an object id.  Yeah use an enum.

Edited by Manner
Link to comment
Share on other sites

Under ideal circumstances you would have enum types for each of the ores and you would instead of switching between strings in the combobox be dealing with objects. However if this is not the case you can always whip out the classic switch statement and deal with it that way. While it's not ideal or tidy, it should be easy for you to understand and should get the job done and should require minimal structure changes

 

edit: hav a quick read of this: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

 

Thanks man! I'll definitely read that and put it to use. I may just do the switches for now though as this is a small personal script.

 

If you are using a JComboBox, just use getSelectedIndex() to get an integer, if I read this correctly

 

How do you mean? I am using that, but there's lets say 3 options in the combobox. I want to associate each with a 4 digit integer and then once one is selected have that integer be used in the script.

 

You need to make a JButton which you can label "Start" for example.

Then you need to give it an ActionListener which then sets the ID's of the rocks / locations you want to mine at based on your users selection from other elements in the gui smile.png

 

I do have a start button and ActionListener to start the script, how does it set the ID's tho?

Nvm I think I get what needs to be done. Thanks guys!

Link to comment
Share on other sites

Thanks man! I'll definitely read that and put it to use. I may just do the switches for now though as this is a small personal script.

 

 

How do you mean? I am using that, but there's lets say 3 options in the combobox. I want to associate each with a 4 digit integer and then once one is selected have that integer be used in the script.

 

 

I do have a start button and ActionListener to start the script, how does it set the ID's tho?

Nvm I think I get what needs to be done. Thanks guys!

 

Question 2:

He is saying that an easy way to assign a "4 digit integer" which is used for the rock id is by getting the selected index and using a switch case. For instance (pseudo code):

int rockId;
switch(comboBox.getSelectedIndex())
case 0:
rockId = 1111;
break;
case 1:
rockId = 2222;
break;

You would put this code in your action listener. 

  • Like 2
Link to comment
Share on other sites

public class GUI extends JFrame {

	private JPanel contentPane;

	private Boolean finished = false;

	public Boolean isFinished() {
		return finished;
	}

	public Rock getSelection() {
		return (Rock) comboBox.getSelectedItem();
	}

	JComboBox<Rock> comboBox;

	public GUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JButton btnNewButton = new JButton("Start");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				finished = true;
			}
		});
		contentPane.add(btnNewButton, BorderLayout.WEST);

		comboBox = new JComboBox<Rock>();
		comboBox.setModel(new DefaultComboBoxModel<Rock>(Rock.values()));
		contentPane.add(comboBox, BorderLayout.CENTER);
		setVisible(true);
	}

}

Where Rock.java is defined as

public enum Rock {
	TIN(3151), IRON(215), CLAY(45), COAL(211);
	
	private int color;
	
	Rock(int color) {
		this.color=color;
	}
	
	public int getColor() {
		return color;
	}
}

Using modified color is a much better way to distinguish rocks. Every rock type has lots of ids based on the mesh but the color is the same for all of them, hence it's a better approach. You can either use the enum model comboboxes or get enum fields using the Enum#valueOf() on a String based model.

 

EDIT: those rock colors are not real, it's just for the sake of the example, I can't be bothered opening eclipse and looking through my projects for my actual Rock enum

Edited by Token
  • Like 2
Link to comment
Share on other sites

Question 2:

He is saying that an easy way to assign a "4 digit integer" which is used for the rock id is by getting the selected index and using a switch case. For instance (pseudo code):

int rockId;
switch(comboBox.getSelectedIndex())
case 0:
rockId = 1111;
break;
case 1:
rockId = 2222;
break;

You would put this code in your action listener. 

 

Ohh I got it. Thats so simple too.. thanks Alek!

 

public class GUI extends JFrame {

	private JPanel contentPane;

	private Boolean finished = false;

	public Boolean isFinished() {
		return finished;
	}

	public Rock getSelection() {
		return (Rock) comboBox.getSelectedItem();
	}

	JComboBox<Rock> comboBox;

	public GUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JButton btnNewButton = new JButton("Start");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				finished = true;
			}
		});
		contentPane.add(btnNewButton, BorderLayout.WEST);

		comboBox = new JComboBox<Rock>();
		comboBox.setModel(new DefaultComboBoxModel<Rock>(Rock.values()));
		contentPane.add(comboBox, BorderLayout.CENTER);
		setVisible(true);
	}

}

Where Rock.java is defined as

public enum Rock {
	TIN(3151), IRON(215), CLAY(45), COAL(211);
	
	private int color;
	
	Rock(int color) {
		this.color=color;
	}
	
	public int getColor() {
		return color;
	}
}

Using modified color is a much better way to distinguish rocks. Every rock type has lots of ids based on the mesh but the color is the same for all of them, hence it's a better approach. You can either use the enum model comboboxes or get enum fields using the Enum#valueOf() on a String based model.

 

EDIT: those rock colors are not real, it's just for the sake of the example, I can't be bothered opening eclipse and looking through my projects for my actual Rock enum

 

 

Hmm so each ore/rock has a different color associated with it? I knew they had different colors in-game but didn't think that would correspond well to scripting. Thanks man! When I start my next project with a mining script I will for sure use this. Appreciate it!

Link to comment
Share on other sites

Ohh I got it. Thats so simple too.. thanks Alek!

 

 

Hmm so each ore/rock has a different color associated with it? I knew they had different colors in-game but didn't think that would correspond well to scripting. Thanks man! When I start my next project with a mining script I will for sure use this. Appreciate it!

 

Ids change almost every game update so don't use them. If you open the options tab > debug > entity hover debug, you will get a list of what different values are if you hover over a rock. Rocks are a bit tricky but modified colours works and is unaffected by non-specific game updates

  • Like 1
Link to comment
Share on other sites

Question 2:

He is saying that an easy way to assign a "4 digit integer" which is used for the rock id is by getting the selected index and using a switch case. For instance (pseudo code):

int rockId;
switch(comboBox.getSelectedIndex())
case 0:
rockId = 1111;
break;
case 1:
rockId = 2222;
break;

You would put this code in your action listener. 

 

Hey Alek, I have a question regarding this, but for some reason I keep getting errors when trying to PM you as to not clutter this thread. Could you PM me or possibly even add me on Skype? Not sure if you do that.

 

Anyways if neither of those work, I'll explain here just in case.

 

I tried adding the switch to my Action Listener for the sake of this script and then I can use other methods on my next one, but it's telling me the getSelectedIndex cannot be accessed from a static context.

 

Here is what I have in my GUI void:

oreSelector.addActionListener(e -> {
            switch(JComboBox.getSelectedIndex()) {
                case 0:
                    rockID = 1111;
                    break;
                case 1:
                    rockID = 2222;
                    break;
            }
        });

The "getSelectedIndex()" is what gives me the error. Sorry if this is something obvious, I can't get it right tho

 

Edited by imancity
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...