Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Draws a bar graph for rainfall
- *
- * Gaileen
- *
- * 7/7/19
- */
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Font;
- public class BarGraph {
- private JFrame frame;
- public BarGraph() {
- frame = new JFrame("Bar Graph");
- frame.setSize(600, 400);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setPreferredSize(frame.getSize());
- frame.add(new DrawBars(frame.getSize()));
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String... argv) {
- new BarGraph();
- }
- public static class DrawBars extends JPanel {
- int x = 20;
- int y = 200;
- 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 };
- 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 };
- public DrawBars(Dimension dimension) {
- setSize(dimension);
- setPreferredSize(dimension);
- }
- @Override
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- Dimension d = getSize();
- // background
- g2.setColor(Color.white);
- g2.fillRect(0, 0, d.width, d.height);
- // philly rain bar graphs
- Color purple = new Color(102, 0, 102);
- g2.setColor(purple);
- for (int i = 0; i < phila.length; i++) {
- double p_num = phila[i] * 10;
- g2.fillRect(x, y - (int) p_num, 10, (int) p_num);
- x = x + 50;
- }
- // seattle bar graphs
- Color green = new Color(10, 255, 102);
- g2.setColor(green);
- x = 10;
- for (int i = 0; i < seattle.length; i++) {
- double s_num = seattle[i] * 10;
- g2.fillRect(x, y - (int) s_num, 10, (int) s_num);
- x = x + 50;
- }
- // key
- g2.setColor(purple);
- g2.setFont(new Font("TimesRoman", Font.PLAIN, 20));
- g2.drawString("Philadelphia", 10, 30);
- g2.setColor(green);
- g2.drawString("Seattle", 200, 30);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement