Results 1 to 3 of 3
Thread: 2D Graph Repaint Issue
- 11-16-2011, 03:30 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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();
}
}
-
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.
- 11-16-2011, 04:34 AM #3
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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
repaint every
By 3xpr1ment in forum AWT / SwingReplies: 10Last Post: 03-23-2010, 05:39 PM -
Problem with repaint();
By dunafrothint in forum AWT / SwingReplies: 8Last Post: 01-07-2010, 12:33 AM -
repaint problem
By amith in forum Java 2DReplies: 2Last Post: 07-01-2008, 12:10 AM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks