Results 1 to 20 of 20
  1. #1
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default [SOLVED] Need help making 2d graph, with x and y

    nothing here anymore to see
    Last edited by kiduut; 01-31-2009 at 07:32 AM.

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

    Default

    Please give more details on just what you've done and what your stuck on. The more info you can give, the better folks can help you.

    Best of luck

  3. #3
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    nothing here anymore to see
    Last edited by kiduut; 01-31-2009 at 07:32 AM.

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

    Default

    So what I mean is what have you done/tried so far to make your plot?
    Last edited by Fubarable; 01-31-2009 at 04:30 AM.

  5. #5
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    nothing here anymore to see
    Last edited by kiduut; 01-31-2009 at 07:32 AM.

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

    Default

    So if I were you I'd do the following:
    1) Change the above code so that it is no longer static but a true OOPs class that can generate data points for another class when necessary
    2) Step away from the computer, get a few sheets of paper and a pencil and start brain-storming what classes will be necessary for my graphics program and start thinking about what states (fields) these classes will have and what behaviors (methods) they will have.
    3) If I didn't know graphics and Swing, I'd start going through the appropriate Sun tutorials, and then
    4) Use this outline and knowledge to create a plotting program.

    I think that the planning portion is the best part here as it's where your creative juices really come into full force. So why not have at it, and then come on back if you get stuck at a specific step.

    Best of luck.

  7. #7
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    hmm. where is point 0 and 0 on java plot?

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

    Default

    It's wherever you decide to place it (me, I'd place it in the middle of a JPanel), but I think that you may be getting ahead of yourself with that question. You need to plan the general behaviors of the classes before you start worrying about specifics.

  9. #9
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    nothing here anymore to see
    Last edited by kiduut; 01-31-2009 at 07:31 AM.

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

    Default

    As suggested above, please first read the sun graphics tutorial before attempting any graphics coding. Your code shows that you haven't read the tutorial yet, and so you don't have the knowledge framework for us to even begin trying to help you. This tutorial will give you the basics you'll need to be able to get started.

    Also, do your code in Swing not AWT to take full advantage of the latest enhancements of Java GUI.

    Also, do your code in a JPanel. Then when it works right, you can put into a JApplet.

    Again, best of luck.

    edit: I've looked at your code and it's time for you to fess up: you found that code online and posted it without understanding it, didn't you? I mean using RenderingHints before you know to override a paint (or for Swing paintComponent) method? Come on now, don't hurt yourself like this -- read the tutorials and learn to understand it. You'll thank me for this recommendation, trust me.
    Last edited by Fubarable; 01-31-2009 at 06:41 AM.

  11. #11
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Please, just little help. i dont have time for manuals atm:(

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

    Default

    Here's a link to the tutorial: Trail: 2D Graphics (The Java™ Tutorials)

    Please, just little help. i dont have time for manuals
    Not a good thing to say around here as:
    a) Your time management issues are just that, your issues not ours, and
    b) We like to help people learn, not cheat.

    Again, best of luck.

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

    Default

    Other suggestions:
    1) try to do most of your calculations outside of the paint or paintComponent method, especially the time-intensive Math-trigonometry methods. The painting methods needs to be short and fast, and so the calculations could be done in some initialization method. Obviously any calculations needed for the rendering of the graphics itself need to be in the painting methods.
    2) try not to put everything in one method. Otherwise you'll have a "God" method that tries to do too much and becomes impossible to debug or extend. Divide and conquer is the key.
    Last edited by Fubarable; 01-31-2009 at 07:10 AM.

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

    Default

    Another suggestion: again, do your plotting in a JPanel in one class, the calculating in another class, and the applet in a third class. Your plotting jpanel class could have this outline:
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import javax.swing.JPanel;
    
    public class DrawPanel extends JPanel
    {
      private int[] xArray;
      private int[] yArray;
      private int maxX, minX, maxY, minY; // domain and range of our data points
      
      // the trig calculations could be done in another class and then
      // the final resulting arrays passed to the DrawPanel when ready
      public DrawPanel(int[] xArray, int[] yArray)
      {
        this.xArray = xArray;
        this.yArray = yArray;
        
        // iterate through the points to get the max X and Y and mins.
      }
    
      @Override
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
        
        // use these guys and the max and min X and Ys to figure out
        // how to translate the data points so that they are well 
        // positioned in the panel
        int width = getWidth();
        int height = getHeight();
        
        // draw your x axis and y axis here
        
        // use a for loop to draw your x and  y points.  
        // You may wish to use small circles to give them some size
      }
      
    }
    and the applet could look something like this:
    Java Code:
    import javax.swing.JApplet;
    
    public class PlotApplet extends JApplet
    {
      public void init()
      {
        try
        {
          javax.swing.SwingUtilities.invokeAndWait(new Runnable()
          {
            public void run()
            {
              createGUI();
            }
          });
        }
        catch (Exception e)
        {
          System.err.println("createGUI didn't successfully complete");
        }
      }
    
      private void createGUI()
      {
        // you'll need to create a class to calculate your points
        PlotCalculatePoints calcPoints = new PlotCalculatePoints(); 
        int[] xArray = calcPoints.getXArray(); // extract data arrays from class
        int[] yArray = calcPoints.getYArray();
        
        // create JPanel and add data points
        DrawPanel drawPanel = new DrawPanel(xArray, yArray);
        
        // add to applet's contentPane
        getContentPane().add(drawPanel);
      }
    }
    Java Code:
    class PlotCalculatePoints
    {
    
      public int[] getXArray()
      {
        // TODO do calculations
      }
    
      public int[] getYArray()
      {
        // TODO do calculations
      }
      
    }

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i hate it when they do that. they think they can just rip off the ans and erase their tracks. Just wait till i find the cache data.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    that ungrateful sun of a witch. someone to add to the never-to-help list.

  17. #17
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    google didn't cache the page!! ran away codes...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    how do you find the cache data?

    edit: never mind

    again, I will do all in my power to see that he receives no more help here again.

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

    Default

    Some of his code looked like so:
    Java Code:
    public class plot extends Applet
    {
      private static final long serialVersionUID = 1L;
    
      public void joonista(Graphics g)
      {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        double t, s = 0.1, m, m2, k = 0;
        int a = 0, u;
    
        int w = getWidth();
        int h = getHeight();
    
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        // Calculate array size
        for (t = -10; t < 10; t = t + s)
        {
          a = a + 1;
        }
        // create array according to needed size
        double[] xarray = new double[a], yarray = new double[a];
        // calculate x and y, put them into array place a
        for (a = 0, t = -10; t < 10; t = t + s, a = a + 1)
        {
          xarray[a] = (10 * t) * Math.cos(t);
          yarray[a] = (10 * t) * Math.sin(t);
        }
        // find smallest number from x array
        m = xarray[0];
        int index = 0;
        for (u = 1; u < xarray.length; u++)
        {
          if (xarray[u] < m)
          {
            m = xarray[u];
            index = u;
          }
        }
        // find smallest number from y array
        m2 = yarray[0];
        int index1 = 0;
        for (u = 1; u < yarray.length; u++)
        {
          if (yarray[u] < m2)
          {
            m2 = yarray[u];
            index1 = u;
          }
        }
        // wich is smaller small number from y array or x array
        if (xarray[index] < yarray[index1])
        {
          k = xarray[index];
        }
        else
        {
          k = yarray[index1];
        }
        // subtract smallest number from all numbers in arrays so we dont have any
        // negative numbers
        for (a = 0, t = -10; t < 10; t = t + s, a = a + 1)
        {
          xarray[a] = xarray[a] - k;
          yarray[a] = yarray[a] - k;
        }
    
        // draw
        final int PAD = 20;
        g.setColor(Color.green);
        for (a = xarray.length - 2; a > 0; a = a - 1)
        {
          double x1 = PAD + a * xarray[a];
          double y1 = h - PAD - yarray[a];
          double x2 = PAD + (a + 1) * xarray[a];
          double y2 = h - PAD - yarray[a + 1];
          g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
        }
      }
    }

  20. #20
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Wink Brave New World

    Quote Originally Posted by angryboy View Post
    google didn't cache the page!! ran away codes...
    Cyber Security Industry Alliance (CSIA) - Press Release Search and Archive
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 02:57 PM
  2. graph + combo box
    By abelah in forum New To Java
    Replies: 7
    Last Post: 10-13-2008, 05:36 PM
  3. Bar Graph
    By Zosden in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2008, 06:52 AM
  4. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 PM
  5. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 09:19 PM

Posting Permissions

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