Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    PROBLEM SOLVED!! Fubarable was correct. the problem was about the re-initialization of the for loop.

    thanx Fubarable. but plz suggest where should I place the parsing and analysing within my application.

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

    Default

    Consider using an ArrayList of either Point (if ints) or Point2D (if doubles). In the button's action listener, consider creating a background thread, such as a SwingWorker object, and using your parsing and all to fill the ArrayList of points, and then call repaint() on the JPanel when done. Inside of the paintComponent method, you'd iterate through the arraylist plotting the points.

    But this is a more minor issue addressing program responsiveness, not your primary error. You've yet to address the for-loop issues that I've discussed beginning in reply #8.

    edit: nevermind, you've finally seen and addressed it. Note, that it was solved using an SSCCE that I created myself in my own time:

    Java Code:
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    @SuppressWarnings("serial")
    public class JPanel00 extends JPanel {
      private static final double X_INIT = -1080;
      private Random random = new Random(); // !!
      private boolean flag = false;
      private char c[];
      //public static double x = X_INIT;
      private int x_origin = 640, y_origin = 320;
      private double x_scale = 1, y_scale = 100;
    
      public JPanel00() {
        initComponents();
    
        sketchGraphBtn.addActionListener(new ActionListener() {
    
          public void actionPerformed(ActionEvent event) {
            if (event.getSource() == sketchGraphBtn) {
              flag = true;
              repaint();
            }
          }
        });
    
      }
    
      @Override
      public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
    
        g.drawLine(x_origin, 0, x_origin, 770);
        g.drawLine(0, y_origin, 1280, y_origin);
    
        if (flag) {
          double x = X_INIT;
          double y = 0;
          double x_lookback = x; // !!
          double y_lookback = y; // !! ??
    
          //!! for (x += 0.05; x <= 1080; x += 0.05) {
          for (x = X_INIT; x <= 1080; x += 0.05) {
            y += 0.00008 * random.nextDouble(); // !!
            
            int x1 = (int) (x_scale * x_lookback) + x_origin;
            int y1 = (int) (-y_scale * y_lookback) + y_origin;
            int x2 = (int) (x_scale * x) + x_origin;
            int y2 = (int) (-y_scale * y) + y_origin;
     
            System.out.printf("[%d, %d, %d, %d]%n", x1, y1, x2, y2);
            g.drawLine(x1, y1, x2, y2);
    
            x_lookback = x;
            y_lookback = y;
          }
        }
      }
    
      private void initComponents() {
        sketchGraphBtn = new JButton("Sketch Graph");
        JPanel btnPanel = new JPanel();
        btnPanel.add(sketchGraphBtn);
        
        setLayout(new BorderLayout());
        add(btnPanel, BorderLayout.SOUTH);
      }
    
      private javax.swing.JButton sketchGraphBtn;
    
      public static void main(String args[]) {
        JFrame frame = new JFrame("Two-Dimensional Graph Sketcher");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JPanel00());
        frame.setSize(1280, 770);
        frame.setVisible(true);
      }
    }
    and this was only done because you refused to create one yourself. Next time, please create an SSCCE when asked, OK?

  3. #23
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to plot graph in java for given samples
    By annesteve31 in forum New To Java
    Replies: 27
    Last Post: 11-29-2009, 09:27 PM
  2. Replies: 7
    Last Post: 11-16-2009, 01:22 AM
  3. 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
  4. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 02:57 PM
  5. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 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
  •