Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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(600, 400);
  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. 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};
  38. 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};
  39.  
  40. public DrawBars(Dimension dimension) {
  41. setSize(dimension);
  42. setPreferredSize(dimension);
  43. addMouseListener(this);
  44.  
  45. }
  46.  
  47. @Override
  48. public void paintComponent(Graphics g) {
  49. Graphics2D g2 = (Graphics2D)g;//g2 is the graphics object that we need to use
  50. //to draw things to the screen
  51. Dimension d = getSize();
  52. //create a background
  53. g2.setColor(Color.white);
  54. g2.fillRect(0, 0, d.width, d.height);
  55. /**/
  56.  
  57. //hat
  58. Color purple= new Color(102,0, 102); //instance variable purple
  59. g2.setColor(purple);
  60. //philly rain bar graphs
  61. g2.fillRect(25,100,20,(int)(phila[0]*30));
  62. g2.fillRect(65,100,20,(int)(phila[1]*30));
  63. g2.fillRect(105,100,20,(int)(phila[2]*30));
  64. g2.fillRect(145,100,20,(int)(phila[3]*30));
  65. g2.fillRect(185,100,20,(int)(phila[4]*30));
  66. g2.fillRect(225,100,20,(int)(phila[5]*30));
  67. g2.fillRect(265,100,20,(int)(phila[6]*30));
  68. g2.fillRect(305,100,20,(int)(phila[7]*30));
  69. g2.fillRect(345,100,20,(int)(phila[8]*30));
  70. g2.fillRect(385,100,20,(int)(phila[9]*30));
  71. g2.fillRect(425,100,20,(int)(phila[10]*30));
  72. g2.fillRect(465,100,20,(int)(phila[11]*30));
  73.  
  74. Color green = new Color(10,255, 102); //instance variable purple
  75. g2.setColor(green);
  76. x = 5;
  77. //seattle Bar graphs
  78. g2.fillRect(5,100,20,(int)(seattle[0]*30));
  79. g2.fillRect(45,100,20,(int)(seattle[1]*30));
  80. g2.fillRect(85,100,20,(int)(seattle[2]*30));
  81. g2.fillRect(125,100,20,(int)(seattle[3]*30));
  82. g2.fillRect(165,100,20,(int)(seattle[4]*30));
  83. g2.fillRect(205,100,20,(int)(seattle[5]*30));
  84. g2.fillRect(245,100,20,(int)(seattle[6]*30));
  85. g2.fillRect(285,100,20,(int)(seattle[7]*30));
  86. g2.fillRect(325,100,20,(int)(seattle[8]*30));
  87. g2.fillRect(365,100,20,(int)(seattle[9]*30));
  88. g2.fillRect(405,100,20,(int)(seattle[10]*30));
  89. g2.fillRect(445,100,20,(int)(seattle[11]*30));
  90.  
  91.  
  92. //display Text
  93. g2.setColor(purple);
  94. g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  95. g2.drawString("Philadelphia" , 10,30);//text to display, x and y coordinates
  96. g2.setColor(green);
  97. g2.drawString("Seattle" , 200,30);
  98.  
  99. }
  100. public void mousePressed(MouseEvent e) {
  101. x = e.getX();
  102. y = e.getY();
  103.  
  104. repaint();//updates the paint method
  105. }
  106.  
  107. public void mouseReleased(MouseEvent e) {
  108. }
  109.  
  110. public void mouseEntered(MouseEvent e) {
  111. }
  112.  
  113. public void mouseExited(MouseEvent e) {
  114. }
  115.  
  116. public void mouseClicked(MouseEvent e) {
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement