Jump to content

Need help for some random Project

FrancisGroleau

Hi. i've made a program in JAVA applets. The program draw a polygon (A) with 4 randomly generated points then it draw another polygon with 4 randomly genrated point (B).

After this it compares each points of each ploygon 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

:

(Sorry my fields are written in french ...)

package francis.groleau.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(); }                }}}
Link to comment
Share on other sites

Link to post
Share on other sites

You should always post your code between code tags.

Now, I could just paste your code somewhere and format it automatically, but only have the time to say this for now.

I might edit this post or reply later.

Link to comment
Share on other sites

Link to post
Share on other sites

You should always post your code between code tags.

Now, I could just paste your code somewhere and format it automatically, but only have the time to say this for now.

I might edit this post or reply later.

Oh sorry i didn't know you could do that
Link to comment
Share on other sites

Link to post
Share on other sites

You should always post your code between code tags.

Now, I could just paste your code somewhere and format it automatically, but only have the time to say this for now.

I might edit this post or reply later.

is there anyway to keep the initial indentation because i had to do it manually... well anyways I hope it help ...
Link to comment
Share on other sites

Link to post
Share on other sites

Indentation! without proper formatting and indentation its difficult to read
i've tried to fix it but it's still sketchy ...
Link to comment
Share on other sites

Link to post
Share on other sites

Pas certain de comprendre exactement ce que tu veux faire, mais tu regénères toujours des points aléatoires différents pour ton polygonTmp, donc forcément à chaque fois que tu entres dans ta boucle pour comparer tes points, des fois ton point de ton polygonTmp va être plus grand que polygonCourant, et le coup d'après il peut être plus petit... alors c'est possible que ton polygonCourant ne sera jamais = polygonTmp.

Tu peux peut-être commencer avec ceci, il reste encore quelques trucs à corriger mais ça te donne sûrement une piste :



import java.applet.Applet;


import java.awt.Dimension;


import java.awt.Graphics;


import java.awt.Graphics2D;


import java.awt.Polygon;


import java.awt.image.BufferedImage;


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);


protected Polygon polygonTmp = createNewPolygon();





@Override


public void start()


{


isRunning = true;


new Thread(this).start();


}





@Override


public void stop()


{


isRunning = false;


}





@Override


public void init()


{


mDimension = getSize();


mBuffer = (BufferedImage) createImage(mDimension.width, mDimension.height);


mGraphics = mBuffer.createGraphics();


}





@Override


public void paint(Graphics pGraphics)


{


pGraphics.clearRect(0, 0, mDimension.width, mDimension.height);





if (comparaisonFinni)


{


polygonTmp = createNewPolygon();


polygonCourant = 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;


}





@Override


public void update(Graphics pGraphics)


{


paint(pGraphics);


}





@Override


public void run()


{


while (isRunning)


{


repaint();


try


{


Thread.sleep(mFPS);


}


catch (InterruptedException pException)


{


pException.printStackTrace();


}


}


}


}

Link to comment
Share on other sites

Link to post
Share on other sites

Pas certain de comprendre exactement ce que tu veux faire, mais tu regénères toujours des points aléatoires différents pour ton polygonTmp, donc forcément à chaque fois que tu entres dans ta boucle pour comparer tes points, des fois ton point de ton polygonTmp va être plus grand que polygonCourant, et le coup d'après il peut être plus petit... alors c'est possible que ton polygonCourant ne sera jamais = polygonTmp.

Tu peux peut-être commencer avec ceci, il reste encore quelques trucs à corriger mais ça te donne sûrement une piste :



import java.applet.Applet;


import java.awt.Dimension;


import java.awt.Graphics;


import java.awt.Graphics2D;


import java.awt.Polygon;


import java.awt.image.BufferedImage;


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);


protected Polygon polygonTmp = createNewPolygon();





@Override


public void start()


{


isRunning = true;


new Thread(this).start();


}





@Override


public void stop()


{


isRunning = false;


}





@Override


public void init()


{


mDimension = getSize();


mBuffer = (BufferedImage) createImage(mDimension.width, mDimension.height);


mGraphics = mBuffer.createGraphics();


}





@Override


public void paint(Graphics pGraphics)


{


pGraphics.clearRect(0, 0, mDimension.width, mDimension.height);





if (comparaisonFinni)


{


polygonTmp = createNewPolygon();


polygonCourant = 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;


}





@Override


public void update(Graphics pGraphics)


{


paint(pGraphics);


}





@Override


public void run()


{


while (isRunning)


{


repaint();


try


{


Thread.sleep(mFPS);


}


catch (InterruptedException pException)


{


pException.printStackTrace();


}


}


}


}

Exact ahaha je viens de trouver l'erreur aussi .Le but était de créer deux polygon. Polygon A et Polygon B. Seulement le A est déssiné. les points du polygon A devait se dirigé tranquillement vers leur nouvel position c'est à dire les points du Polygon B mais ça marche à présent merci
Link to comment
Share on other sites

Link to post
Share on other sites

i've fixed my problem it's working now !! here is the final code



package francis.groleau.games;








import java.applet.Applet;


import java.awt.BasicStroke;


import java.awt.Dimension;


import java.awt.Graphics;


import java.awt.Graphics2D;


import java.awt.Color;


import java.awt.Image;


import java.awt.Polygon;


import java.awt.geom.GeneralPath;


import java.awt.image.BufferedImage;


import java.awt.image.ImageProducer;


import java.util.Random;





import javax.crypto.spec.GCMParameterSpec;





public class graphics extends Applet implements Runnable


{


public boolean isRunning = false;





private int width, height;


private Image offScreenImage;


private Graphics offScreenGraphics;


private Thread animationThread = null;





protected boolean comparaisonFinni = false;


protected Polygon polygonTmp = createNewPolygon();


protected Polygon polygonCourant = createNewPolygon();








//number of frame per seconds for instance here it's 30FPS.


protected int mFPS = (1000 / 30);





public void start()


{


animationThread = new Thread(this);


animationThread.start();


}


public void stop()


{


animationThread.stop();


}





public void init()


{


//Initialized each fields.


width = getSize().width;


height = getSize().height;


offScreenImage = createImage(width, height);


offScreenGraphics = offScreenImage.getGraphics();


offScreenGraphics.setColor(Color.black);


}





public void paint(Graphics pGraphics)


{





//if the 2 polygon match create a new one


if (comparaisonFinni)


polygonTmp = createNewPolygon();





//Check if each points are the same if not increment or decrement points


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]++;


}


//hardcoded .... chhhhh ... tssss


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;


}


}





//Doubled buffering to eliminate flickering in the applet


offScreenGraphics.clearRect(0, 0, width, height);


offScreenGraphics.drawPolygon(polygonCourant);


((Graphics2D) offScreenGraphics).setStroke(new BasicStroke(5.0f, // Line width


BasicStroke.CAP_ROUND, // End-cap style


BasicStroke.JOIN_ROUND));





pGraphics.drawImage(offScreenImage, 0, 0, this);


}





public Polygon createNewPolygon()


{


Random aNumber = new Random();


int x1Points[] = new int [4];


int y1Points[] = new int [4];





for (int i = 0; i < 4; i++)


{


//set the number in random.next to not exceed your Applet size. You can modify the Applet size in run Configuration


x1Points[i] = aNumber.nextInt(600);


y1Points[i] = aNumber.nextInt(600);


}





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





if (comparaisonFinni)


{


comparaisonFinni = false;


polygonCourant = createNewPolygon();


}


return lePolygon;


}





public void update(Graphics pGraphics)


{


paint(pGraphics);


}





public void run()


{


Thread myThread = Thread.currentThread();





//IT'S A WHILE TRUE !!! you could maybe add a button start and set the thread value there but meh...


while(animationThread==myThread)


{


repaint();


try


//sets the number of frame per seconds


{ Thread.sleep(mFPS); }


catch (InterruptedException pException)


{ pException.printStackTrace(); }


}


}





}


Link to comment
Share on other sites

Link to post
Share on other sites

i've fixed my problem it's working now !! here is the final code

package francis.groleau.games;

import java.applet.Applet;

import java.awt.BasicStroke;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.Image;

import java.awt.Polygon;

import java.awt.geom.GeneralPath;

import java.awt.image.BufferedImage;

import java.awt.image.ImageProducer;

import java.util.Random;

import javax.crypto.spec.GCMParameterSpec;

public class graphics extends Applet implements Runnable

{

public boolean isRunning = false;

private int width, height;

private Image offScreenImage;

private Graphics offScreenGraphics;

private Thread animationThread = null;

protected boolean comparaisonFinni = false;

protected Polygon polygonTmp = createNewPolygon();

protected Polygon polygonCourant = createNewPolygon();

//number of frame per seconds for instance here it's 30FPS.

protected int mFPS = (1000 / 30);

public void start()

{

animationThread = new Thread(this);

animationThread.start();

}

public void stop()

{

animationThread.stop();

}

public void init()

{

//Initialized each fields.

width = getSize().width;

height = getSize().height;

offScreenImage = createImage(width, height);

offScreenGraphics = offScreenImage.getGraphics();

offScreenGraphics.setColor(Color.black);

}

public void paint(Graphics pGraphics)

{

//if the 2 polygon match create a new one

if (comparaisonFinni)

polygonTmp = createNewPolygon();

//Check if each points are the same if not increment or decrement points

for (int i = 0; i < 4; i++)

{

if (polygonTmp.xpoints < polygonCourant.xpoints)

{

polygonCourant.xpoints--;

}

if (polygonTmp.xpoints > polygonCourant.xpoints)

{

polygonCourant.xpoints++;

}

if ( polygonTmp.ypoints < polygonCourant.ypoints)

{

polygonCourant.ypoints--;

}

if ( polygonTmp.ypoints > polygonCourant.ypoints)

{

polygonCourant.ypoints++;

}

//hardcoded .... chhhhh ... tssss

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;

}

}

//Doubled buffering to eliminate flickering in the applet

offScreenGraphics.clearRect(0, 0, width, height);

offScreenGraphics.drawPolygon(polygonCourant);

((Graphics2D) offScreenGraphics).setStroke(new BasicStroke(5.0f, // Line width

BasicStroke.CAP_ROUND, // End-cap style

BasicStroke.JOIN_ROUND));

pGraphics.drawImage(offScreenImage, 0, 0, this);

}

public Polygon createNewPolygon()

{

Random aNumber = new Random();

int x1Points[] = new int [4];

int y1Points[] = new int [4];

for (int i = 0; i < 4; i++)

{

//set the number in random.next to not exceed your Applet size. You can modify the Applet size in run Configuration

x1Points = aNumber.nextInt(600);

y1Points = aNumber.nextInt(600);

}

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

if (comparaisonFinni)

{

comparaisonFinni = false;

polygonCourant = createNewPolygon();

}

return lePolygon;

}

public void update(Graphics pGraphics)

{

paint(pGraphics);

}

public void run()

{

Thread myThread = Thread.currentThread();

//IT'S A WHILE TRUE !!! you could maybe add a button start and set the thread value there but meh...

while(animationThread==myThread)

{

repaint();

try

//sets the number of frame per seconds

{ Thread.sleep(mFPS); }

catch (InterruptedException pException)

{ pException.printStackTrace(); }

}

}

}

cant spot any changes I would make, looks pretty efficient (well as java can be :P) just for you're own sake you should drop the closed curly brackets at the end directly under there corresponding starting brackets, makes it easier to spot where loops start and stop in most text editors (like notepad++ for example).

Well done :)

Link to comment
Share on other sites

Link to post
Share on other sites

He could have used a constant for the number of vertices :

final int NBR_VERTICES = 4;

Then simplify the vertices adjustment checks like this :

for (int i = 0; i < NBR_VERTICES; i++)

{


if (polygonTmp.xpoints[i] != polygonCourant.xpoints[i])


{


polygonCourant.xpoints[i] = polygonTmp.xpoints[i] < polygonCourant.xpoints[i] ? polygonCourant.xpoints[i] - 1 : polygonCourant.xpoints[i] + 1;


}


if (polygonTmp.ypoints[i] != polygonCourant.ypoints[i])


{


polygonCourant.ypoints[i] = polygonTmp.ypoints[i] < polygonCourant.ypoints[i] ? polygonCourant.ypoints[i] - 1 : polygonCourant.ypoints[i] + 1;


}

}

And also the check to see if both polygons match :

[SIZE=13px]comparaisonFinni = true;[/SIZE]

for(int i = 0; i < NBR_VERTICES; i++){


if(polygonCourant.xpoints[i] != polygonTmp.xpoints[i] || polygonCourant.ypoints[i] != polygonTmp.ypoints[i]){


comparaisonFinni = false;


break;


}

[SIZE=13px]}[/SIZE]

That way all you would need to do is change the constant to make it work with polygons with any given number of vertices.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×