Results 1 to 8 of 8
  1. #1
    abelah is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default graph + combo box

    hi all.

    let say i have this code

    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class GraphingData extends JPanel {
        int[] data1 = {
            21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
            61, 55, 48, 60, 49, 36, 38, 27, 20, 18
        };
        
        int[] data2 = {
            21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
            61, 55, 48, 60, 49, 36, 38, 27, 20, 18
        };
        
        int[] data3 = {
            21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
            61, 55, 48, 60, 49, 36, 38, 27, 20, 18
        };
        
        final int PAD = 20;
    
        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));
            // Draw labels.
            Font font = g2.getFont();
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics("0", frc);
            float sh = lm.getAscent() + lm.getDescent();
            // Ordinate label.
            String s = "data";
            float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
            for(int i = 0; i < s.length(); i++) {
                String letter = String.valueOf(s.charAt(i));
                float sw = (float)font.getStringBounds(letter, frc).getWidth();
                float sx = (PAD - sw)/2;
                g2.drawString(letter, sx, sy);
                sy += sh;
            }
            // Abcissa label.
            s = "x axis";
            sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
            float sw = (float)font.getStringBounds(s, frc).getWidth();
            float sx = (w - sw)/2;
            g2.drawString(s, sx, sy);
            // Draw lines.
            double xInc = (double)(w - 2*PAD)/(data1.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < data1.length-1; i++) {
                double x1 = PAD + i*xInc;
                double y1 = h - PAD - scale*data1[i];
                double x2 = PAD + (i+1)*xInc;
                double y2 = h - PAD - scale*data1[i+1];
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
            // Mark data points.
            g2.setPaint(Color.red);
            for(int i = 0; i < data1.length; i++) {
                double x = PAD + i*xInc;
                double y = h - PAD - scale*data1[i];
                g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
            }
        }
    
        private int getMax() {
            int max = -Integer.MAX_VALUE;
            for(int i = 0; i < data1.length; i++) {
                if(data1[i] > max)
                    max = data1[i];
            }
            return max;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GraphingData());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    i want to use combo box to change graph from data1, to data2 and to data3.
    i hope anyone can help.

    TQ

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Can you explain a bit more?
    Do you mean you want a GUI component to control which graph is displayed?
    What controls which graph is displayed now?
    YOu have it HARD CODED in the paint method. Parameterize that to another variable that is set to the graph you want to display.
    int[] displayThis = data1; // default to data1

    then later have the GUI (in a listener) change it:

    displayThis = <new datax>; // set which to display

  3. #3
    abelah is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Yes exactly like u said. i want to use combo box as my GUI component to control which graph to be displayed (data1, data2 or data3).

    Rite now in my code, there is nothing that controls which graph is displayed. only graph for default data1 will be used.

    I dun understand this part...

    int[] displayThis = data1; // default to data1

    then later change it:

    displayThis = <new datax>; // set which to display

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    To parameterize the access to the graph's data:
    1)Copy the first line of the above code to your program as a class variable.

    2)Change ALL references to data1 to displayThis in the paintComponent() method.

    Now changes to displayThis will be seen by the paint method

  5. #5
    abelah is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Is there anybody can give me hints on how to make this graph display 3 sets of data at one time in one graph?

    let say i have a var temperature. i want to plot temperature graph from 3 different node in one page.

    TQ
    Last edited by abelah; 10-13-2008 at 05:32 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Copy the three arrays into a single array and have the paint method use that array.

  7. #7
    abelah is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    still cannot catch. how can i copy 3 array into 1?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    create a new array big enough to hold all 3
    copy the first
    copy the second following the first
    copy the third following the second

Similar Threads

  1. Syntax for choice box(combo box)
    By NickkicN in forum New To Java
    Replies: 2
    Last Post: 08-14-2008, 10:26 PM
  2. Creating a non-editable combo box
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:45 PM
  3. How to place a combo box in a tool bar
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:09 PM
  4. how to add links (Action) to a combo box
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 07:04 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
  •