#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Problem with repaint()


    Code:
    package bouncetest;
    
    
    public class BounceTest {
    
        
        public static void main(String[] args) {
            new Frame().setVisible(true);
        }
    Code:
    package bouncetest;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Ball {
        double x=195, y=195;
        int WIDTH=10, HEIGHT=10;
        public Ball(){
            System.out.println("Ball created");
        }
        public void drawBall(Graphics g){
            g.setColor(Color.BLUE);
            g.drawOval((int) x, (int) y, WIDTH, HEIGHT);
            g.fillOval((int)x, (int) y, WIDTH, HEIGHT);
        }
    }
    Code:
    package bouncetest;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Platform {
        double x, y, a = 1.1;
        int WIDTH=40, HEIGHT=10;
        public Platform(){
            System.out.println("Platform has been created");
            x = 195 + 125*Math.cos(a);
            y = 195 + 125*Math.sin(a);
        }
        
        public void Draw(Graphics g){
            g.setColor(Color.red);
            g.drawRect((int)x, (int)y, WIDTH, HEIGHT);
            g.fillRect((int)x, (int)y, WIDTH, HEIGHT);
            
        }
    }
    Code:
    package bouncetest;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.MouseInfo;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Frame extends JFrame implements MouseListener, KeyListener{
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        Ball b = new Ball();
        Platform p = new Platform();
        int ovalX = (400-250)/2, ovalY = (400-250)/2;
        JPanel game = new JPanel(){
            
            @Override
            public void paint(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.red);
                b.drawBall(g);
                g.drawOval(ovalX, ovalY, 250, 250);
                p.Draw(g);
                g.dispose();
                System.out.println("Painted\n" + p.a);
            }
    };
        
        
        public Frame(){
            super("Bounce");
            setSize(400,400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocation((d.width-getWidth())/2,(d.height-getHeight())/2);
            setContentPane(game);
            game.setBackground(Color.black);
            game.addMouseListener(this);
            setResizable(false);
            addKeyListener(this);
            
        }
        
        @Override
        public void mouseClicked(MouseEvent e) {
            b.x = MouseInfo.getPointerInfo().getLocation().x;
            b.y = MouseInfo.getPointerInfo().getLocation().y;
            game.repaint();
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
    //        b.x = MouseInfo.getPointerInfo().getLocation().x;
    //        b.y = MouseInfo.getPointerInfo().getLocation().y;
    //        repaint();
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            p.a += .1;
            game.repaint();
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
        }
        
    }
    Alright, so the problem is that when I call repaint(), it doesn't do anything (by that I mean the screen isn't updated, the first time it runs it does paint though). I put System.out.print's in places to verify that it is repainting, as well as the value for a is increasing. I can't find what is wrong now, and have searched over the internet and haven't found any problem like mine. Thanks for any help.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What is supposed to happen when the code is executed. I get this printed when I execute and click the mouse:

    Ball created
    Platform has been created
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    g.repaint
    Painted p.a=1.1
    I added the g.paint message and change what was printed in the paintComponent method. It shows that repaint was called and that the paintComponent method was called.
    Please explain what problem you are having.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    What is supposed to happen when the code is executed. I get this printed when I execute and click the mouse:


    I added the g.paint message and change what was printed in the paintComponent method. It shows that repaint was called and that the paintComponent method was called.
    Please explain what problem you are having.
    The problem is that is doesn't do anything when I press a key. I press a key, and p.a adds .1. In the console you will see it start adding. The only problem is that the rectangle doesn't move. If you change the p.a value starting the program, it will however, be in a different location.

    Not only that though. When you click in a certain location, the ball is suppose to go to that location. All it does it disappear though.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    When I press a key I get this:
    Running: java MyFrame

    Ball created
    Platform has been created
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.1
    g.repaint p.a=1.2000000000000002 >>>>>>>>>> key press calls repaint
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.2000000000000002 <<<<<<<<<<<< paintComponent
    g.repaint p.a=1.3000000000000003
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.3000000000000003
    g.repaint p.a=1.4000000000000004
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.4000000000000004
    g.repaint p.a=1.5000000000000004
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.5000000000000004
    g.repaint p.a=1.6000000000000005
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.6000000000000005
    g.repaint p.a=1.7000000000000006
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.7000000000000006
    g.repaint p.a=1.8000000000000007
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.8000000000000007
    g.repaint p.a=1.9000000000000008
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=1.9000000000000008
    g.repaint p.a=2.000000000000001
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.000000000000001
    g.repaint p.a=2.100000000000001
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.100000000000001
    g.repaint p.a=2.200000000000001
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.200000000000001
    g.repaint p.a=2.300000000000001
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.300000000000001
    g.repaint p.a=2.4000000000000012
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.4000000000000012
    g.repaint p.a=2.5000000000000013
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.5000000000000013
    g.repaint p.a=2.6000000000000014
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.6000000000000014
    g.repaint p.a=2.7000000000000015
    Ball.drawBall x=195.0, y=195.0
    Painted p.a=2.7000000000000015
    The print out shows that repaint was called and then paintComponent was called.

    the rectangle doesn't move.
    There are no printlns in the Platform Draw method to show it is called and what it does.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    When I press a key I get this:
    The print out shows that repaint was called and then paintComponent was called.


    There are no printlns in the Platform Draw method to show it is called and what it does.
    Wow I feel like an idiot. I got it to move. The problem I was having is that the x and y for the platform were changed in the constructor, but never changed in the draw method. Once I added those it started working fine. I hate those types of mistakes.

    Either way, the ball doesn't move when I click still though.
    Thanks for your help so far.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Look at the API doc for the getLocation method.
    Print out the values of x and y. Do some mouse clicks, Then move the window and do it again
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Look at the API doc for the getLocation method.
    Print out the values of x and y. Do some mouse clicks, Then move the window and do it again

    AHHH, thanks. I never would have gotten that on my own. I guess I should start putting printlns everywhere so I know what is going on. So we got them both working, thanks for the help.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    putting printlns everywhere
    That's a tried and true technique I've used for years. Interactive debuggers are good for some things depending on what you're working on.

IMN logo majestic logo threadwatch logo seochat tools logo