Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Better way of doing this?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Better way of doing this?

    Okay, right before, you read the code its self i'm really new to this, even though i have a slight knowledge of java as a language i'm still getting a bit confused on whats the best way to do things...

    Okay, i'm making a game... and on the game client i want it to print the following variables...
    level
    xptnl
    workers
    coins

    Okay, now at the moment i'm doing this straight from my GameLoader class which extends JFrame

    Now i would prefer to load this information from my playerdata class instead of straight out of my Gameloader...

    Heres what i have so far...
    public void paint(Graphics g) {
    		this.level = 0;
    		this.xptnl = 0;
    		this.workers = 0;
    		this.coins = 50000;
    			try {
    		java.net.InetAddress i = java.net.InetAddress.getLocalHost();
    		String characterName = i.getHostName(); 
    		g.setColor(Color.lightGray);
    		g.fillRect(0,0,600,400);
    		g.setColor(Color.RED);
    		g.drawString("Username: "+characterName+"", 7, 40);
    		g.drawString("Level: "+level+"", 180, 40);
    		g.drawString("Exp from "+level+1+ " is : "+xptnl, 250, 40);
    		g.drawString("workers: "+workers, 370, 40);
    		g.drawString("coin stack: "+coins, 450, 40);
    		}
    	       catch(Exception e){e.printStackTrace();}  
    	}

    To me this seems an extremely stupid way of doing it...

    How would i load this information from my playerdata class?

    Could i maybe use an object?

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Better way of doing this?

    You have to pass an instance of one of the classes to the other (perhaps through a parameter in the constructor or a setter method) then call a setter or a getter to get at the info you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Better way of doing this?

    Setters & Getters How could i forget!!!! Thanks alot :')