Problem z ustawieniem KeyListenera, tak, żeby można było dzięki niemu przesuwać obiekt.

0

Nie wiem, jak ustawić KeyListener, żeby obsługiwał ruch topazu. Wydaje mi się, że powinienem ustawić addKeyListener na panelAnimacji ale nie działa.


package anim;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class Anim extends JFrame  {
     public static Image topaz = new ImageIcon("Topaz.jpg").getImage();

     PanelAnimacji panelAnimacji = new PanelAnimacji();
     

    Anim(){
        this.setTitle("Ruch, animacja");
        this.setBounds(100, 100, 500, 300);
        
        panelAnimacji.setBackground(Color.GRAY);
        this.getContentPane().add(panelAnimacji);
            
        
        this.setDefaultCloseOperation(3);
    }
    
    
    class PanelAnimacji extends JPanel implements ActionListener, KeyListener{
                       
          
         Timer tm = new Timer(2,this);
 
         int x = 0, y = 0, dx = 0, dy = 0;
         
       
         public void paintComponent(Graphics g)
        {  
            tm.start();

             addKeyListener(this);
             setFocusable(true);
            
            super.paintComponent(g);
            this.setBackground(Color.GRAY);
            
            g.drawImage(topaz, x, y, null);
            
        } 

        @Override
        public void actionPerformed(ActionEvent e) {
        
           x += dx;
           y += dy;
           repaint();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            System.out.println("Key typed");
 
        }

        @Override
        public void keyPressed(KeyEvent e) {
             if(e.getKeyCode() == KeyEvent.VK_LEFT){
                dx = -10;
                System.out.println("Lewo");
            }
             if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                dx = 10;
                System.out.println("Prawo");
            }
             if(e.getKeyCode() == KeyEvent.VK_UP){
                dy = -10;
                System.out.println("Góra");
            }
             if(e.getKeyCode() == KeyEvent.VK_DOWN){
                dy = 10;
                System.out.println("Dół");
            }
             else System.out.println("Coś innego");
        }

        @Override
        public void keyReleased(KeyEvent e) {
            dx = 0;
            dy = 0;
            System.out.println("Key released");
        }
     }

     public static void main(String[] args) {
        new Anim().setVisible(true);
     }  
    
}
1

Ja bym zaczął od nie robienia addKeyListener(this); za każdym razem jak malujesz komponent... Analogicznie ze startowanie timera. To się robi w konstruktorze, raz!
Reszta to debugger. Postaw breakpointy i zobacz co sie dzieje.

0

Dzięki, rzeczywiście problem rozwiązało przeniesienie addKeyListener(this); oraz setFocusable(true); do konstruktora. Dodatkowo przeniosłem też timer.

...
PanelAnimacji(){
            tm.start();
            addKeyListener(this);
            setFocusable(true);
    }
       
         public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            this.setBackground(Color.GRAY);
            g.drawImage(topaz, x, y, null);
        } 
...

1 użytkowników online, w tym zalogowanych: 0, gości: 1