Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /**
  2. * This program draws a bargraph comparing the rainfall every month in Philly and Seattle
  3. *
  4. * @Maria Abdelazim
  5. * @07/09/19
  6. */
  7.  
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.WindowConstants;
  11. import java.awt.Dimension;
  12. import java.awt.Color;
  13. import java.awt.Graphics;
  14. import java.awt.Graphics2D;
  15. import java.awt.Point;
  16. import java.awt.event.MouseListener;
  17. import java.awt.event.MouseEvent;
  18. import java.awt.Font;
  19. import java.awt.FontMetrics;
  20.  
  21. public class BarGraph {
  22. private JFrame frame;
  23.  
  24. public BarGraph() {
  25. frame = new JFrame("Bar Graph");
  26. frame.setSize(600, 400);
  27. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  28. frame.setPreferredSize(frame.getSize());
  29. frame.add(new DrawBars(frame.getSize()));
  30. frame.pack();
  31. frame.setVisible(true);
  32. }
  33.  
  34. public static void main(String... argv) {
  35. new BarGraph();
  36. }
  37.  
  38. public static class DrawBars extends JPanel implements MouseListener {
  39. /*
  40. * Declare Class Variables Here
  41. */
  42. int x = 20;
  43. int y = 200;
  44. double[] philly = {2.85,6.02,4.74,3.94,5.21,3.34,3.06,4.11,8.35,3.08,9.03,6.38};
  45. 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};
  46.  
  47. public DrawBars(Dimension dimension) {
  48. setSize(dimension);
  49. setPreferredSize(dimension);
  50. addMouseListener(this);
  51.  
  52. }
  53.  
  54. @Override
  55. public void paintComponent(Graphics g) {
  56. Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
  57. //to draw things to the screen
  58. Dimension d = getSize();
  59. //create a background
  60. g2.setColor(Color.white);
  61. g2.fillRect(0, 0, d.width, d.height);
  62. /**/
  63.  
  64. //hat
  65. Color purple= new Color(102,0, 102); //instance variable purple
  66. g2.setColor(purple);
  67. //philly rain bar graphs
  68. for (int i = 0; i < philly.length; i++) {
  69. double height = philly[i] * 10;
  70. double y = 200-height;
  71. g2.fillRect(x, (int)y, 5, (int)height);
  72. x += 15;
  73. }
  74.  
  75. Color green = new Color(10,255, 102); //instance variable purple
  76. g2.setColor(green);
  77. //seattle Bar graphs
  78. x = 15;
  79. for (int i = 0; i < seattle.length; i++) {
  80. double height = seattle[i] * 10;
  81. double y = 200-height;
  82. g2.fillRect(x, (int)y, 5, (int)height);
  83. x += 15;
  84. }
  85.  
  86.  
  87. //display Text
  88. g2.setColor(purple);
  89. g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  90. g2.drawString("Philadelphia" , 10,30);//text to display, x and y coordinates
  91. g2.setColor(green);
  92. g2.drawString("Seattle" , 200,30);
  93.  
  94. }
  95. public void mousePressed(MouseEvent e) {
  96. x = e.getX();
  97. y = e.getY();
  98.  
  99. repaint();//updates the paint method
  100. }
  101.  
  102. public void mouseReleased(MouseEvent e) {
  103. }
  104.  
  105. public void mouseEntered(MouseEvent e) {
  106. }
  107.  
  108. public void mouseExited(MouseEvent e) {
  109. }
  110.  
  111. public void mouseClicked(MouseEvent e) {
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement