Java graphics slow rendering of a draw methods. How to fix it?

Why the following code draws big space gap before circles equivalent to sum of consecutive executions of x=+10 statement?

class Panel extends JPanel {
    private int x=10;
    public void paintComponent( Graphics g ) {
        super.paintComponent( g );
        g.setColor( Color.MAGENTA );
        for (int i=1; i<=8; i++) {
            g.drawOval( x, 10, 50, 50 );
            x+=10;
        }
    }
}

public class Circles156 {
    public static void main(String[] args) {
    JFrame frame = new JFrame( "Drawing lines, rectangles and ovals" );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    Panel Panel = new Panel();
    Panel.setBackground( Color.WHITE );
    frame.add( Panel ); // add panel to frame
    frame.setSize( 800, 300 ); // set frame size
    frame.setVisible( true ); // display frame
    }
}

Upvotes: 0

Views: 347

Answers (1)

Jean Waghetti
Jean Waghetti

Reputation: 4727

Put x inside paintComponent() method. Every time it is called, x is will increase the "initial value" by 80.

Upvotes: 1

Related Questions