0

I need to make an animation with a part controlled with arrows. I created JPanel which draws the "background" simply with timer and repaint(). Then I'm trying to add a canvas with key listener.

public class MyPanel extends JPanel implements ActionListener{
      MyPanel(){
           ... creating other objects ...
        MyCanvas canv = new MyCanvas();
        this.add(canv);
        Timer timer = new Timer(30, this); 

      }

     ...actionPerformed and other functions for background animation...
 }

 public class MyCanvas extends Canvas implements ActionListener, KeyListener{
      int rX;
      int rY;
      Color color;
      KeyEvent e;
      int code;
      Timer timer;

      MyCanvas() {      
        rX = 400;
        rY = 400;
        color=Color.red;
        this.setSize(1220, 840);

        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

        timer = new Timer(5, this);
        timer.start();
      }

      public void KeyPressed(KeyEvent e){
         code = e.getKeyCode();
      }

      public void paint (Graphics g)
      {
          g.setColor(color);
          g.fillOval(this.rX, this.rY, 30, 30); 
      }

      public void actionPerformed(KeyEvent evt) {
          int keyCode = evt.getKeyCode();
          if(keyCode == KeyEvent.VK_LEFT){
               rX-=2;
      }
         ...and so on...

      public void keyPressed(KeyEvent e) {}
      public void keyReleased(KeyEvent e) {}
      public void keyTyped(KeyEvent e) {}

      public void actionPerformed(ActionEvent e) {
           repaint();
      }
 }

The background animation works and moves just fine, but canvas is added to JPanel, it completely covers it up. Also the key control doesn't work at all. How to fix it?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
qwert
  • 1
  • 3
  • 1
    Don't mix AWT classes with Swing classes. Why are you using a `Canvas` at all? – RealSkeptic May 07 '19 at 18:05
  • I thought it wouldn't cover the background up. I don't know what else I could use. I don't know if (or how) I can do it just in one JPanel? – qwert May 07 '19 at 18:11
  • @RealSkeptic I tried doing the same with another JPanel added to the main JFrame and it still covers the rest up. I'm lost. – qwert May 08 '19 at 06:16
  • Do it I. The same panel. One paintComponrnt. – RealSkeptic May 08 '19 at 06:18
  • For better help sooner post a proper [mcve]. Don't use a `KeyListener` use `KeyBinding`s instead, look at [This answer](https://stackoverflow.com/questions/40944066/how-to-set-a-shortcut-key-for-jradiobutton-without-modifiers/40944215#40944215) for the keybinding part. Don't mix AWT and Swing components, `Canvas` belongs to AWT and is deprecated. Don't override `paint(...)` instead override `paintComponent(...)` and call `super.paintComponent(...)` as the first line in it, otherwise you're breaking the paint chain. – Frakcool May 08 '19 at 21:39
  • For your particular problem, create a single `JPanel` and inside the `paintComponent` method draw both the background and then the object to draw, or have a `List` of objects that need to be drawn and iterate over it and draw them in the `JPanel`, for [example](https://stackoverflow.com/a/41703107/2180785), [example](https://stackoverflow.com/a/47720180/2180785), [example](https://stackoverflow.com/a/48812636/2180785) and [example](https://stackoverflow.com/a/41944799/2180785). If this still doesn't solve your problem, post a proper MCVE that we can copy-paste-compile-run – Frakcool May 08 '19 at 21:48

0 Answers0