Results 1 to 4 of 4
  1. #1
    sacr83 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default How to draw a VERTICAL BAR GRAPH in JAVA

    hi all. I'm new to java 2D graphs and i want to draw a VERTICAL BAR GRAPH in java.So how i develop it?If you have class library or source code pls send me.The graph shuld be change every one hour.That's mean the graph shuld redraw after one hour.If you have any kind of java class library for VERTICAL BAR GRAPH please send me.

    **RegardZ**

    ChiNthAka

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class HiBars implements Runnable {
        HiBarPanel hiBarPanel = new HiBarPanel();
        Random seed = new Random();
        int DATA_MAX = 100;
        long delay = 20*1000;
    
        public HiBars() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public void run() {
            while(true) {
                hiBarPanel.setData(getData(), DATA_MAX);
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    break;
                }
            }
        }
    
        private int[] getData() {
            int[] data = new int[4];
            for(int i = 0; i < data.length; i++) {
                data[i] = seed.nextInt(DATA_MAX+1);
            }
            return data;
        }
    
        private JPanel getContent() {
            return hiBarPanel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new HiBars().getContent());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class HiBarPanel extends JPanel {
        int[] data;
        int maxValue = 10;
        final int PAD = 20;
    
        public void setData(int[] data, int maxValue) {
            this.data = data;
            this.maxValue = maxValue;
            System.out.printf("data = %s%n", java.util.Arrays.toString(data));
            repaint();
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            // Draw ordinate.
            g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
            // Draw abcissa.
            g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
            double xInc = (double)(w - 2*PAD)/data.length;
            double scale = (double)(h - 2*PAD)/maxValue;
            Path2D.Double path = new Path2D.Double();
            // Draw data.
            double x = PAD;
            double y = h - PAD;
            path.moveTo(x, y);
            for(int i = 0; i < data.length; i++) {
                x = PAD + i*xInc;
                y = h - PAD - scale*data[i];
                path.lineTo(x, y);
                path.lineTo(x+xInc, y);
            }
            path.lineTo(x+xInc, h-PAD);
            g2.setPaint(new Color(220,220,210));
            g2.fill(path);
            g2.setPaint(Color.blue);
            g2.draw(path);
        }
    }

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Why would you want to wait an hour that is completely ludicrous.
    My IP address is 127.0.0.1

  4. #4
    sacr83 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default

    I waite an hour,,,coZ i have existing system for mesure ADSL speeds.That system mesure ADSL speeds one by one hour.SO i have to draw a bar graph for that speeds.Thatz y i waite for an hour for drawing that graph.If you can help me to draw Bar graph coz its an urgent for me.

    Thanks a lot for your code

    AND

    path2D class is not avalable.So how i get that package.

Similar Threads

  1. Java chart & graph lib
    By saturdaybugs in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 01:59 PM
  2. how to draw an arrow mark using java swing
    By sandhyau in forum AWT / Swing
    Replies: 5
    Last Post: 02-07-2008, 11:52 AM
  3. what layout to use for vertical alignment?
    By dim_ath in forum AWT / Swing
    Replies: 6
    Last Post: 01-20-2008, 05:28 PM
  4. How to insert graph in java
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:38 PM
  5. how to draw in Java
    By Heather in forum AWT / Swing
    Replies: 2
    Last Post: 07-12-2007, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •