Grafico de barras horizontal

2 respostas
PePeLeGaL

o que ha de errado com o programa abaixo ? Por que ele nao esta
gerando o grafico de barras horizontal ? Quando eu coloco valores especificos ele funciona, mas quando eu generalizo como abaixo para o usuario entrar com valores no intervalo dado, entao ele nao funciona :cry:

import javax.swing.*;

public class GraficoTeste
{
	public static void main(String args[])
	{
		String input1 = JOptionPane.showInputDialog("value 1","enter value 1");
		
		int in1 = Integer.parseInt(input1);
		
		String input2 = JOptionPane.showInputDialog("value 2","enter value 2");

		int in2 = Integer.parseInt(input2);

		String input3 = JOptionPane.showInputDialog("value 3","enter value 3");

		int in3 = Integer.parseInt(input3);
		
		String input4 = JOptionPane.showInputDialog("value 4","enter value 4");

		int in4 = Integer.parseInt(input4);						
		
		String input5 = JOptionPane.showInputDialog("value 5","enter value 5");								
	
		int in5 = Integer.parseInt(input5);
			
		
		Grafico gr = new Grafico(in1,in2,in3,in4,in5);
		
		JFrame app = new JFrame();
		
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.add(gr);
		app.setSize(800, 800);
		app.setVisible(true);

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

public class Grafico extends JPanel
{
	int choice1,
		 choice2,
		 choice3,
		 choice4,
		 choice5;
	
	public Grafico(int c1, int c2, int c3, int c4, int c5)
	{
		choice1 = c1;
		choice2 = c2;
		choice3 = c3;
		choice4 = c4;
		choice5 = c5;
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		if(choice1 >= 1 && choice1 <= 500)			
			g.drawRect(0, 10, choice1, 20);
			
		else if(choice2 >= 1 && choice2 <= 500)
			g.drawRect(0, 30, choice2, 20);
			
		else if(choice3 >= 1 && choice3 <= 500)				
			g.drawRect(0, 50, choice3, 20);
			
		else if(choice4 >= 1 && choice4 <= 500)				
			g.drawRect(0, 70, choice4, 20);
			
		else if(choice5 >= 1 && choice5 <= 500)					
			g.drawRect(0, 90, choice5, 20);
	}
}

2 Respostas

acopiara

Sua lógica que não foi muito legal, por que se o valor1 for >=1 e <=500
so entra na primeira condicão. Você poderia fazer assim:

public void paintComponent(Graphics g)
 	{
 		super.paintComponent(g);
 		
 	if(choice1 >= 1 && choice1 <= 500)			
 			g.drawRect(0, 10, choice1, 20);
 			
 	 if(choice2 >= 1 && choice2 <= 500)
 			g.drawRect(0, 30, choice2, 20);
 			
 	if(choice3 >= 1 && choice3 <= 500)				
 			g.drawRect(0, 50, choice3, 20);
 			
 	if(choice4 >= 1 && choice4 <= 500)				
 			g.drawRect(0, 70, choice4, 20);
 			
 	 if(choice5 >= 1 && choice5 <= 500)					
 			g.drawRect(0, 90, choice5, 20);
 	}

Valeu

PePeLeGaL

eh mesmo :lol: obrigado pela ajuda

Criado 24 de março de 2007
Ultima resposta 24 de mar. de 2007
Respostas 2
Participantes 2