我正在创建一个程序,该程序读取带有一系列x / y坐标的文本文件,并在每个应该放置的位置放置一个小方块。该程序还必须处理另一个文本文件,该文件具有一系列x / y坐标和一个双值,表示以分贝为单位的信号强度。当我开始启动程序时,它会在左上角显示一个带有白色小矩形的黑色屏幕。我的代码出了什么问题?我在控制台中没有收到任何错误。问题几乎可以肯定在我的第二个主要for循环中的coveRage.java文件中。
第一个文本文件......
500.0 500.0
250.0 250.0
第二个文本文件
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);
}
}
}
}
}
}