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 5 of 5

Thread: Why, won't this update?

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

    Default Why, won't this update?

    Okay, basically i'm using an object to get some ints to print out but it won't update...

    GameLoader Class (extends jframe)
    public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
     
    Image img1 = Toolkit.getDefaultToolkit().getImage("sprites/background.gif");
    g2.drawImage(img1, 0, 0, this);
    g2.finalize();
    // Start Text
    PlayerAssistant getPA = new PlayerAssistant();
    g.setColor(Color.green);
    g.drawString("Village Health: "+getPA.vHealth, 30, 65);
    g.drawString("Resource - Wood: "+getPA.rWood, 30, 90);
    g.drawString("Resource - Plants: "+getPA.rPlants, 30, 115);
    g.drawString("Resource - Stone: "+getPA.rStone, 30, 140);
    g.drawString("Village Value: "+getPA.vValue, 30, 165);
    g.drawString("Village Strength: "+getPA.vStrength+"%", 30, 190);
    g.drawString("Population: "+getPA.Population, 30, 215);
    }
     
    public static void main(String[] args)
    {
    PlayerAssistant getPA = new PlayerAssistant();
    getPA.loadPlayer();
    new GameLoader();
     
     
    }

    PlayerAssistant
    public void loadPlayer() {
    vHealth = 100;
    rWood = 0;
    rPlants = 0;
    rStone = 0;
    vValue = 1000;
    vStrength = 1;
    Population = 20;
    }

    Sorry i'm pretty new to this...
    Last edited by copeg; October 5th, 2012 at 09:04 PM. Reason: Reverted to original post


  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: Why, won't this update?

    What exactly do you mean when you say it doesn't update? Why are you using paint() instead of paintComponent() and a call to super.paintComponent()? Why are you finalizing the Graphics instance before you're done using it?
    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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why, won't this update?

    Quote Originally Posted by Jerba View Post
    Okay, basically i'm using an object to get some ints to print out but it won't update...
    You're doing a couple of things wrong here. Let's dive in:

    GameLoader Class (extends jframe)
    public void paint(Graphics g) {
    Don't draw directly in a JFrame. The drawing instead should be inside of the paintComponent(...) method of a JPanel or other class that derives from JComponent. This way you don't lose the benefits of Swing drawing including double buffering, and this way you don't accidently misdraw child components or borders.

    	    Graphics2D g2 = (Graphics2D) g;
     
    	    Image img1 = Toolkit.getDefaultToolkit().getImage("sprites/background.gif");
    Don't read in files from within the paint or paintComponent methods as this is unnecessary -- why read in a file many times when once will do? and also this will slow down the responsiveness of the program greatly. Read it in the constructor or someother initialization code.

    	    g2.drawImage(img1, 0, 0, this);
    	    g2.finalize();
    finalize? where did that come from? This is not only unnecessary but is also very dangerous. Just don't do this. Ever.

    	    // Start Text
    	    PlayerAssistant getPA = new PlayerAssistant();
    Don't do program logic from within paint or paintComponent. Realize that you do not have control over when or if this method gets called.
    Last edited by curmudgeon; October 5th, 2012 at 06:43 PM.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Why, won't this update?

    Quote Originally Posted by Jerba View Post
    FIIIIIIIIIIIIIIIIIIIIIXEEEEEEED
    Thanks for deleting your question. This is inconsiderate to those who put time in to help you, not to mention future users of this forum who may have the same problem.

    Instead wouldn't it have better to have posted your fix as a reply?
    Last edited by curmudgeon; October 5th, 2012 at 06:47 PM.

  5. The Following 3 Users Say Thank You to curmudgeon For This Useful Post:

    copeg (October 5th, 2012), KevinWorkman (October 5th, 2012), Tjstretch (October 5th, 2012)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why, won't this update?

    Well said curmudgeon. The mod team has access to post edits, and in cases such as this have the option to revert the edited post, which I have elected to do.

    Jerba, I recommend to avoid editing your posts to entirely remove your questions in the manner you have

  7. The Following User Says Thank You to copeg For This Useful Post:

    curmudgeon (October 5th, 2012)

Similar Threads

  1. update .jar file?
    By denti in forum Java Theory & Questions
    Replies: 7
    Last Post: June 28th, 2012, 10:07 AM
  2. update database from List<>
    By beruska in forum JDBC & Databases
    Replies: 3
    Last Post: November 7th, 2011, 03:21 PM
  3. Why my update.jsp is not working?
    By tangara in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: September 8th, 2011, 01:14 AM
  4. java + mysql update
    By A4Andy in forum JDBC & Databases
    Replies: 6
    Last Post: August 26th, 2011, 09:04 PM