SHARE
TWEET

Untitled

a guest Jul 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = 100;
  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.             for(double i:phila) {
  62.             g2.fillRect(x, y, 5, (int) i*10);
  63.             x+=10;
  64.             }
  65.            
  66.             x=25;
  67.             Color green = new Color(10,255, 102); //instance variable purple
  68.             g2.setColor(green);
  69.            
  70.             //seattle Bar graphs
  71.             for(double j:seattle) {
  72.             g2.fillRect(x, y, 5, (int) j*10);
  73.             x+=10;
  74.             }
  75.            
  76.            
  77.    
  78.  
  79.             //display Text
  80.             g2.setColor(purple);
  81.             g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  82.             g2.drawString("Philadelphia" ,  10,30);//text to display, x and y coordinates
  83.             g2.setColor(green);
  84.             g2.drawString("Seattle" ,  200,30);
  85.  
  86.         }
  87.         public void mousePressed(MouseEvent e) {
  88.             x = e.getX();
  89.             y = e.getY();
  90.  
  91.             repaint();//updates the paint method
  92.         }
  93.  
  94.         public void mouseReleased(MouseEvent e) {
  95.         }
  96.  
  97.         public void mouseEntered(MouseEvent e) {
  98.         }
  99.  
  100.         public void mouseExited(MouseEvent e) {
  101.         }
  102.  
  103.         public void mouseClicked(MouseEvent e) {
  104.         }
  105.     }
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top