我在 JAVA 小程序中制作了一个程序。该程序用 4 个随机生成的点绘制一个多边形A
,然后用 4 个随机生成的点绘制另一个多边形B
。在此之后,它比较每个多边形的每个点以查看它们是否相同。如果不是,那就A[0] > B[0]
说吧A[0]--
。直到旧多边形的所有点都A
变成新多边形B
。然后它重复。
所以我遇到的问题是,由于某种原因,多边形变得越来越小。
这是我的代码:
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(); }
}
}
}