0

I am a beginner at programming and I'm working on a simple game. I wanted to make a timer between each position of a JLabel so it would look animated though I can't figure out how the timer method works. Thanks.

Movement class:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class movement 
{
    private JComponent jt;
    private InputMap ip;
    private ActionMap ap;
    private String comm;
    private KeyStroke key;
    private int movement;

    public movement(JComponent jt, InputMap ip,ActionMap ap, String comm,KeyStroke key,int movement)
    {
        this.jt = jt;
        this.ip = ip;
        this.ap= ap;
        this.comm = comm;
        this.key = key;
        this.movement = movement;
    }
    public void newAction()
    {
        this.ip  = this.jt.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        this.ip.put(key, comm);
        this.ap = this.jt.getActionMap();
        this.ap.put(this.comm, new AbstractAction() 
        {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                int originaly = jt.getY();
                if(key ==  KeyStroke.getKeyStroke(KeyEvent.VK_UP,0))
                {

                    Timer t = new Timer(100, this);
                    t.setRepeats(false);
                    jt.setBounds(jt.getX(),jt.getY()+movement , 50, 50);
                    t.start();

                    if(originaly<=jt.getY()+50)
                    {
                        t.stop();
                    }

                }
                else
                {
                    jt.setBounds(jt.getX()+movement,jt.getY() , 50, 50);
                }
            }
        });

    }
}

Over here is my main method. I encountered some issues with it before but I think its OK now and that's not the problem.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.beans.PropertyChangeListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.management.openmbean.KeyAlreadyExistsException;
import javax.swing.*;

import java.util.concurrent.TimeUnit;
public class Malario 
{

    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("malario");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        frame.setSize(700, 700);
        JPanel panel = new JPanel();

        panel.setLayout(null);
        panel.setBackground(Color.blue);
        JLabel malario = new JLabel("Malario");
        malario.setOpaque(true);
        malario.setBackground(Color.green);
        panel.add(malario);

        malario.setBounds(100, 550, 50, 50);

        JLabel platform = new JLabel();
        platform.setOpaque(true);
        platform.setBounds(0,600,700,50);
        panel.add(platform);
        frame.setContentPane(panel);
        final int originalx = 100;
        final int originaly = 550;
        int currentlocx = originalx;
        int currentlocy = originaly;

        movement moveRight = new movement(malario, new InputMap(), new ActionMap(), "move Right",KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0) , 10);
        movement moveLeft = new movement(malario, new InputMap(), new ActionMap(), "move Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0), -10);
        movement jumpUp = new movement(malario, new InputMap(), new ActionMap(), "move Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), -10);
        moveRight.newAction();
        moveLeft.newAction();
        jumpUp.newAction();
    }
}
Robo Mop
  • 3,485
  • 1
  • 10
  • 23
  • this post may help https://stackoverflow.com/questions/8088002/how-to-use-a-swing-timer-to-start-stop-animation?rq=1 – Minh Kieu Jun 04 '17 at 18:02
  • Your welcome for the help in your original question. Check the edit for a working example. – camickr Jun 04 '17 at 18:46
  • [For example](https://stackoverflow.com/questions/16493809/how-to-make-sprite-jump-in-java/16494178#16494178) – MadProgrammer Jun 04 '17 at 22:44
  • @David Schiff Will you be willing to create a class extending `JPanel`, which you can add to the JFrame? Instead of creating an instance of `JPanel` in the `main` method. – Robo Mop Jun 05 '17 at 06:40
  • @CoffeehouseCoder I am willing to... sorry im really new to this, just learned some basics in school. Can you explain just exactly what this will do and what the advantages are? – David Schiff Jun 09 '17 at 08:13
  • @DavidSchiff Well, see, I am not familiar with InputMaps and ActionMaps. I could post some code with my technique and explain it, but it will be without these. I hope that's fine :) – Robo Mop Jun 09 '17 at 08:20
  • @CoffeehouseCoder sure my friend , I am happy to learn new techniques – David Schiff Jun 09 '17 at 08:33

0 Answers0