I am currently learning java. My professor has given me the task of representing the following pattern with squares. No imports or predefined classes may be used.

Template pattern

At the moment i have programmed a pattern that is too square (see output picture). Unfortunately I can’t get any further and don’t know how to adapt my code.

public void zeichne_Sinus (Graphics g) {
        Zeichenblatt zeichenblatt = new Zeichenblatt(g);

        int repetitions = 5;

        for (int rep = 0; rep < repetitions; rep++) {
            for (int i = 0; i < 5; i++) {
                Quadrat quad = new Quadrat();
                quad.setPositionX(i * 10 + rep * 100 + 10);
                quad.setPositionY(i * 10);
                if (i % 2 == 0) {
                    quad.setFarbe(Color.orange);
                } else {
                    quad.setFarbe(Color.green);
                }

                for (int j = 0; j < 5; j++) {
                    Quadrat quad2 = new Quadrat();
                    quad2.setPositionX(j * 10 + rep * 100 + 60);
                    quad2.setPositionY((4 - j) * 10 + rep);
                    if (j % 2 == 0) {
                        quad2.setFarbe(Color.green);
                    } else {
                        quad2.setFarbe(Color.orange);
                    }

                    zeichenblatt.zeichne(quad, quad2);
                }

            }
        }
    }

My output

Can you help me?

7

Khám phá các thẻ bài đăng