Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree8Likes

Thread: How to plot graph in java for given samples

  1. #21
    ramadread is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

  2. #22
    ramadread is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Default

    Hello, I come from Chile (This forum it's very famous here, for help people like me), congratulations for your forum, but I have some problems with Graphics2D, looking at this forum have obtained help but my problem is this:

    I must programme an algorithm of administration of secondary memory

    with the algorithms,NO PROBLEMS but with the graphics .. I cannot use YOUR example



    This is my source code :
    /*IF YOU WANT MY ALGOROTHM, NO PROBLEMS (y) POST YOU e-MAIL ;) or IF SOMEONE FIND SOME ERROR TELL ME PLEASE*/
    C-LOOK
    Java Code:
    class CLook {
    public int  Preparar(int a,int b){
        int c =a - b;
        if(c<0)c*=-1;
        return c;
        }
        public CLook(int []sectores,int Inicial) {
    System.out.println("Algoritmo CLook");
    int m=1;
    int Maximo = 0; // count for all Movements
    int PosicionActual = Inicial; //Start
    Arrays.sort(sectores);  //Arranging the array
    int []b = new int [ (sectores.length+1) ];  // this Array must be graphicated
    // in  GraphicsArray class (Graphics.java)
    b[0]=Inicial;
    
    for(int i=0;i<sectores.length;i++){
        if(sectores[i]>PosicionActual){
        System.out.println("Servido : "+sectores[i]); // Done Request sectores[i]
        b[m]=sectores[i];
        m++;
        Maximo+=Preparar(PosicionActual,sectores[i]);//total Movement
        PosicionActual=sectores[i];//Changing the beginning
        }
    }
    for(int j=0;sectores[j]<Inicial;j++){
        System.out.println("Servido : "+sectores[j]); // Done Request sectores[i]
        b[m]=sectores[i];
        m++;
        Maximo+=Preparar(PosicionActual,sectores[j]);//total Movement
        PosicionActual=sectores[j]; //Changing the beginning
    }
    System.out.println("Movimiento Total : "+Maximo+" cilindros" );//total Movement
    
    
    /* This is my problem, I need you help, please help , THANK YOU*/
    
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Graphincs());
            f.setSize(600,400);
            f.setLocation(200,200);
            f.setVisible(true);
    
    
        }
    
    }
    this example use a pre-defined array

    but i need send the array (int []b) .. HOW? i don't know :(

    Only I want that the class Graphics.java receives an array and draws it like in the example

    PLEASE HELP





    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class GraphingData extends JPanel {
        int[] data = {
            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)/(data.length-1);
            double scale = (double)(h - 2*PAD)/getMax();
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < data.length-1; i++) {
                double x1 = PAD + i*xInc;
                double y1 = h - PAD - scale*data[i];
                double x2 = PAD + (i+1)*xInc;
                double y2 = h - PAD - scale*data[i+1];
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
            // Mark data points.
            g2.setPaint(Color.red);
            for(int i = 0; i < data.length; i++) {
                double x = PAD + i*xInc;
                double y = h - PAD - scale*data[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 < data.length; i++) {
                if(data[i] > max)
                    max = data[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);
        }
    }
    HELP ME ! XD , THANK YOU VERY MUCH

    IF MY ENGLISH IS A SH*T SORRY :(

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

    Default

    I want that the class Graphics.java receives an array and draws it like in the example
    Java Code:
    public class CLook {
        // declare array "b" as a member variable so
        // you can access it after your calculations
        int[] b;
        ...
        public CLook(int[] sectores, int Inicial) {
            ...
            instantiate array "b"
            b = new int [ (sectores.length+1) ];
            ...
            System.out.println("Movimiento Total : "+Maximo+" cilindros" );
        }
    
        public static void main(String[] args) {
            int[] sectores = ...
            int Inicial = ...
            CLook clook = new CLook(sectores, Inicial);
            int[] data = clook.b;
            GraphingData graph = new GraphingData(data);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(graph);
            f.setSize(600,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class GraphingData extends JPanel {
        int[] data;
        final int PAD = 20;
    
        public GraphingData(int[] data) {
            this.data = data;
        }
    
        ... as before ...

  4. #24
    ramadread is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks man !

    i'll try you code ... thanks for you help

    **********

    Thanks man it's works

    thanks you so much

    i'm using for FCFS , SSTF, LOOK , CLOOK SCAN AND CSCAN ... tomorow i upload my work for share it..
    Last edited by ramadread; 07-21-2009 at 06:51 AM.

  5. #25
    ramadread is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi , i need you help .. again

    my problem is


    when i create a listener:

    I need that when I do click on a button the graph appears .. but many mistakes go out for me

    listener :
    Java Code:
    private void botonFCFSActionPerformed(ActionEvent evt) {//GEN-FIRST:event_botonFCFSActionPerformed
            // TODO add your handling code here:
            ManejadorBoton manejador = new ManejadorBoton();
          botonCLOOK.addActionListener( manejador);
    }
    AND

    Java Code:
     private class ManejadorBoton implements ActionListener
       {
          
          public void actionPerformed( ActionEvent evento )
          {
             CLook clook = new CLook(sectores, Inicial);
            int[] data = clook.b;
            GraphingData graph = new GraphingData(data);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(graph);
            f.setSize(600,400);
            f.setLocation(200,200);
            f.setVisible(true);
          }
    
       }
    why it's happen??

    sorry for my english but the cafe and Code make stupid xd ajjaja

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

    Default

    It is not a good idea to be creating JFrames from inside an ActionListener. We generally have only a single JFrame for an application. When you press a button you can create a component and add it to your JFrame in something like a JTabbedPane or show it in a JDialog.
    The Swing tutorial is a good place to start with this: Trail: Creating a GUI with JFC/Swing.

  7. #27
    student2010 is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default urgent plzz

    how to save the plot graph in java

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 06:49 PM
  2. Java chart & graph lib
    By saturdaybugs in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 01:59 PM
  3. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 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. Java cert exam samples
    By orchid in forum New To Java
    Replies: 5
    Last Post: 06-04-2007, 09:33 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
  •