Search code examples
javaswinggraphics2djcomponentpaintcomponent

JComponent wont show image


Im a beginner programmer and Im trying to get this program to run. Everything compiles correctly although no images appear on the new Java screen for the JComponent. Pretty much what this program is suppose to do is take an input value and assign it to to the values of a bar chart for size. In the program I have to use an exterior paintcomponent class to run the original class as well as have a driver which is probably what is throwing me off. Thanks in Advance!

public class BarChartTester
{
public static void main(String[] args)
  {

      BarChartPaintComponent component = new BarChartPaintComponent();
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the Values you wish to use (>0). Press -1 on an empty line to stop");
      Boolean flag = false;
      while(!flag)
      {
           double numbers = in.nextDouble();
           if(numbers == -1)
           flag = true;
           else if(numbers<-1)
           System.out.println("You have typed in invalid number");
           else
           component.add(numbers);
     }
      JFrame frame = new JFrame();
      frame.setSize(300, 300);
      frame.setTitle("A Bar Graph");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(component);
      frame.setVisible(true);
    }

}

public class BarChart extends JComponent
{
private ArrayList<Double> list;
private double value;
private int i;

public BarChart()
{

    list = new ArrayList<Double>();
}


public void add(double value)
{
    list.add(i, value);
    i++;
}

public void draw(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    Double greatest = list.get(0);
    Double least;
    for(int j =1;j<=list.size();j++)
    {
        if(list.get(j)> greatest)
        greatest = list.get(j);
        else
        least = list.get(j);


    }

    for(int i = 0;i<=list.size();i++)
    {
        int x = 20;
        int width = 20;
        double barNumber = list.get(i);
        double size = barNumber;
        if(list.get(i) == greatest){
        g2.setPaint(Color.BLUE);
        g2.fill(new Rectangle2D.Double(x,300,width,300));
        }
        else
        {
            g2.setPaint(Color.BLUE);
             g2.fill(new Rectangle2D.Double(x,300,width, barNumber));
         }

        x +=20;

    }



}










}


public class BarChartPaintComponent extends BarChart
{
public void paintComponent(Graphics g, double array){
    Graphics2D g2 = (Graphics2D) g;
    BarChart component= new BarChart();
    component.add(array);
    component.draw(g2);

}
}

Solution

  • Your main problem is that the code intended do draw will never be called, since you are not implementing the paintComponent(Graphics g) method. So, replace your paintComponent for this:

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        this.draw(g2);
    }
    

    Then make the corrections suggested by Stevens.

    Also, in the second for, you are initializing the variable x (int x = 20). This way, x will be 20 at every iteration of the loop. Do this before the for loop.