Hello,

The program below selects two shapes at random from a choice of four shapes (line, rectangle, oval and rounded rectangle) and draws them onto an applet. Once six such shapes have been drawn, the screen is cleared and the above process continues indefinitely. The issue I am having is that the program only draws four shapes before resetting instead of six. I would be grateful for any help.

Thank you!

import java.awt.image.BufferedImage; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.util.Random; 




public class ScreenSaver1 extends JApplet implements ActionListener 
{ 

    private BufferedImage image; 
    private Timer timer; 
    private int count = 0, number; 
    private Random random = new Random(); 
    private boolean flag = true; 
    private boolean flag1 = true; 
    private int delay, shape1, shape2; 


    public void init() 
    { 
        image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); 
        timer = new Timer(1000, this); 
        timer.start(); 

        Graphics g = image.getGraphics(); 
        g.setColor (Color.GRAY); 
        g.fillRect (0,0, image.getWidth(), image.getHeight()); 
    }

    public void actionPerformed(ActionEvent e) 
    { 
        Graphics g = image.getGraphics(); 
        int r = (int) (Math.random() * 255); 
        int gr = (int) (Math.random() * 255); 
        int b = (int) (Math.random() * 255); 
        g.setColor(new Color (r,gr,b)); 
        while (flag == true) 
        { 
            shape1 = random.nextInt(4) + 1; 
            shape2 = random.nextInt(4) + 1; 
            while (shape2 == shape1) 
            { 
                shape2 = random.nextInt(4) + 1; 
            } 
            flag = false; 
       } 

       if (shape1 == 1 || shape2 == 1) 
       { 
           int x1 = (int) (Math.random() * getWidth()); 
           int y1 = (int) (Math.random() * getHeight()); 
           int x2 = (int) (Math.random() * getWidth()); 
           int y2 = (int) (Math.random() * getHeight()); 
           g.drawLine (x1, y1, x2, y2); 
        } 


        if (shape1 == 2 || shape2 == 2) 
        { 
            int x1 = (int) (Math.random() * getWidth()); 
            int y1 = (int) (Math.random() * getHeight()); 
            g.fillRect (x1, y1, 50, 70); 
        } 


        if (shape1 == 3 || shape2 == 3) 
        { 
            int x1 = (int) (Math.random() * getWidth()); 
            int y1 = (int) (Math.random() * getHeight()); 
            g.fillOval (x1, y1, 50, 70); 
        } 


        if (shape1 == 4 || shape2 == 4) 
        { 
            int x1 = (int) (Math.random() * getWidth()); 
            int y1 = (int) (Math.random() * getHeight()); 
            g.fillRoundRect(x1, y1, 50, 70, 10, 20); 
        } 
        count = count + 2; 

        repaint();

        if (count == 6) 
        { 
            g.setColor (Color.GRAY); 
            g.fillRect (0,0, image.getWidth(), image.getHeight()); 
            flag = true; 
            count = 0; 

        } 

    }



    public void paint(Graphics g) 
    { 
       g.drawImage(image, 0, 0, this); 

    } 

} 

Recommended Answers

All 2 Replies

You increment count by 2 on each loop, so its values go 0, 2, 4, 6, 0, 2 ... that's a four value cycle

ps you have a redundntt loop lines 41/50 - flad g is always set false, so its only ever executed once.

pps while (flag == true) ... flag is a boolean, so that's just while(flag)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.