Jump to content

Java Programming.


Recommended Posts

hey guys, whats up?

come in come in, please do.

 

JEEZ!, take off those shoes, god knows where you have been.:confused:

 

Well lets get started.

Do we have any Java programmers here?

Im making a roleplaying app for oblivion, its going to create a completely random and unique backstory for the `Serious Roleplayer'.

Im doing this of course using strings, mashing them together. The best way aha.

 

Im having an issue, and I was hoping someone could help.

Ive got my string set up, lets say its name. Ive got it to generate random names. Good, great.

But when the user clicks the button, it writes the result to a JLabel, we will call it `Scroll.,'

 

Now, heres my BIG issue, If I can get this figured out, I can release the application.

 

How, in gods name, do I get my button to create a random result every time it is clicked?

If someone can anser this and help me get it working, im offering a £5 reward.

Its not much, but it could be a lot more depending on how happy I am feeling aha.

 

So come on in, or if you know a friend who knows some java, link em to this topic., and let them earn some money.:laugh:

Link to comment
Share on other sites

No no, im not THAT bad. But I am new.

I shall glady give you payment if you could help me with this little problem. :)

 

here is a stripped down version of the `name masher together randomiser,' as I call it.

public static  String[] name1 = {"a","b","c","d","e","f","g","h","i","j","k","l","n","o","p","q","r","s",
		   "t","u","v","w","x","y","z"};
public static String[] name2 = {"oh noes","its not working","sad face"};
public static int name1length = name1.length;
public static int name2length = name2.length;
public static int rand1 = (int)(Math.random()*name1length);
public static int rand2 = (int)(Math.random()*name2length);
public static String phrase = name1[rand1] + " " + name2[rand2];
public void buttonNameActionPerformed(java.awt.event.ActionEvent evt) {
scroll.setText(phrase);

 

 

In my mind I want to put all of those methods that make up the name generation into like...hmm a small box? if you understand.

and have a `small box,' for every random trait - e.g eye colour, home town, age ect.

I hope you get what I mean. I think I explained it well, but its hard to put it into terms.

I think the correct term is encapsulation? that word has been thrown at me a lot.:thumbsup:

Link to comment
Share on other sites

I'm going to assume this is running on the Event Dispatch thread (if you're not using multiple threads, then you don't need to worry about this), if it's not, you'll need to use SwingUtilities.InvokeAndWait() to update the button. If you haven't messed around with threads, that shouldn't be a problem.

 

AWT and Swing are probably the two worst GUI libraries I've ever had the misfortune of having to use in any language ever, so there could be any number of silly reasons why it's not working. You could try and force the button to redraw by calling repaint(), validate(), or pack() on the container it's contained in (presumably a JPanel or JFrame or something) right after you update the string, and see if any of those works.

 

In my mind I want to put all of those methods that make up the name generation into like...hmm a small box? if you understand.

and have a `small box,' for every random trait - e.g eye colour, home town, age ect.

I hope you get what I mean. I think I explained it well, but its hard to put it into terms.

I think the correct term is encapsulation? that word has been thrown at me a lot.:thumbsup:

 

 

What do you mean by a 'small box'? You can create a separate class with all your random generation methods in, if that's what you mean. Could even make it static, if you don't plan to use it for anything else. Something like this:

 

static class RandomGen {

public static String[] name1 = { "a", "b", "etc" };
//more name variables etc

public static String GenName() {
	String name = new String();

	name = //name generation code here
	return name;
}

public static String GenHomeTown() {
	//etc
}
}

 

Encapsulation is when you make a variable within a class private, and then use a method to access it - meaning you can change the variable etc within the class, without having to worry too much about other classes accessing it. eg:

 

private String something;

public String getSomething() {
return something;
}

public void setSomething(String str) {
something = str;
}

Edited by The_Terminator
Link to comment
Share on other sites

This

 

public void buttonNameActionPerformed(java.awt.event.ActionEvent evt) {

 

Needs to call a function that does all of the other things. Right now it's just setting the text in the label.

Link to comment
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
  • Recently Browsing   0 members

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