Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to update constructor?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Blockgen




PostPosted: Mon Jun 03, 2013 3:41 pm   Post subject: How to update constructor?

I'm trying to make a game where the ship is moving left or right. However I need a way to update the constructor, or the JFrame. Do I have to use paint (Graphics g), or is there a more efficient method than doing what I am now?
code:

package mi;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;

public class Game implements KeyListener {

    ImageIcon stars = new ImageIcon("resources/background.jpg");
    ImageIcon sship = new ImageIcon("resources/ship.png");
    ImageIcon life = new ImageIcon("resources/life.png");
    Font text = new Font("Arial", Font.PLAIN, 18);
    JFrame f = new JFrame("Multiplication Nation");
    JLabel ship;
    static int shipx = 100;

    public Game() {
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBounds(200, 100, 406, 522);
        f.setLayout(null);
        //f.setFocusable(true);

        ;
        JLayeredPane lp = new JLayeredPane();
        lp.setBounds(0, 0, 406, 522);

        JLabel background = new JLabel(stars);
        background.setSize(400, 500);

        f.setFocusable(true);
        f.addKeyListener(this);

        //Life
        JLabel[] hearts = new JLabel[5];//change to variable for health
        int heartx = 360;
        int hearty = 10;
        for (int i = 0; i != 5; i++) //change to variable for health
        {
            hearts[i] = new JLabel(life);
            hearts[i].setBounds(heartx, hearty, 30, 27);
            heartx -= 35;
            lp.add(hearts[i], new Integer(2));
        }

        //Controls
        ship = new JLabel(sship);
        ship.setBounds(shipx, 370, 100, 100);
        lp.add(background, new Integer(1));
        lp.add(ship, new Integer(2));

        f.add(lp);
        f.show();
            }

    public void keyPressed(KeyEvent a) {
        int keyCode = a.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT) {
            shipx -= 100;
            f.dispose();
            new Game();
       
        }
        if (keyCode == KeyEvent.VK_RIGHT) {
            shipx += 100;
       
            f.dispose();
            new Game();   
        }
    }

    public void keyReleased(KeyEvent a) {
    }

    public void keyTyped(KeyEvent a) {
    }

    public static void main(String[] args) {

        new Game();

    }
}


Thanks in advance.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jun 03, 2013 3:43 pm   Post subject: RE:How to update constructor?

Why do you need to update the constructor? Why don't you try giving it parameters instead?
Blockgen




PostPosted: Mon Jun 03, 2013 6:34 pm   Post subject: Re: How to update constructor?

I need to update it so that when the x value changes for 'ship', the frame can register it.
And parameters for what?
Insectoid




PostPosted: Mon Jun 03, 2013 7:18 pm   Post subject: RE:How to update constructor?

I don't think you understand what the constructor is supposed to do.

However, if I remember right, changing a jlabel will automatically update its parent (the jframe).
Blockgen




PostPosted: Mon Jun 03, 2013 7:46 pm   Post subject: Re: How to update constructor?

My bad, I meant updating the component.
And what do you mean by changing the JLabel. I'm looking for a .repaint() type method without using paint.
andrew.




PostPosted: Mon Jun 03, 2013 10:05 pm   Post subject: RE:How to update constructor?

I'm a bit rusty with Java but I think what he means is to just update the contents of your JLabel to reflect your new values and that update will bubble up to the JFrame to redraw itself.
Zren




PostPosted: Mon Jun 03, 2013 10:37 pm   Post subject: RE:How to update constructor?

code:

public void keyPressed(KeyEvent a) {
        int keyCode = a.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT) {
            shipx -= 100;
            f.dispose();
            new Game();
       
        }
        if (keyCode == KeyEvent.VK_RIGHT) {
            shipx += 100;
       
            f.dispose();
            new Game();   
        }
    }


Definitely not the way to do this. You will be creating an a Game instance inside a game instance (inside a game instance inside a ...) until you hit a stack overflow error.

~

I suggest moving some of the code in your constructor into separate class member function(s).

~

code:
JLabel[] hearts = new JLabel[5];//change to variable for health

You can't access this array outside of the constructor, so you will not be able to update the health "bar".

~

code:
static int shipx = 100;

I'm guessing you were forced to make that static as you kept making new Game instances... Ask yourself this: If two separate games/instances were running at once, would they share the variable? If not, it should not be static.
Blockgen




PostPosted: Mon Jun 03, 2013 11:38 pm   Post subject: Re: How to update constructor?

andrew. wrote:
I'm a bit rusty with Java but I think what he means is to just update the contents of your JLabel to reflect your new values and that update will bubble up to the JFrame to redraw itself.
Exactly, what I want to do. I'm just asking if I have to do it trough paintComponent(Graphics G) or if there is another way.

Zren wrote:
Definitely not the way to do this.
Yea, that was just me brute forcing the problem.

Zren wrote:
I suggest moving some of the code in your constructor into separate class member function(s).
Can you please possibly link to an example of this. I know what you mean, but I don't know the exact syntax.

Zren wrote:
You can't access this array outside of the constructor, so you will not be able to update the health "bar".
I'm going to be changing that five to a variable and also the one in the for loop, so that it would change. the comment is just a reminder for myself.


The reason I don't want to use paint, is because I don't think you can layer things in it. That's why I'm using JLayeredPane. If you there is a way, I would be more than happy to learn it.
Sponsor
Sponsor
Sponsor
sponsor
Blockgen




PostPosted: Tue Jun 04, 2013 1:57 pm   Post subject: Re: How to update constructor?

I got it!
I had to use ship.setLocation(shipx,370); to update the icon.

Thanks to everyone who tried to help.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: