0

I'm starting to paint squares in java and I use key listeners to move them. The only problem I seem to be having is, smooth transitions between moving from one direction to another. What I mean by this is, for example, when I move right then change my direction to left it stops for a second then starts moving again, but to the left. I'm trying to get a smooth transition to where when i switch from right to left it will instantly start going that direction instead of stopping, then going. Any help would be nice, thanks in advance :) !

My main frame class:

package mainpackages;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class Frame extends JFrame implements KeyListener{
    Screen s;
    public static void main(String[] args) {
     new Frame();
    }

    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setTitle("Drawing practice");
        setResizable(false);
        setLocationRelativeTo(null);
        addKeyListener(this); 
        init(); 
    }
    public void init() {
         s = new Screen();
         add(s);
        setVisible(true);
    }

    @Override
    public void keyPressed(KeyEvent e) {  

        if(e.getKeyCode() == e.VK_UP) {
            s.y -= s.deltay; 

        }
        if(e.getKeyCode() == e.VK_DOWN) {
            s.y += s.deltay;

        }
        if(e.getKeyCode() == e.VK_LEFT) {
            s.x -= s.deltax;

        }
        if(e.getKeyCode() == e.VK_RIGHT) {
            s.x += s.deltax;

        }
        repaint();

    }

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

    }

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

    }



}

And my Screen class, where I paint the square to the JPanel

package mainpackages;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Screen extends JPanel {
    public static int x = 100;
    public static int y = 100;
    public static int deltax = 5;
    public static int deltay = 5;
    public Screen() {


    }


    public void paint(Graphics g) { 
        g.setColor(Color.RED);
        g.fillRect(x, y, 50, 50); 
    }



}
  • 1
    As for changing direction -- change your program logic. Your code is only doing what you tell it to do, and presently it's changing delta variables on key press. Instead have it change *direction* on key press, and then use a Swing Timer to drive the animation. – Hovercraft Full Of Eels Apr 19 '18 at 21:58
  • Suggestion: Don't use `KeyListener`, use the [Key Bindings API](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). Use them to set a state flag which determines the direction of movement (instead of updating the delta directly). Use something like a Swing `Timer` to inspect the state flag and modify the movement delta and schedule repaints. Override `paintComponent` instead of `paint` and make sure you call `super.paintComponent` before performing any custom painting – MadProgrammer Apr 19 '18 at 22:18
  • Also, `static` is not your friend. The `Screen` and `Frame` (can't tell you how crappy that name is) should be sharing a "model" which provides the state information needed – MadProgrammer Apr 19 '18 at 22:19
  • Why don't you answer in answers, why do you answer in comments? Yes, both of you HFOE and MP. – user unknown Apr 19 '18 at 22:28
  • @MadProgrammer do you work for this site? because i've seen you everywhere for the past 2 years – andrew lopez Apr 19 '18 at 22:51
  • @andrewlopez Dear lord, I wish – MadProgrammer Apr 19 '18 at 22:53
  • 1
    @userunknown Primarily because the question has already been answered, many times before. Providing a copy-and-paste answer to the user isn't going to provide them with the skills they need to further answer their own questions in the future without resorting to posting on SO first. Personally, I don't need the rep from this kind of question, which I've already answered in a number of different ways - as evident from my choice of duplicate. SO is not a replacement for good tutorials and research – MadProgrammer Apr 19 '18 at 22:56

0 Answers0