0
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import java.util.Scanner;

public class BarGraph extends JPanel
{
  private int n1, n2, n3, n4, n5;
    BarGraph(int num1, int num2, int num3, int num4, int num5)
    {
        int n1 = num1;
        int n2 = num2;
        int n3 = num3;
        int n4 = num4;
        int n5 = num5;
    }

    public void paintComponent( Graphics g )
    {
        super.paintComponent(g);
        g.drawRect(0, 100, 100, 10);
        g.drawRect(0, 0, n1 * 10, 10);
        g.drawRect(0,20, n2 * 10, 10);
        g.drawRect(0,40, n3 * 10, 10);
        g.drawRect(0,60, n4 * 10, 10);
        g.drawRect(0,80, n5 * 10, 10);
     System.out.print(n1);
    }
}

BarGraphTest

package BarGraph;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Scanner;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author AJ
 */
public class BarGraphTest
{
    public static void main( String[] args)
    {

                System.out.print("Enter 5 integers seperated by spaces:");
                Scanner input = new Scanner(System.in);
                int n1 = input.nextInt();
                int n2 = input.nextInt();
                int n3 = input.nextInt();
                int n4 = input.nextInt();
                int n5 = input.nextInt();

        BarGraph panel = new BarGraph(n1, n2, n3, n4, n5);
        JFrame application = new JFrame();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        application.add( panel );
        application.setSize( 300, 300);
        application.setVisible( true );
    }
}

Basically trying to get 5 integers and draw 5 rectangles that are scaled accordingly. My variables are empty though. Am I missing something? I System.out.printed the variable n1, but there is nothing in it.

3
  • Look into variable shadowing. Commented Apr 1, 2014 at 19:16
  • What is a variable declaration? How do you do it? Once you've answered that, what are you doing here: int n1 = num1;? Commented Apr 1, 2014 at 19:16
  • I'm a dummy. Thanks so much. Commented Apr 1, 2014 at 19:22

1 Answer 1

2

In your constructor for BarGraph, you've declared local variables and ignored your class variables, so the class variables don't get assigned. Local variables take precedence over class variables. Remove the int to remove the declaration, and the class variables will get resolved properly.

Change

BarGraph(int num1, int num2, int num3, int num4, int num5)
{
    int n1 = num1;
    int n2 = num2;
    int n3 = num3;
    int n4 = num4;
    int n5 = num5;
}

to

BarGraph(int num1, int num2, int num3, int num4, int num5)
{
    n1 = num1;
    n2 = num2;
    n3 = num3;
    n4 = num4;
    n5 = num5;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time out of your day to answer such a simple minded question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.