我正在尝试创建一个程序,该程序可以创建9种生物体,这些生物体是从中间生物体进化而来的。目前,我有9个节目,尽管这些是完全随机的。存储要在其他8种中使用的初始生物形态的值的最佳方法是什么?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JPanel;

public class BiomorphObject extends JPanel {

Graphics2D g2d;
Dimension size = getSize();
int w = size.width;
int h = size.height;
Random ran = new Random();
int x1;
int x2;
int y1;
int y2;

public BiomorphObject(Graphics g){

    g2d = (Graphics2D) g;
    g2d.setColor(Color.BLUE);
    Random ran = new Random();
    int x = Math.abs(ran.nextInt(100)) % w + 120;
    int y = Math.abs(ran.nextInt(100)) % h;
    int xMirror = x - 120;
    g2d.drawLine(120, 85, x, y);
    g2d.drawLine(120, 85, 120 - xMirror, y);

    x1 = x;
    y1 = y;
    x2 = Math.abs(ran.nextInt(50)) % w + 120;
    y2 = Math.abs(ran.nextInt(50)) % h;

    paintBiomorph(g);
}

public void paintBiomorph(Graphics g) {

    super.paintComponent(g);

    for (int i = 0; i <= 10; i++) {
        int xMirror1 = x1 - 120;
        int xMirror2 = x2 - 120;
        g2d.drawLine(x1, y1, x2, y2);
        g2d.drawLine(120 - xMirror1, y1, 120 - xMirror2, y2);
        x1 = x2;
        y1 = y2;
        x2 = Math.abs(ran.nextInt(100)) % w + 120;
        y2 = Math.abs(ran.nextInt(100)) % h;
    }
}


}

这是我当前拥有的GUI的屏幕截图:

最佳答案

您不应在绘画方法中使用Random()方法。您无法控制Swing何时重新绘制组件,并且您不希望绘画随机改变。

相反,您需要在类中创建绘画方法可以用来进行自定义绘画的属性。因此,您应该创建一个Biomorph对象。该对象将包含绘制单个Biomorph所需的信息。它甚至应该包含一种绘制自身的方法。让我们将此方法称为paintBiomorph(Graphics)。

当您创建Biomorph对象时,您可能会有一个空的构造函数来创建一个随机的Biomorph,然后您可能会有一个将Biomporh对象作为参数的构造函数,以便可以基于“父” Biomorph创建“子” Biomorph。

然后,您可以创建一个包含多个ArrayList对象的Biomorh。然后,在paintComponent()方法中循环访问ArrayList,并简单地在每个Biomorph对象上调用paintBoimorph(GRaphics)方法。

仅在最初创建Biomorph对象时才使用Random()方法。然后,所有需要重绘的数据之后,Biomorph必须成为对象本身的一部分。

编辑:

Biomorph类可能类似于:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JPanel;

public class Biomorph extends JPanel
{
    List<Shape> lines = new ArrayList<Shape>();

    //Graphics2D g2d;
    //Dimension size = getSize();
    //int w = size.width;
    //int h = size.height;
    //Random ran = new Random();
    //int x1;
    //int x2;
    //int y1;
    //int y2;

    public Biomorph()
    {

        //g2d = (Graphics2D) g;
        //g2d.setColor(Color.BLUE);
        setForeground(Color.BLUE);

        Random ran = new Random();
        int x = Math.abs(ran.nextInt(100)) + 120;
        int y = Math.abs(ran.nextInt(100));
        int xMirror = x - 120;
//        g2d.drawLine(120, 85, x, y);
//        g2d.drawLine(120, 85, 120 - xMirror, y);
        lines.add( new Line2D.Double(120, 85, x, y);
        lines.add( new Line2D.Double(120, 85, -x, y);

        int x1 = x;
        int y1 = y;
        int x2 = Math.abs(ran.nextInt(50)) + 120;
        int y2 = Math.abs(ran.nextInt(50));

        for (int i = 0; i <= 10; i++) {
            int xMirror1 = x1 - 120;
            int xMirror2 = x2 - 120;
            //g2d.drawLine(x1, y1, x2, y2);
            //g2d.drawLine(120 - xMirror1, y1, 120 - xMirror2, y2);
            lines.add( new Line2D.Double(x1, y1, x2, y2);
            lines.add( new Line2D.Double(-x1, y1, -x2, y2);

            x1 = x2;
            y1 = y2;
            x2 = Math.abs(ran.nextInt(100)) + 120;
            y2 = Math.abs(ran.nextInt(100));
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setColor( getForeground() );

        for (Shape line: lines)
        {
            g2d.fill( line );
            //g2d.draw( line );
        }

        g2d.dispose();
    }
}

07-24 03:23