1

when i run it, the repaint doesn't work. The frame opens and stays for 2000ms and then closes. else if i remove the System.exit(0); statement, it doesn't closes but the painted ball still doesn't shows up.

BallWorld.java

import java.awt.*;
import javax.swing.JFrame;

public class BallWorld extends Frame {

public static void main(String[] args) {

    BallWorld bw1 = new BallWorld(Color.red);
    bw1.show();


}

public static final int framewidth = 600;
public static final int frameheight = 400;

private Ball aball;
private int counter = 0;

private BallWorld(Color ballColor){

    setSize(framewidth,frameheight);
    setTitle("Ball World");

    aball = new Ball(10, 15, 5);
    aball.setColor(ballColor);
    aball.setMotion(3.0, 6.0);

}


public void paint(Graphics g){

    aball.paint1(g);

    aball.move();


    if((aball.x() < 0) || (aball.x() > framewidth))

        aball.setMotion(-aball.xMotion(), aball.yMotion());

    if((aball.yMotion() < 0) || (aball.yMotion() > frameheight))

        aball.setMotion(aball.xMotion(),-aball.yMotion());

    //redraw  the frame

    counter = counter + 1;

    if(counter < 2000)
    repaint();
    else
        System.exit(0); 
}   
}

the other class is

Ball.java

import java.awt.*;
import java.awt.Rectangle;

public class Ball {

protected Rectangle location;
protected double dx,dy;
protected Color color;



public Ball(int x,int y, int r){

    location =new Rectangle(x-r, y-r, 2*r, 2*r);

    dx=0;   //initially no motion
    dy=0;
    color = Color.blue;
}

// Setters
public void  setColor(Color newColor){
    color = newColor;
}

public void setMotion(double new_dx, double new_dy){
    dx = new_dx;
    dy = new_dy;
}


//Getters

public int radius(){    
    return location.width/2;
}

public int x(){
    return location.x + radius();
}

public int y(){
    return location.y + radius();
}

public double xMotion(){
    return dx;
}

public double yMotion(){    
    return dy;
}

public Rectangle region(){
    return location;
}

//Methods that change attributes of the ball

public void moveTo(int x, int y){
    location.setLocation(x, y);
}

public void move(){
    location.translate((int) dx,(int) dy);
}

public void paint1(Graphics g){

    g.setColor(color);
    g.fillOval(location.x, location.y,location.width,location.height);
}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Abdullah A Malik
  • 354
  • 2
  • 4
  • 12

1 Answers1

3
  1. Use Swing instead of AWT, AWT is basically obsolute
  2. Don't override paint of top level containers, you've actually stepped into one of the reason why you shouldn't
  3. Don't call repaint or any method that might call repaint from inside any paint method, it will create an infinite loop that will consume your CPU
  4. Painting is passive in AWT/Swing. That means that updates occur at irregular intervals based on changes, like mouse movements or changes to the size of the frame.

Now, to the core of your problem. Basically, you're painting under the frame decoration. This occurs because the window has decorations added inside it's visible area, not added onto it.

See How to get the EXACT middle of a screen, even when re-sized and How can I set in the midst? for more details

This is why it is recommended that you don't override paint of top level containers. You need to create a component which can be added to the frame and onto which you can then paint

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i have extended my BallWorld class to JPanel and have added to a JFrame. Now it shows a straight line instead of a moving ball. Still have some problem with the repaint() or what? please help!! – Abdullah A Malik Jul 19 '13 at 13:37
  • You didn't call super.paintCompoonet, assuming your overrode paintComponent – MadProgrammer Jul 19 '13 at 20:25