Home » Java » Arraylist that changes without couse [on hold]

Arraylist that changes without couse [on hold]

Posted by: admin June 30, 2018 Leave a comment

Questions:

I have been working on BPP (bind paking problem) simulator with some algorithems and when i was testing i encounterd a bug that for reasons unknown re writes my arraylist and even arraylists that only have one conection to the array wil break

the arrays that break are the “ram” array in “BPP_Sim.java” and the “items” array in “BPPPanel.java” and “BPP_Sim.java”

this is my code

BPPPanel.java

        package com.sybrand.BPP;

import com.sybrand.BPP.BPP_Algo.BPP_First_Fit;
import com.sybrand.BPP.BPP_Algo.BPP_A1;
import com.sybrand.BPP.BPP_Algo.BPP_Full_Bin;

import javax.swing .*;
import java.awt .*;
import java.util.ArrayList;

public class BPPPanel extends JPanel {

    public static final int FIRST_FIT_ALG = 0;
    public static final int A1_ALG = 1;
    public static final int FULL_BIN_ALG = 2;

    private BPP_Algorithm currentAlg;

    ArrayList<Item> items;
    private int drawboxWidth; //keep constant
    private int drawboxHeight;
    int boxHeight;

    ArrayList<Box> packedBoxes;

    public BPPPanel(int boxHeight, ArrayList<Item> items) {
        this.setPreferredSize(new Dimension(2000, 900));
        this.boxHeight = boxHeight;
        this.items = items;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.LIGHT_GRAY);
        g.setColor(Color.BLACK);

        //TODO het tekenen van de dozen met gevulde inhoud in percentages
        try {

            drawboxWidth = (getWidth() / packedBoxes.size()) - (100 / packedBoxes.size()) - 5;
            drawboxHeight = (getHeight() / 2);

            //drawing products
            for (int i = 0; i < packedBoxes.size(); i++) {

                int x = 50 + (drawboxWidth * i) + (i * 5);
                int currentY = 50;

                for (Item item : packedBoxes.get(i).getItems()) {
                    float productHeight = (float) item.height / (float) boxHeight;
                    float drawProductHeight = drawboxHeight * productHeight;
                    g.setColor(item.color);
                    g.fillRect(x, currentY, drawboxWidth, (int) drawProductHeight);
                    currentY += drawProductHeight;
                }
                int y = 50;
                g.setColor(Color.BLACK);
                g.drawRect(x, y, drawboxWidth, drawboxHeight);

            }
//          this.items.clear();
//          for (Box doos:packedBoxes) {
//              for (Item ding:doos.getItems()) {
//                  this.items.add(ding);
//              }
//          }


            //drawing empty boxes
//            for (int i = 0; i < packedBoxes.size(); i++){
//                int x = 50 + (drawboxWidth * i) + (i*5);
//                int y = 50;
//                g.drawRect(x, y, drawboxWidth, drawboxHeight);
//            }

        } catch (NullPointerException ex) {

        }

        //TODO percentage van vulling bij de doos zetten

        //TODO kunnen wisselen tussen tonen van verschillende resultaten door opnieuw selecteren van algo of klikken op label
    }

    public void setAlg(int algo) {
        switch (algo) {
            case FIRST_FIT_ALG:
                currentAlg = new BPP_First_Fit(boxHeight, items);
                break;
            case A1_ALG:
                currentAlg = new BPP_A1(boxHeight, items);
                break;
            case FULL_BIN_ALG:
                currentAlg = new BPP_Full_Bin(boxHeight, items);
                break;
        }
        this.packedBoxes = currentAlg.getPackedBoxes();
        repaint();
    }

    public int getBoxHeight() {
        return boxHeight;
    }

    public void setBoxHeight(int boxHeight) {
        this.boxHeight = boxHeight;
    }

    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }
}

BPP_Sim.java

BPP_Full_Bin.java

Box.java

boxPanel.java

BPP_Algorithm.java

Item.java

these are the files that are nescecery for the algorithm that i made.
pls tell me it is fixable.
image of the structure

Answers: