我将重写下面的类以使用构造函数并做一些更聪明的事情(而不是像四个 producer() 这样的方法 - 所以不用担心。
目前我遍历这四个数组列表,然后将对象绘制到 JPanel 上。在大约 2-3000 个对象之后,这可能会导致一些显着的延迟。
我可以用什么来替代这些数组列表?某种类型的 JSON/XML 序列化会起作用吗?在我的专业编码(而不是这个简单的副项目)中,我使用了一个庞大的系统,该系统使用 DataNucleus 将模型类转换为 SQL 中的表结构——我觉得这对于我有趣的副项目来说将是巨大的矫枉过正。想法?
public class Life {
PlayPanel panel;
public int prodSeed;
public int herbSeed;
public int predSeed;
public int decoSeed;
public ArrayList<Producer> prodStore = new ArrayList<Producer>();
public ArrayList<Herbivore> herbStore = new ArrayList<Herbivore>();
public ArrayList<Predator> predStore = new ArrayList<Predator>();
public ArrayList<Decomposer> decoStore = new ArrayList<Decomposer>();
public Life() {
}
public void setPanel(PlayPanel p) {
panel = p;
}
public void producers() {
for(int j = 0; j < prodSeed; j++) {
Producer p = new Producer(panel.life);
prodStore.add(p);
}
panel.producerPaint = true;
}
public void herbivores() {
for(int j = 0; j < herbSeed; j++) {
Herbivore h = new Herbivore(panel.life);
herbStore.add(h);
}
panel.herbivorePaint = true;
}
public void predators() {
for(int j = 0; j < predSeed; j++) {
Predator p = new Predator(panel.life);
predStore.add(p);
}
panel.predatorPaint = true;
}
public void decomposers() {
for(int j = 0; j < decoSeed; j++) {
Decomposer d = new Decomposer(panel.life);
decoStore.add(d);
}
panel.decomposerPaint = true;
}
public void beginAction() {
for (int j = 0; j < prodStore.size(); j++) {
prodStore.get(j).behavior();panel.repaint();
}
for (int j = 0; j < herbStore.size(); j++) {
herbStore.get(j).behavior();panel.repaint();
}
for (int j = 0; j < predStore.size(); j++) {
predStore.get(j).behavior();panel.repaint();
}
for (int j = 0; j < decoStore.size(); j++) {
decoStore.get(j).behavior();panel.repaint();
}
//Use booleans between these for loops to not repaint an extra amount of action
}
JPanel 类中非常痛苦的paintComponent 方法:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(producerPaint == true) {
//int j = 0;
while(j < life.prodStore.size()) {
g.setColor(new Color(19,145,14));
g.fillOval(life.prodStore.get(j).getxAxis(), life.prodStore.get(j).getyAxis(), life.prodStore.get(j).getxSize(), life.prodStore.get(j).getySize());
j++;
}
j=0;
}
if(herbivorePaint == true) {
//int j = 0;
while(j < life.herbStore.size()) {
g.setColor(new Color(228,226,158));
g.fillOval(life.herbStore.get(j).getxAxis(), life.herbStore.get(j).getyAxis(), life.herbStore.get(j).getxSize(), life.herbStore.get(j).getySize());
j++;
}
j=0;
}
if(predatorPaint == true) {
//int j = 0;
while(j < life.predStore.size()) {
g.setColor(new Color(207,87,87));
g.fillOval(life.predStore.get(j).getxAxis(), life.predStore.get(j).getyAxis(), life.predStore.get(j).getxSize(), life.predStore.get(j).getySize());
j++;
}
j=0;
}
if(decomposerPaint == true) {
//int j = 0;
while(j < life.decoStore.size()) {
g.setColor(Color.white);
g.fillOval(life.decoStore.get(j).getxAxis(), life.decoStore.get(j).getyAxis(), life.decoStore.get(j).getxSize(), life.decoStore.get(j).getySize());
j++;
}
j=0;
}
}
作为一个相关的旁注:当我处理这段代码时,我将不再使用布尔值(项目已经有一年或两年了,在我知道自己在做什么之前)!