Menu

[01c4ec]: / Gene.java  Maximize  Restore  History

Download this file

170 lines (154 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (C) 2006-2010 Joan Queralt Molina
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package biogenesis;
import java.io.*;
import java.awt.*;
/**
* This class implements a single organism's gene. A gene is a colored segment.
* This segment is part of the organism's body and will be drawn several times
* depending on the symmetry of the organism. Genes are always segments starting
* at (0,0). The position in the organism's body depends on their gene neighbors
* and the organism's symmetry and mirroring.
*/
public class Gene implements Cloneable, Serializable {
/**
* The version number of this class
*/
private static final long serialVersionUID = Utils.FILE_VERSION;
private double _length = 0;
private double _theta = 0;
/**
* Segment's color
*/
private Color _color;
/**
* Void constructor. Creates the gene but leave it uninitialized.
*/
public Gene() {
}
/**
* Creates a gene with the specified final point and color.
*
* @param x
* x coordinate of the final point
* @param y
* t coordinate of the final point
* @param color
* segment's color
*/
public Gene(double length, double theta, Color color) {
_length = length;
_theta = theta;
_color = color;
}
public void randomizeColor() {
int max_prob = Utils.RED_PROB + Utils.GREEN_PROB + Utils.BLUE_PROB
+ Utils.CYAN_PROB + Utils.WHITE_PROB + Utils.GRAY_PROB
+ Utils.YELLOW_PROB;
int prob = Utils.random.nextInt(max_prob);
int ac_prob = Utils.RED_PROB;
if (prob < ac_prob) {
_color = Color.RED;
return;
}
ac_prob += Utils.GREEN_PROB;
if (prob < ac_prob) {
_color = Color.GREEN;
return;
}
ac_prob += Utils.BLUE_PROB;
if (prob < ac_prob) {
_color = Color.BLUE;
return;
}
ac_prob += Utils.CYAN_PROB;
if (prob < ac_prob) {
_color = Color.CYAN;
return;
}
ac_prob += Utils.WHITE_PROB;
if (prob < ac_prob) {
_color = Color.WHITE;
return;
}
ac_prob += Utils.GRAY_PROB;
if (prob < ac_prob) {
_color = Color.GRAY;
return;
}
_color = Color.YELLOW;
}
public void randomizeLength() {
_length = 2.0 + Utils.random.nextDouble() * 16.0;
}
public void randomizeTheta() {
_theta = Utils.random.nextDouble() * 2.0 * Math.PI;
}
/**
* Randomize the component of this gene: final point and color. Coordinates
* are given a random number between -13 and -2 or 2 and 13. Color is given
* a random color. The probability of each color is taken from user
* preferences.
*/
public void randomize() {
randomizeLength();
randomizeTheta();
randomizeColor();
}
/**
* Return an exact copy of this gene.
*/
@Override
public Object clone() {
Gene newGen = null;
try {
newGen = (Gene) super.clone();
} catch (CloneNotSupportedException e) {// We should never reach this
}
return newGen;
}
public double getLength() {
return _length;
}
public double getTheta() {
return _theta;
}
/**
* Returns the segment's color.
*
* @return the segment's color
*/
public Color getColor() {
return _color;
}
/**
* Assign a color to the segment.
*
* @param color
* The color to assign
*/
public void setColor(Color color) {
_color = color;
}
public void setLength(double length) {
_length = length;
}
public void setTheta(double theta) {
_theta = theta;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.