user2321607
user2321607

Reputation: 1

Using A While Loop With g.drawPolygon Code

So this is my code for making a bunch of tables side by side.(I'm still a beginner)

import java.applet.Applet;
import java.awt.*;

public class Test extends Applet{

public void init() {
setSize(500, 225);
}

public void paint (Graphics g){

//Desk #1
int [ ] x8 = {430, 430, 351, 351};
int [ ] y8 = {200, 185, 185,200};
g.drawPolygon(x8, y8, 4);

//Desk #2
int [ ] x9 = {351, 351, 272, 272};
int [ ] y9 = {200, 185, 185, 200};
g.drawPolygon(x9, y9, 4);

//Desk #3
int [ ] x10 = {272, 272, 193, 193};
int [ ] y10 = {185, 200, 200, 185};
g.drawPolygon(x10, y10, 4);

//Desk #4
int [ ] x11 = {193, 193, 114, 114};
int [ ] y11 = {185, 200, 200, 185};
g.drawPolygon(x11, y11, 4);

//Desk #5
int [ ] x12 = {114, 114, 35, 35};
int [ ] y12 = {185, 200, 200, 185};
g.drawPolygon(x12, y12, 4);

}
}

What I want to be able to do is just make a while loop so then i don't need to do all of this sequence garbage, can someone please make an effective while loop code for me and teach me how they did it, i've been stuck on this for a long time.

Upvotes: 0

Views: 369

Answers (5)

user2321607
user2321607

Reputation: 1

Thank for everyone's work and it did help so thank you and the last guy that for loop is awesome way also but in that time i managed to find out how to do it by myself with a while loop as you see below! :)

    //Desk #1-4 #Using While Loop
    int c=0;
    int [ ] x1 = {65, 65, 153, 153};
    int [ ] y1 = {20, 35, 35, 20};
    g.drawPolygon(x1, y1, 4);


    while (c <= 3) {
        g.drawPolygon(x1, y1, 4);

        x1[0] += 88;
        x1[1] += 88;
        x1[2] += 88;
        x1[3] += 88;

        c += 1;  // c = c + 1; - Same thing.
    }

Upvotes: 0

Sami Korhonen
Sami Korhonen

Reputation: 1303

Here's an alternative solution that uses simple math to calculate coordinates.

public class Test extends Applet {
    public void init() {
        setSize(500, 225);
    }

    public void paint(Graphics g) {
        int deskCount = 5;
        int[] y = { 200, 185, 185, 200 };
        for (int i = 0; i < deskCount; i++) {
            int[] x = { 430 - i * 79, 430 - i * 79, 351 - i * 79, 351 - i * 79 };
            g.drawPolygon(x, y, 4);
        }
    }
}

Upvotes: 0

Paul Grime
Paul Grime

Reputation: 15104

You could maybe used an inner class to store the coords. I'm not sure if you wanted to focus on multi-dimensional arrays or not.

public class Test extends Applet {

    Poly desk1 = new Poly(new int[] {430, 430, 351, 351}, new int[] {200, 185, 185,200});
    Poly desk2 = new Poly(new int[] {351, 351, 272, 272}, new int[] {200, 185, 185, 200});
    Poly desk3 = new Poly(new int[] {272, 272, 193, 193}, new int[] {185, 200, 200, 185});
    Poly desk4 = new Poly(new int[] {193, 193, 114, 114}, new int[] {185, 200, 200, 185});
    Poly desk5 = new Poly(new int[] {114, 114, 35, 35}, new int[] {185, 200, 200, 185});

    Poly[] desks = new Poly[] {desk1, desk2, desk3, desk4, desk5};

    public void init() {
        setSize(500, 225);
    }

    public void paint (Graphics g) {
        for (int i = 0; i < desks.length; i++) {
            g.drawPolygon(desks[i].xs, desks[i].ys, 4);
        }
    }

    private static class Poly {
        // public fields are sometimes frowned upon,
        // but for a private class and a simple example
        public int[] xs;
        public int[] ys;

        public Poly(int[] xs, int[] ys) {
            this.xs = xs;
            this.ys = ys;
        }
    }

}

Upvotes: 1

Jay Patel - PayPal
Jay Patel - PayPal

Reputation: 1489

I wont give you the answer, but this might help. Create a two dimensional array int[][] with size [4][4] for x and [4][4] for y.

Then you can create a while loop to iterate through array. Even a for loop is good enough too.

something like this

for(int i = 0 ; i < 4; i++) {
  g.drawPolygon(x[i], y[i]);
}

Upvotes: 0

Vivin Paliath
Vivin Paliath

Reputation: 95588

Instead of having individual arrays for each of those coordinates, why not have something like this:

int[][] x = {
    {123, 534, 643},
    {123, 543, 152},
    ...
    {543, 125, 163}
};

int[][] y = {
    {123, 534, 643},
    {123, 543, 152},
    ...
    {543, 125, 163}
};

Now you just need to iterate over these using loops. Keep in mind that the implicit assumption here seems to be that both x and y have the same number of coordinates, so you should be able to iterate over both of them at the same time to get corresponding x's and y's.

Asking SO to write your code out for you will probably not work since we don't really give code out here unless you show significant work on your part.

Upvotes: 0

Related Questions