-1

I am new to java and programming in general but i have been having fun with java swings as of late.I built this simple program to practice what i have learnt so far.All I want is for the label (black box) to jump when presssed space, and then come back to the ground.I tried my best but couldnt find anything that was super helpful and understandable. Please help.

This is my "Main" Class:

package jump;

public class Main {

    public static void main(String[] args) {

        MyFrame frame=new MyFrame();
        
    }

}

And this is my "MyFrame" Class where i wrote the main code:

package jump;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyFrame extends JFrame implements KeyListener {
    
    JPanel panel1;
    JLabel label;
    
    MyFrame(){
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        
        panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(800, 500));
        panel1.setBackground(Color.cyan);
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT,10,468)); 
        this.add(panel1, BorderLayout.NORTH);
        
        
        
        label = new JLabel();
        label.setPreferredSize(new Dimension(30, 30));
        label.setBackground(Color.black);
        label.setOpaque(true);
        this.addKeyListener(this);
        panel1.add(label);
        
        JPanel panel2=new JPanel();
        panel2.setPreferredSize(new Dimension(800,310));
        panel2.setBackground(Color.green);
        this.add(panel2,BorderLayout.SOUTH);
        
        
        this.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
        
    
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                if (label.getY() > 0) {
                    label.setLocation(label.getX(), label.getY() - 10);
                }
                break;
            case KeyEvent.VK_LEFT:
                if (label.getX() > 0) {
                    label.setLocation(label.getX() - 10, label.getY());
                }
                break;
            case KeyEvent.VK_DOWN:
                if (label.getY() <= this.getY() + 460) {
                    label.setLocation(label.getX(), label.getY() + 10);
                }
                break;
            case KeyEvent.VK_RIGHT:
                if (label.getX() <= this.getX() + 745) {
                    label.setLocation(label.getX() + 10, label.getY());
                }
                break;
           
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {

        System.out.println(e.getKeyChar());
        System.out.println(e.getKeyCode());
        
        
        
    }
    
    
}

I saw some code that used the "thread.sleep()" method,but i didn't understand its implementation.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
NewToJava
  • 9
  • 1
  • Your code has lots of issues, and so I will enumarate some: 1. Don't extend JFrame but rather extend JPanel and do your animation in the JPanel. 2. Much better if your sprites (the things that move in your animation) are light-weight, meaning, don't "jump" a JLabel, but rather have an image sprite jump. 3. Best to do all your drawing in the paintComponent method of a JLabel -- google tutorial on how to draw with Swing. – Hovercraft Full Of Eels Aug 25 '23 at 04:10
  • 4. You need an animation loop to drive the animation, and with Swing it is simplest and best to use a "Swing Timer". Google the standard tutorial and study it. 5. Don't use KeyListeners for this but rather use Key Bindings, which are a higher-level construct, and thus somewhat safer to use. Again, Google the tutorial on this. – Hovercraft Full Of Eels Aug 25 '23 at 04:10
  • Some links: 1) [Swing Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/index.html) 2) [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). 3) [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) 4) [Key Bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – Hovercraft Full Of Eels Aug 25 '23 at 04:11
  • 6. Don't use `Thread.sleep` in Swing GUI's unless you first fully understand Swing threading issues and know how to avoid them. For details, see, [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). Again, use the Swing Timer. – Hovercraft Full Of Eels Aug 25 '23 at 04:12
  • 1
    You appear to have taken a large bite out of a complex programming issue, and so please be careful not to choke. You might be better off learning some basics of the Swing GUI library and basic Java programming first. – Hovercraft Full Of Eels Aug 25 '23 at 04:14
  • 1
    7. Your code appears to be completely ignoring the affect that layout managers have on component placement within their containers: You're trying to move a JLabel that is held by a JPanel that uses a FlowLayout, and this won't work. This of course means one more tutorial link: [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – Hovercraft Full Of Eels Aug 25 '23 at 04:17
  • 1
    Wow,Had no idea the code was lacking so much.I will look through your advice and keep updating my progress.Thanks. – NewToJava Aug 25 '23 at 04:48
  • Correction. This: "*Best to do all your drawing in the paintComponent method of a JLabel*" should instead read: "*Best to do all your drawing in the paintComponent method of a **JPanel***" – Hovercraft Full Of Eels Aug 25 '23 at 14:32

0 Answers0