Adze2012
May 4, 2012, 12:25 AM
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
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