gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:52:50 25 4
gpt4 key购买 nike

我正在尝试创建一个程序来创建 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

最佳答案

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

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

当你创建 Biomorph 对象时,你可能有一个空的构造函数来创建一个随机的 Biomorph,然后你可能有一个将 Biomporh 对象作为参数的构造函数,这样你就可以根据“父”Biomorph 创建“子”Biomorph .

然后您可以创建一个ArrayList 来容纳多个Biomorh 对象。然后在 paintComponent() 方法中迭代 ArrayList 并在每个 Biomorph 对象上简单地调用 paintBoimorph(GRaphics) 方法。

Random() 方法只会在您最初创建 Biomorph 对象时使用。所有需要重绘的数据之后,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();
}
}

关于java - 在数据结构中存储随机行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30057399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com