0

Let me be honest, I'm not really know what I'm doing. I just moved from python to Java, and i'm still trying to get used to all the classes and the types things.

I decide to make a break with java concepts tutorials and start to get my hands dirty. According to my understanding, I'm using swing to paint a ball on the screen and make it move.

I tried to design a ball object that handle the ball position and the screen bumping, but the ball doesn't moved at all. When I turn on the debug I noticed that the paint() function get called only at creation, but not get called with repaint().

I got a feeling that I'm using a bad tutorial to do this stuff, its look like there is a better way to do it.

Anyway, I will be glad to hear what you guys thinking.

Edit: After I saw your comments I notice that paint actually get called when I put sysout there. Its seems that the debugger doesn't jump to there before I put sysout in paint(). My guess is that I'm not really changing the position of the ball.

@SuppressWarnings("serial")
public class Tennis extends JPanel {
    Ball ball = new Ball(50,50);

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        int[] position = ball.getPosition();
        g2d.fillOval(position[0],position[1], 30, 30);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Mini Tennis");

        Tennis game = new Tennis();
        frame.add(game);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            // just change the position and check for bump
            game.ball.move(game.getHeight(), game.getWidth());
            game.repaint();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
Smiled_One
  • 445
  • 1
  • 4
  • 17
  • use super.paintComponent(g) instead of super.paint(g). Then everytime you want to change the graphics, use repaint() – Aditya Ramkumar Jul 15 '15 at 18:50
  • `repaint()` is being called ~100 times a second, right? Nothing funky going on in the loop? – Anubian Noob Jul 15 '15 at 18:52
  • 2
    The actual method should be `paintComponent()` rather than `paint()`. – RealSkeptic Jul 15 '15 at 18:52
  • *"I got a feeling that I'm using a bad tutorial to do this stuff.."* Link to it. By the looks of the code that resulted from it, the tutorial is crap. – Andrew Thompson Jul 15 '15 at 18:57
  • The code you posted doesn't compile. `When I turn on the debug I noticed that the paint() function get called only at creation,` - works fine for me. I added a System.out.println(...) statement to the painting method and it is continually displayed. If you need more help post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Jul 15 '15 at 18:58
  • possible duplicate of [Java graphics isn't repainting](http://stackoverflow.com/questions/16968625/java-graphics-isnt-repainting) – resueman Jul 15 '15 at 19:07
  • @camickr you are right. The debbuger didn't get into paint() for some reason, but it's seems to get inside and work when I put `sysout` in there. I updated the main question. – Smiled_One Jul 15 '15 at 19:11
  • `My guess is that I'm not really changing the position of the ball.` - probably, but as I stated you code doesn't compile because you didn't include the Ball class, so we have no idea what you move() method is really doing. So again add debug code to see the position of the ball each time you try to paint it. Then debug the code to determine why it is the same. – camickr Jul 15 '15 at 19:17
  • I found the problem, i'ts seems like I mess up with the class while trying to implement abstract class to. Do I need to open a new question about it or just edit this one? – Smiled_One Jul 15 '15 at 19:19

1 Answers1

1

Change paint() to paintComponent(), for an explanation of the differences therein see this.

@Override
public void paintComponent(Graphics g){ //CHANGE HERE
    super.paintComponent(g);            //AND HERE
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
    int[] position = ball.getPosition();
    g2d.fillOval(position[0],position[1], 30, 30);
}
Community
  • 1
  • 1
LemenDrop
  • 160
  • 1
  • 3
  • 11