SHARE
TWEET

Untitled

a guest Jul 16th, 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(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.         double[] phila = {28.5,60.2,47.4,39.4,52.1,33.4,30.6,41.1,83.5,30.8,90.3,63.8};
  38.         double[] seattle = {81.2,21.6,24.4,56.9,1.2,6.3,0.5,2,9.8,37.8,54.2,60.8};
  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.            
  58.             Color purple= new Color(102,0, 102); //instance variable purple
  59.             g2.setColor(purple);
  60.             //philly rain bar graphs
  61.            
  62.             Color green = new Color(10,255, 102); //instance variable purple
  63.             g2.setColor(green);
  64.             x = 5;
  65.             //seattle Bar graphs
  66.            
  67.    
  68.  
  69.             //display Text
  70.             g2.setColor(purple);
  71.             g2.setFont (new Font("TimesRoman", Font.PLAIN, 20));
  72.             g2.drawString("Philadelphia" ,  10,30);//text to display, x and y coordinates
  73.             g2.setColor(green);
  74.             g2.drawString("Seattle" ,  200,30);
  75.             int distance = 10;
  76.             int Sdistance = 30;
  77.        
  78.             for (int x = 0; x < phila.length; x++) {
  79.              g2.setColor(purple);
  80.              
  81.              int height = (200-(int)phila[x]);
  82.              
  83.              g2.fillRect(distance, height , 20, (int)phila[x]);
  84.              
  85.              distance = (distance + 65);
  86.              
  87.  
  88.             }
  89.             for (y = 0; y < seattle.length; y++) {
  90.              g2.setColor(green);
  91.              
  92.              int Sheight = (200-(int)seattle[y]);
  93.              
  94.              g2.fillRect(Sdistance, Sheight, 20, (int)seattle[y]);
  95.              
  96.              Sdistance = (Sdistance + 65);  
  97.             }
  98.            
  99.            
  100.  
  101.         }
  102.         public void mousePressed(MouseEvent e) {
  103.             x = e.getX();
  104.             y = e.getY();
  105.  
  106.             repaint();//updates the paint method
  107.         }
  108.  
  109.         public void mouseReleased(MouseEvent e) {
  110.         }
  111.  
  112.         public void mouseEntered(MouseEvent e) {
  113.         }
  114.  
  115.         public void mouseExited(MouseEvent e) {
  116.         }
  117.  
  118.         public void mouseClicked(MouseEvent e) {
  119.         }
  120.     }
  121. }
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
 
Top