Advertisement
Guest User

Untitled

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