|
|
|
<< Back public void actionPerformed(ActionEvent e) { for(int x=0; x < size; x++) { labels[x] = input[x].getText(); Double temp = Double.valueOf(labels[x]); number[x] = temp.doubleValue(); } // scale graphs as needed max = number[0]; for(int x=1; x < size; x++) if(max < number[x]) max = number[x]; scaleFactor = 450/max; for(int x=0; x < size; x++) number[x] *= scaleFactor; valuesEntered = true; repaint(); } public void paint(Graphics g) { // draw axises g.drawLine(50,95,50,100+size*30); g.drawLine(50,100+size*30,500,100+size*30); // draw labels and bar graphs int y=100; for(int x=0; x < size; x++) { g.drawString("Value " + (x+1),5,y+17); g.setColor(new Color((float)Math.random(), (float)Math.random(), (float)Math.random())); g.fillRect(50,y,(int)Math.round(number[x]),25); g.setColor(Color.black); g.drawString(labels[x],55,y+15); y+=30; } // horizontal axis based on scaling value if(valuesEntered) { double tickSpace = max/10; double factor; factor = (int)(Math.log(tickSpace)/Math.log(10)); factor = Math.pow(10.0, factor); // NOW we will round to the nearest factor tickSpace = Math.round((float)(tickSpace/factor))*factor; double spacing; if (max <=10) spacing = 450/max; else spacing=45; if (tickSpace >5 && tickSpace <10) { tickSpace = 10; spacing = 4500/max; } for(int i = 0; i<=10; i++) { g.drawLine((int)(50+i*spacing),218,(int)(50+i*spacing),222); g.drawString(Integer.toString((int)(tickSpace*i)),(int)(50+i*spacing),235); } } } |