2

I've made a program in JAVA applets. The program draw a polygon A with 4 randomly generated points then it draws another polygon with 4 randomly generated point B. After this it compares each points of each polygon to see if they're the same. If they are not, let say A[0] > B[0] then A[0]--. Until all the points of the old polygon A becomes the new polygon B. Then it repeat.

So the problem I have is that for some reason the Polygons just keep getting tinnier.

Here is my Code:

package some.package.games;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.util.Random;

public class graphics extends Applet implements Runnable
{
    public boolean isRunning = false;
    protected BufferedImage mBuffer;
    protected boolean comparaisonFinni = false;
    protected Polygon polygonCourant = createNewPolygon();
    protected Graphics2D mGraphics;
    protected Dimension mDimension;
    protected int mFPS = (1000 / 9);

    public void start()
    {
        isRunning = true;
        new Thread(this).start();
    }
    public void stop()
    {
        isRunning = false;
    }

    public void init()
    {
        mDimension = getSize();
        mBuffer = (BufferedImage)createImage(mDimension.width, mDimension.height);  
        mGraphics = (Graphics2D)mBuffer.createGraphics();
    }

    public void paint(Graphics pGraphics)
    {
        pGraphics.clearRect(0, 0, mDimension.width, mDimension.height);


        Polygon polygonTmp = new Polygon();

        if (!comparaisonFinni)
            polygonTmp = createNewPolygon();


            for (int i = 0; i < 4; i++) 
            {
                if (polygonTmp.xpoints[i] < polygonCourant.xpoints[i])
                {
                    polygonCourant.xpoints[i]--;            
                }
                if (polygonTmp.xpoints[i] > polygonCourant.xpoints[i])
                {
                    polygonCourant.xpoints[i]++;
                }
                if ( polygonTmp.ypoints[i] < polygonCourant.ypoints[i])
                {
                    polygonCourant.ypoints[i]--;
                }
                if ( polygonTmp.ypoints[i] > polygonCourant.ypoints[i])
                {
                    polygonCourant.ypoints[i]++;
                }
                if ((polygonCourant.xpoints[0] == polygonTmp.xpoints[0]) && (polygonCourant.xpoints[1] == polygonTmp.xpoints[1]) &&
                        (polygonCourant.xpoints[2] == polygonTmp.xpoints[2]) && (polygonCourant.xpoints[3] == polygonTmp.xpoints[3]) &&
                        (polygonCourant.ypoints[0] == polygonTmp.ypoints[0]) && (polygonCourant.ypoints[1] == polygonTmp.ypoints[1]) && 
                        (polygonCourant.ypoints[2] == polygonTmp.ypoints[2]) && (polygonCourant.ypoints[3] == polygonTmp.ypoints[3]))
                {
                    comparaisonFinni = true;
                }   
            }       
         pGraphics.drawPolygon(polygonCourant);


    }

    public Polygon createNewPolygon()
    {   
        Random aNumber = new Random();
        int x1Points[] = new int [4];
        int y1Points[] = new int [4];

        for (int i = 0; i < 4; i++) 
        {
            x1Points[i] = aNumber.nextInt(200);
            y1Points[i] = aNumber.nextInt(300);
        }

        Polygon lePolygon = new Polygon(x1Points, y1Points, 4);

        if (comparaisonFinni)
        {
            comparaisonFinni = false;
            polygonCourant = lePolygon;

        }

        return lePolygon;

    }
    public void update(Graphics pGraphics)
    {
        paint(pGraphics);
    }

    public void run() 
    {
        while (isRunning)
        {
            repaint();
            try
            { Thread.sleep(mFPS); }
            catch (InterruptedException pException)
            { pException.printStackTrace(); }
        }
    }

}
ATrubka
  • 3,982
  • 5
  • 33
  • 52

1 Answers1

0

You use random there. For instance, xpoints are created as nextInt(200).

public Polygon createNewPolygon()
{   
    Random aNumber = new Random();
    int x1Points[] = new int [4];
    int y1Points[] = new int [4];

    for (int i = 0; i < 4; i++) 
    {
        x1Points[i] = aNumber.nextInt(200);
        y1Points[i] = aNumber.nextInt(300);
    }

    Polygon lePolygon = new Polygon(x1Points, y1Points, 4);

    if (comparaisonFinni)
    {
        comparaisonFinni = false;
        polygonCourant = lePolygon;

    }

    return lePolygon;

}

nextInt(200) is an evenly distributed random value with mean value = 100. This means that all xpoints slowly move towards value of 100. Not sure if that was your idea.

Same goes for ypoints. They move towards 150.

As a result, of course, you see that polygon shrinks around point (100,150).

ATrubka
  • 3,982
  • 5
  • 33
  • 52
  • Yes because i want the current Polygon (PolygonCourant) to match the new polygon (PolygonTmp) – Francis Groleau Feb 14 '13 at 17:32
  • I think it has something to do with the check for when it's building a new polygon but i'm not quite shure how to fix it yet – Francis Groleau Feb 14 '13 at 17:44
  • Nobody can tell what you're trying to draw here, but you probably don't want to create new polygonTmp every time. And maybe you want to do else after ifs. You have to tell what your goal is. – ATrubka Feb 14 '13 at 17:46
  • OK well i want to draw a random polygon A then create a new polygon B. Then modifie the polygon A to match the polygon B. and so on. – Francis Groleau Feb 14 '13 at 17:50
  • Do you just want to move this polygon in random direction? Or do you want one slowly move towards the other? – ATrubka Feb 14 '13 at 17:52
  • each points of the old polygon move slowly towards their new matching position ( polygon B points). witch is why i do polygonCourant.yPoint[i]++ – Francis Groleau Feb 14 '13 at 17:58
  • You use random there. For instance, xpoints are created as nextInt(200). It's an evenly distributed value with mean value = 100. This means that all xpoints slowly move towards value 100. Not sure if that was your idea. – ATrubka Feb 14 '13 at 18:03
  • As a result, of course, you see that polygon shrinks around point 100,150. – ATrubka Feb 14 '13 at 18:03
  • random.next(100) doesn't create a new int that has for value a random number between 0 and 100 ?? Anyways I've fixed the problem it was liked you said i was creating a new polygonTmp each time the paint method was called. Thanks for your answer !! – Francis Groleau Feb 14 '13 at 18:17
  • the only problem i have is that for some reason the applet is flickering but this is not realted to this Thread so.. i'll check on that – Francis Groleau Feb 14 '13 at 18:19
  • OK. That's what I thought. You don't need new random polygon upon every repaint. – ATrubka Feb 14 '13 at 18:29