java - 将许多对象绘制到 JPanel 上的替代方法

标签 java sql serialization arraylist

我将重写下面的类以使用构造函数并做一些更聪明的事情(而不是四个类似 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;
    }
}

作为一个相关的旁注:当我处理这段代码时,我将不再使用 boolean 值(项目已经有一两年了,在我知道我在做什么之前)!

最佳答案

将很多很多项绘制到组件上不是问题,但它不应该发生在 paintComponent 内部。 。您想要做的是拥有一个单独的图像(例如 BufferedImage ),在该图像上绘画,然后是您的 paintComponent只负责将该图像绘制到 JPanel 上。因此,您的用户不会看到所有这些东西一一绘制。他们只看到最终产品。

搜索“Java 中的双缓冲”以获取有关此想法的精彩演示。

对于数据结构,保留 4 个列表就可以了,但您可能希望拥有在所有对象之间共享的更抽象的绘图实用程序。例如,除了颜色之外,绘制它们的方式大致相同,因此创建一个接受任何这些子类(例如 BiologyObjectProducer )的父类(super class)(例如 Decomposer )的绘图方法。等)作为参数。如果这没有意义,请告诉我。

更新:在 JPanel 上绘图的正确方法

private BufferedImage image;

public MyClass() {
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_ARGB);
}

public void paintOntoImage() {
    //here is where you would draw all the things that you currently have in your paintComponent method
    Graphics2D g = (Graphics2D) image.getGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    //now you can call "g.fillOval" or any other methods on "g"
}

public void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, null);
}

关于java - 将许多对象绘制到 JPanel 上的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735687/

相关文章:

c# - JSON 反序列化期间使用设置访问器

java - 为什么可以在 PHP 中覆盖实例变量而在 Java 中不能?

java - Kotlin 中选择 "when"与 "if/else"的标准是什么?

sql - Postgres 中的递归 SQL 查询

mysql - 根据条件过滤sql查询

WCF-域对象和IExtensibleDataObject

java - 为什么我的程序在视频第一帧后终止?

java - Android - 如何从其他类访问已创建的数据库?

sql - 具有计数和总和的单个 MySQL 查询

python - Django REST Framework 序列化一对一模型