pablo escobrah
pablo escobrah

Reputation: 79

JFrame displaying black screen with a little white rectangle in the top right

I'm creating a program that reads a text file with a series of x/y coordinates and places a small square where each of them should go. The program also has to deal with another text file which has a series of x/y coordinates and a double value which represents a signal strength in decibels. When I go to launch to the program it displays a black screen with a small white rectangle in the top left corner. What is wrong with my code? I'm not getting any errors in the console. The problem is almost certainly in my coveRage.java file in the second main for loop.

what the program displays

first text file...

500.0 500.0
250.0 250.0

second text file

1000.0 2500.0 -143.2
1213.0 2132.0 -100.7

Main.java

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File towers = new File("towers.txt");
        File readings = new File("readings.txt");
        //System.out.println(new File(".").getAbsoluteFile());
        Scanner towers1 = new Scanner(towers);
        Scanner readings1 = new Scanner(readings);
        ArrayList<Integer> towerPos = new ArrayList<Integer>();
        ArrayList<Integer> readingPos = new ArrayList<Integer>();

        while(towers1.hasNextDouble()) {
            towerPos.add((int)towers1.nextDouble());
        }
        towers1.close();

        while(readings1.hasNextDouble()) {
            readingPos.add((int)readings1.nextDouble());
        }
        readings1.close();


        JFrame f = new JFrame("Cellphone Coverage");
        f.setVisible(true);     
        f.setSize(500, 500);
        f.setDefaultCloseOperation(
            JFrame.EXIT_ON_CLOSE);
        f.add(new CoveRage(towerPos, readingPos));

    }
}

coveRage.java

public class CoveRage 
extends JComponent {

    private ArrayList<Integer> readingPos;
    private ArrayList<Integer> towerPos;
    ArrayList<Integer> towerPosis = new ArrayList<Integer>();
    ArrayList<Integer> distances = new ArrayList<Integer>();
    int xAxis;
    int yAxis;

    public CoveRage(ArrayList<Integer> towerPos, ArrayList<Integer> readingPos) {
         this.towerPos = towerPos;
         this.readingPos = readingPos;
        }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        for (int j = 0; j < towerPos.size(); j += 2) {
            int xAxis = towerPos.get(j) / 10;
            int yAxis = towerPos.get(j + 1) / 10;
            towerPosis.add(xAxis); // adds to list for checking distance between tower and signal
            towerPosis.add(yAxis);
            g2.setColor(Color.black);
            g2.fillRect(xAxis, yAxis, 5, 5);
        }

        for (int i = 0; i < readingPos.size(); i =+ 3) { // for there are still readings left take in 3 values and repeat
            int xAxiss = readingPos.get(i) / 10; // grabs x axis of reading
            int yAxiss = readingPos.get(i + 1) / 10; // grabs y axis of reading
            int sigNal = readingPos.get(i + 2); // grabs signal strength of reading
                for (int k = 0; k < towerPosis.size(); k=+2) { // for there are still readings in towerPosis 
                    int distance = (int) Math.sqrt(Math.pow(towerPosis.get(k)-xAxiss, 2)+(Math.pow(towerPosis.get(k + 1)-yAxiss, 2))); // calulates distance between tower and reading
                    distances.add(distance); // add distance to arrayList
                    int leastDist = distances.get(0);
                    for (int u = 0; u < distances.size(); u++) { // for there are still distance
                        if (distances.get(u) < leastDist) {
                            leastDist = distances.get(u);
                        }
                        int expected = (int) ((int) 40*(Math.log10(1.0/leastDist)));
                        if (sigNal >= expected) {
                            g2.setColor(Color.green);
                            g2.fillRect(xAxiss, yAxiss, 5, 5);
                        } else if (sigNal <= expected - 9) {
                            g2.setColor(Color.red);
                            g2.fillRect(xAxiss, yAxiss, 5, 5);
                        }
                    }
            }
        }
    }
}

Upvotes: 1

Views: 724

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You have a (number of) infinite loops caused by k = +2, which is the same as saying k = 2, so k never increases in size

If you change all the = + to += it will work just fine

enter image description here

Upvotes: 4

Related Questions