import java.util.Scanner; import javax.swing.JFrame; public class StockChart { static Scanner input = new Scanner(System.in); public static void main(String[] args){ System.out.print("Enter stock name: "); String name = input.nextLine().trim(); double[] p = getPrices(); System.out.print("Enter width of chart: "); int width = Integer.parseInt(input.nextLine()); System.out.print("Enter height of chart: "); int height = Integer.parseInt(input.nextLine()); JFrame frame = new JFrame("Chart for " + name); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); StockChartPanel panel = new StockChartPanel(p, width, height); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } //prompts the user to enter the stock prices and stores them in an array of double values private static double[] getPrices(){ double[] prices = new double[5]; for(int i = 0; i < 5; i++){ System.out.print("Enter "); switch(i){ case 0: System.out.print("Monday "); break; case 1: System.out.print("Tuesday "); break; case 2: System.out.print("Wednesday "); break; case 3: System.out.print("Thursday "); break; case 4: System.out.print("Friday "); break; } System.out.print("price: "); prices[i] = input.nextDouble(); input.nextLine(); } return prices; } }
panel class:
import javax.swing.JPanel; import java.awt.*; public class StockChartPanel extends JPanel{ double[] prices; int height, width; public StockChartPanel(double[] p, int h, int w){ prices = p; height = h; width = w; //the height is height+height/10 because there is a margin under the bars so I can write the days setPreferredSize(new Dimension(width, height+height/10)); } public void paintComponent(Graphics g){ int y = height/2; g.setColor(Color.RED); g.fillRect(0, height/2, width/5, height-(height/2)); g.setColor(Color.BLUE); /////////////////////////////////////this is where I'm having trouble/////////////////////////////////////////////////////////////////////////////////////// g.fillRect(width/5, (int)(y+(prices[0]/(height/2)*prices[1])), width/5, (int)(height - (y+(prices[0]/(height/2)*prices[1])))); } }
as you can probably see, I'm having a lot of trouble figuring out the y coordinate and the height of the second bar, let alone the other three. If anyone could help that would be swell.
Thanks a lot