Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. /*
  2. * Draws a bar graph for rainfall
  3. *
  4. * Gaileen
  5. *
  6. * 7/7/19
  7. */
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.WindowConstants;
  12. import java.awt.Dimension;
  13. import java.awt.Color;
  14. import java.awt.Graphics;
  15. import java.awt.Graphics2D;
  16. import java.awt.Font;
  17.  
  18. public class BarGraph {
  19. private JFrame frame;
  20.  
  21. public BarGraph() {
  22. frame = new JFrame("Bar Graph");
  23. frame.setSize(600, 400);
  24. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  25. frame.setPreferredSize(frame.getSize());
  26. frame.add(new DrawBars(frame.getSize()));
  27. frame.pack();
  28. frame.setVisible(true);
  29. }
  30.  
  31. public static void main(String... argv) {
  32. new BarGraph();
  33.  
  34. }
  35.  
  36. public static class DrawBars extends JPanel {
  37.  
  38. int x = 20;
  39. int y = 200;
  40. double[] phila = { 2.85, 6.02, 4.74, 3.94, 5.21, 3.34, 3.06, 4.11, 8.35, 3.08, 9.03, 6.38 };
  41. double[] seattle = { 8.12, 2.16, 2.44, 5.69, 0.12, 0.63, 0.05, 0.2, 0.98, 3.78, 5.42, 6.08 };
  42.  
  43. public DrawBars(Dimension dimension) {
  44. setSize(dimension);
  45. setPreferredSize(dimension);
  46.  
  47. }
  48.  
  49. @Override
  50. public void paintComponent(Graphics g) {
  51. Graphics2D g2 = (Graphics2D) g;
  52. Dimension d = getSize();
  53.  
  54. // background
  55. g2.setColor(Color.white);
  56. g2.fillRect(0, 0, d.width, d.height);
  57.  
  58. // philly rain bar graphs
  59. Color purple = new Color(102, 0, 102);
  60. g2.setColor(purple);
  61. for (int i = 0; i < phila.length; i++) {
  62. double p_num = phila[i] * 10;
  63.  
  64. g2.fillRect(x, y - (int) p_num, 10, (int) p_num);
  65. x = x + 50;
  66.  
  67. }
  68.  
  69. // seattle bar graphs
  70. Color green = new Color(10, 255, 102);
  71. g2.setColor(green);
  72. x = 10;
  73. for (int i = 0; i < seattle.length; i++) {
  74. double s_num = seattle[i] * 10;
  75.  
  76. g2.fillRect(x, y - (int) s_num, 10, (int) s_num);
  77. x = x + 50;
  78.  
  79. }
  80.  
  81. // key
  82. g2.setColor(purple);
  83. g2.setFont(new Font("TimesRoman", Font.PLAIN, 20));
  84. g2.drawString("Philadelphia", 10, 30);
  85. g2.setColor(green);
  86. g2.drawString("Seattle", 200, 30);
  87.  
  88. }
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement