Results 1 to 3 of 3
  1. #1
    cms370 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 2D Graph Repaint Issue

    Hello all...newb question. Please bare with me.

    I am trying to build a simple graph that will plot array values. I would like for this graph to update so that I have a "live" graph.

    I have built something (starting with a very generic 2d graphics example) that will plot one array, wait for 2 seconds, then plot the other array. Everything plots as it should, but the old data remains over top of the new data, until I resize the window. Then everything is as it should be.

    Could someone explain why repaint() does not work? I saw that revalidate() should be used in these situations, but this only applies to JPanels??

    Thanks for the help.

    Here is the code:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;


    public class JustaFrame2 extends JFrame {

    public int[] plot_x;
    public int[] plot_y;

    public JustaFrame2(int[] data_x, int[] data_y){
    super();
    plot_x = data_x;
    plot_y = data_y;
    }

    public void paint(Graphics g){

    //set preliminary
    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.red);
    // fill Ellipse2D.Double

    for(int i=0;i<5;i++) {
    g2.fill (new Ellipse2D.Double(plot_x[i], plot_y[i], 10, 10));
    }

    }

    public static void main(String[] args) throws InterruptedException {

    int[] plot_x = {500,400,300,200,100};
    int[] plot_y = {100,200,300,400,500};
    int[] new_data = {100,200,300,400,500};
    JustaFrame2 frame = new JustaFrame2(plot_x,plot_y);
    frame.setSize(600,600);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    Thread.sleep(2000);
    System.out.println("New Data");
    System.arraycopy(new_data,0,plot_x,0,new_data.leng th);
    System.arraycopy(new_data,0,plot_y,0,new_data.leng th);
    frame.repaint();
    }
    }

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

    Default Re: 2D Graph Repaint Issue

    Please read a tutorial on drawing in Swing before doing this as you're doing several things wrong, things that a good tutorial would have shown you was wrong early on. For instance, you almost never draw directly in a top level container, but rather in a component derived from JComponent such as a JPanel. Also you should draw in the JPanel's paintComponent method and should call the super's method first in your override, but again, the tutorials will show you all of this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: 2D Graph Repaint Issue

    1. Unrelated to the posted problem, but do not set the defaultCloseOperation or otherwise change the state of a component ion a painting method override.
    2. Don't attempt custom painting in a top level window. Learn how to do it correctly here: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  2. Problem with repaint();
    By dunafrothint in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2010, 12:33 AM
  3. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 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
  •