Ask Experts Questions for FREE Help !
Ask
    Adze2012's Avatar
    Adze2012 Posts: 1, Reputation: 1
    New Member
     
    #1

    May 4, 2012, 12:25 AM
    Label movement
    My class is making a simple 2D Mario-style for our PAT. One of the problems I have is to make a label appear and disappear. Now that doesn't exactly sound too difficult, but this label has to move around along with a pre-created label that we can already move. So basically if you press Space (fire), a label pops up saying "OOO". But this label needs to follow our other label around.

    Here is what we have so far:

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

    public class pat extends JFrame {
    boolean up, down, left, right, fire;
    JLabel sg;
    JButton b;

    // Main constructor
    public pat() {
    setSize(1000, 1000);
    setLayout(null);
    sg = new JLabel("SG");
    sg.setSize(20, 20);
    sg.setLocation(100, 700);
    sg.setVisible(true);
    add(sg);

    this.setVisible(true);
    this.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_UP: {
    up = true;
    break;
    }

    case KeyEvent.VK_LEFT: {
    left = true;
    break;
    }

    case KeyEvent.VK_RIGHT: {
    right = true;
    break;
    }

    case KeyEvent.VK_DOWN: {
    down = true;
    break;
    }

    case KeyEvent.VK_SPACE: {
    fire = true;
    break;
    }

    case KeyEvent.VK_ESCAPE: {
    // Exit
    System.exit(0);
    }
    }
    }

    public void keyReleased(KeyEvent e) {
    // Upon releasing key, stop direction
    switch (e.getKeyCode()) {
    case KeyEvent.VK_UP: {
    up = false;
    break;
    }

    case KeyEvent.VK_LEFT: {
    left = false;
    break;
    }

    case KeyEvent.VK_RIGHT: {
    right = false;
    break;
    }

    case KeyEvent.VK_DOWN: {
    down = false;
    break;
    }
    }
    }
    });
    }

    public static void main(String[] args) {
    // create frame
    pat test = new pat();
    test.setLocationRelativeTo(null);
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    test.setVisible(true);
    test.loop();
    }

    public void loop() {
    Timer timer = new Timer(1, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (up)
    sg.setLocation(sg.getX(), sg.getY() - 30);
    if (left)
    sg.setLocation(sg.getX() - 30, sg.getY());
    if (right)
    sg.setLocation(sg.getX() + 30, sg.getY());
    if (down)
    sg.setLocation(sg.getX(), sg.getY() + 30);

    }
    });

    timer.start();
    }

    public void fire() {

    if (fire = true)

    {
    JLabel ooo;
    ooo = new JLabel("OOO");
    ooo.setSize(20, 20);
    ooo.setVisible(true);
    add(ooo);
    this.setVisible(true);
    this.addKeyListener(new KeyAdapter() {
    });
    }
    }
    }



    I would appreciate any help. Thank you

Check out some similar questions!

Blood creatinine label [ 2 Answers ]

How can one reduce his blood creatinine label

No Label, what are we then? [ 11 Answers ]

I started seeing this guy at the beginning of January. He has been a friend for about 6 months and used to come and hang out with me and my ex. He also used to work with my ex. I am fresh out of the relationship with my ex, but me and this guy have been seeing each other regularly (3-4 times at...


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.