将随机行值存储在数据结构中

时间:2015-05-05 15:38:01

标签: java swing

我正在尝试创建一个程序,该程序创建了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的屏幕截图: http://i.imgur.com/I6qweT7.png

1 个答案:

答案 0 :(得分:2)

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

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

当您创建Biomorph对象时,您可能有一个空构造函数,它会创建一个随机的Biomorph,那么您可能有一个构造函数将Biomporh对象作为参数,因此您可以基于“父”Biomorph创建“子”Biomorphs

然后,您可以创建一个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();
    }
}