Advertisement
Guest User

Untitled

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