0

So I have two files and I want to access a variable from my first file and use it in my second file which paint a pie chart.

import java.util.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    File file = new File(args[0]);
    Scanner input = new Scanner(file);
    //int i = 0;
    ArrayList<Integer> slices = new ArrayList<Integer>();
    ArrayList<String> names = new ArrayList<String>();
    while (input.hasNextLine()) {
            names.add(input.next());    
            slices.add(input.nextInt());
        }
    JFrame f = new JFrame("Pie chart");
    f.setSize(600, 350);
    f.setDefaultCloseOperation(
        JFrame.EXIT_ON_CLOSE);
        f.add(new PieChart());
        f.setVisible(true);     
}
}

here is my second file

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

public class PieChart
extends JComponent {

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    Graphics2D g3 = (Graphics2D) g.create();
    g3.setColor(Color.BLACK);
    g2.setColor(Color.BLUE);
    for (int i = 0; i < 4; i = i + 1) {
        g2.fillRect(230, 20 * i + 50 , 20, 20); 
        g3.drawString(names.get(i), 255, 20 * i + 65);  
        g3.drawString("37.0%", 385, 20 * i + 65);           
    }
    g2.fillArc(50, 50, 150, 150, 0, 360);       
}
}

here's the error i'm getting...
error

I am trying to read from a text file into two separate arrays. I want one list to hold the names and then one to hold the values. I then want to be able to then access the values from the array from my second file.

  • 1
    As was started in your [previous question](http://stackoverflow.com/questions/32728588/pie-chart-using-graphics-2d-having-trouble-using-arrays/32728636#32728636), you will need pass the arrays to the `PieChart`, possible via a constructor. Take a look at [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) for more details. You should also be calling `dispose` on the `Graphics2D` contexts which you create when you no longer need them – MadProgrammer Sep 23 '15 at 02:02

1 Answers1

1

You could pass names to the PieChart constructor. Change

f.add(new PieChart());

to something (assuming you need slices too) like

f.add(new PieChart(slices, names));

and in PieChart, add local slices and names fields and a constructor. Something like

private List<Integer> slices;
private List<String> names;
public PieChart(List<Integer> slices, List<String> names) {
     this.slices = slices;
     this.names = names;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • will that give it the entire list? – pablo escobrah Sep 23 '15 at 02:04
  • In fact, they will refer to the same lists from your first class. `List` is a type of `Object`, therefor it's value is a *reference*. If you don't use something like `this.slices = new ArrayList<>(slices)` then it's the same `slices` from your first instance. To verify this (I'm just some guy on the Internet after all), try calling `this.slices.clear()`. – Elliott Frisch Sep 23 '15 at 02:07
  • Do I have to create a completely new constructor or can I add (List slices, List names) after "public class PieChart extends JComponent"? – pablo escobrah Sep 23 '15 at 02:09
  • I posted an example constructor for you. – Elliott Frisch Sep 23 '15 at 02:10
  • Yes but in my second file my current PieChart class extends JComponent. What I'm asking is do I edit my code to match yours or add that new constructor in seperately? – pablo escobrah Sep 23 '15 at 02:14
  • That's a declaration, not a constructor. Your code has a default empty constructor. – Elliott Frisch Sep 23 '15 at 02:36