1

Im trying to add an object of the RedSquare class in to the JFrame in CatchMeV2 class. What is the problem?

public class CatchMeV2 implements ActionListener{
int width = 400;
int height = 450;

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setSize(400, 400);
    frame.setTitle("CatchMe.V2");
    RedSquare r = new RedSquare();
    frame.add(r);

}

@Override
public void actionPerformed(ActionEvent e) {

    }
}

public class RedSquare extends JPanel implements ActionListener {
int x = 20; int y = 20;
int velX = 4; int velY = 4;
public RedSquare(){
    addActionListener(this);
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.green);
    g.fillRect(x, y, 50, 50);
    repaint();
}

@Override
public void actionPerformed(ActionEvent e) {
    x += velX;
    y += velY;
    if (x < 0) {
        velX = 0;
        x = 0;
    }
    if (x > 400 - 50) {
        velX = 0;
        x = 400 - 50;
    }
    if (y < 0) {
        velY = 0;
        y = 0;
    }
    if (y > 400 - 40) {
        velY = 0;
        y = 400 - 40;
    }
    repaint();
    }
}

The actionPerformed method doesn't do anything. Can anyone help? Or is there an easy way to do this? Background: I was trying to make a game by using one class. I did it but the problem was i could only take 1 key input at a time and it was lagging. And my teacher said that if I divided it into different classes it wouldn't lag. Is it true?

Mert Karakas
  • 178
  • 1
  • 4
  • 22

1 Answers1

3

You cannot add non visual component to JPanel so you need to extend RedSquare class from component, for example JPanel and override paintComponent() method.

public class CatchMeV2 {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setTitle("CatchMe.V2");
        RedSquare r = new RedSquare();
        frame.setContentPane(r);
        frame.setVisible(true);
    }
}

class RedSquare extends JPanel implements ActionListener {

    public RedSquare() {

    }

    @Override    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // don't forget to call super method
        g.setColor(Color.green);
        g.fillRect(20, 20, 50, 50);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

update>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

you can't use action performed for panel it's only for buttons or like that.if you want to do something with click on panel then you need to use implement mouselistner .and put action code inside mouseclick method .run this example

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class CatchMeV2 {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(400, 400);
        frame.setTitle("CatchMe.V2");
        RedSquare r = new RedSquare();
        frame.add(r);
    }
}

class RedSquare extends JPanel implements MouseListener {

    int x = 20;
    int y = 20;
    int velX = 4;
    int velY = 4;

    public RedSquare() {
        addMouseListener(this);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
        g.fillRect(x, y, 50, 50);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("hi");
        x += velX;
        y += velY;
        if (x < 0) {
            velX = 0;
            x = 0;
        }
        if (x > 400 - 50) {
            velX = 0;
            x = 400 - 50;
        }
        if (y < 0) {
            velY = 0;
            y = 0;
        }
        if (y > 400 - 40) {
            velY = 0;
            y = 400 - 40;
        }
        this.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}


}

at that time you only want to move square with mouse click so u can still use awt mouse event but when you use keys you have to use key binding

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • Thank you. Would it work if RedSquare extended JFrame. – Mert Karakas Dec 24 '14 at 15:01
  • @MertKarakas yes but then you canot add it to a panel but you can create.but better thing is to use panel for this not jframe.don't overide jframe paintcomponent method overide panel method instead – Madhawa Priyashantha Dec 24 '14 at 15:05
  • 1+ and please check my edits (hope those are helpful). Also please note `paintComponent()` is protected not public. See [this Q&A](http://stackoverflow.com/questions/21338125/is-it-good-practice-override-methods-with-a-higher-visibility) for a discussion on this matter. – dic19 Dec 24 '14 at 15:08
  • Thanks to all. @dic19 I have another problem. I wrote a simple code into the actionPerfomed method but it doesnt work? Why? – Mert Karakas Dec 25 '14 at 14:43
  • Silly question i need to add an actionListener sorry! – Mert Karakas Dec 25 '14 at 14:47
  • @FastSnail It's not an error. I guess it's something i missed. I added a simple code to the actionPerformed method of RedSquare. But it doesnt act. Can you tell me why? – Mert Karakas Dec 25 '14 at 17:53
  • @MertKarakas could you show your code.you can post your code in http://pastebin.com/ and give the link – Madhawa Priyashantha Dec 25 '14 at 17:56
  • @FastSnail I edited the question. The code is really short at the moment. – Mert Karakas Dec 25 '14 at 18:00
  • @MertKarakas but what is x velX ?without seeing it i cannot guess.and i don't see you have improved codes .i only see the codes with errors. – Madhawa Priyashantha Dec 25 '14 at 18:03
  • @FastSnail Sorry i forgot to add them. They are just integers. – Mert Karakas Dec 25 '14 at 18:05
  • @MertKarakas do you trying to move rectangle with keys?or what? – Madhawa Priyashantha Dec 25 '14 at 18:08
  • @FastSnail At the moment Im trying to move the square. Only move. For that i add velX and velY to the squares x,y components. If I can make it move. I'll add the key inputs. – Mert Karakas Dec 25 '14 at 18:13
  • @MertKarakas see my updated answer but I'm little disappointed because still see mistakes in your updated code still has `paint` not `paintcomponent` and `static void paint` it's not `static` .carefully look at codes and try to run my updated code – Madhawa Priyashantha Dec 25 '14 at 19:10
  • @FastSnail Ive edited the code but i can't add an actionlistener why is it? – Mert Karakas Dec 26 '14 at 12:13
  • @MertKarakas because action not performed when you click on a panel .action perform event work with jbutton and etc.if you want to do something when click on jpanel you need to use mouselistner instead of actionlistner.if you run my updated code you will see it's working..why your code not work:because you have not set mouselistner – Madhawa Priyashantha Dec 26 '14 at 12:28
  • @FastSnail Oh I understand that. But I'll use a KeyListener instead. Thank you! – Mert Karakas Dec 26 '14 at 16:49
  • @MertKarakas ok but keep in mind awt key event will not work you need to use key blind – Madhawa Priyashantha Dec 26 '14 at 16:54
  • @FastSnail What do you mean by key blind? – Mert Karakas Dec 26 '14 at 17:49