DZone Forums
Go Back   DZone Forums > Community > Tools & IDEs > Eclipse
Reload this Page how to repaint cavas dynamiclly with respect to TIMER
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: May 2009
Lightbulb how to repaint cavas dynamiclly with respect to TIMER - 06-26-2009, 08:12 AM

hi,

i had a canvas which consists of xy-chart which had both line and shape render.
my chart is a dynamic one for every sec some set of points will come , so enitre chart need to be redraw w.r.t timer .

My issue is here package org.jfree.chart.demo;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.Timer;


/**
* A simple demonstration of the {@link XYLineAndShapeRenderer} class.
*/
public class TestDynChart1 extends ApplicationFrame {

private RealTimeXYDataset dataset;
Shell shell;
static TestDynChart1 demo;
private JFreeChart chart;
ChartComposite composite;
private int counter = 0;

public TestDynChart1(String title) {

super(title);
final Display display = new Display();
shell = new Shell(display);
shell.setSize(720, 500);
shell.setLayout(new FillLayout());
dataset = createDataset();
chart = createRTChart(dataset);
// Note: must enable using buffer. Otherwise, only two points are drawn.
composite = new ChartComposite(shell, SWT.NONE, chart, true);

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
if (shell.isDisposed()) {
System.exit(0);
}
}
}

public void refresh() {
dataset.updateValue();
counter++;

// adjust range
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis xaxis = plot.getDomainAxis();

Range range = xaxis.getRange();
double lower = dataset.getDomainLowerBound(false);
double upper = dataset.getDomainUpperBound(false);

if (Double.isNaN(lower) || !xaxis.isAutoRange())
return;

if (range.contains(upper))
return;

// if fixed auto range, then derive lower bound...
double fixedAutoRange = xaxis.getFixedAutoRange();

if (fixedAutoRange > 0.0) {
double margin = xaxis.getUpperMargin();

// margin is defined
if (margin > 0.001) {
upper = upper + margin * fixedAutoRange;
}

lower = upper - fixedAutoRange;
}

// attach the new range
xaxis.setRange(new Range(lower, upper), false, false);
dataset.restore();
composite.refresh();
}

/**
* Creates a sample dataset.
* * @return A sample dataset.
*/
private RealTimeXYDataset createDataset() {
RealTimeXYDataset series = new RealTimeXYDataset(new IDataSource() {

public double getParamValue(String param) {
// Just have a random number.
if (param.equals("X"))
return counter;

return Math.random();
}
});

series.setBufferSize(10000);
series.setXParam("X", "TBD");
series.addSeriesParam("Y1", "Y1");
series.addSeriesParam("Y2", "Y2");
return series;
}

private XYItemRenderer renderer1;

private DataGenerator dataGenerator;

private JFreeChart createRTChart(XYDataset dataset) {
// parent plot... X Axis
ValueAxis xaxis = new NumberAxis();
xaxis.setRange(0.0, 100);
xaxis.setAutoRange(true);
xaxis.setFixedAutoRange(100);


xaxis.setLowerMargin(0.1); // 10% margin
xaxis.setUpperMargin(0.1);

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,
false);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
NumberAxis rangeAxis = new NumberAxis();
XYPlot plot = new XYPlot(dataset, xaxis, rangeAxis, renderer);

JFreeChart chart = new JFreeChart(null, null, plot, false);

TextTitle ttitle = new TextTitle("Scroll Plot Demo");

chart.setTitle(ttitle);
chart.setAntiAlias(true);

dataGenerator = new DataGenerator(250);
dataGenerator.start();
return chart;
}


class DataGenerator extends Timer implements ActionListener , SelectionListener {

Control control;

DataGenerator(int interval) {
super(interval, null);
addActionListener(this);
}

public void actionPerformed(ActionEvent event) {

refresh();
repaint();
//composite.reedraw(true);
//control.update();
}
}

public static void main(String[] args) {

demo = new TestDynChart1(
" swt Realtime XY Series Demo");
demo.pack();

demo.setVisible(true);


}
}
in above ActionPerformed class repaint method is not working ,
actually repaint is an awt method so i want to change it into swt method .
is there any alternative for it

please if anyone know help me....

...sorry for poor english

Thnaks,
anirudh.k
Reply With Quote
  (#2 (permalink)) Old
Active Contributor
 
Posts: 62
Thanks: 0
Thanked 6 Times in 6 Posts
Join Date: Jun 2009
Default 06-30-2009, 05:32 AM

In SWT, the Display provide several methods ,for example:
timerExec (int milliseconds, Runnable runnable)
syncExec (Runnable runnable)
asyncExec (Runnable runnable)

You need to write one Runnable and draw you curves in the SWT Canvas in the run() method.
If use the first method, you only need to set the value of ‘ milliseconds’, it will repaint the Canvas in timer.
The last two methods, you can use ScheduledExecutorService to excute the Runable in timer,
the methods is scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);

You may find some swt sinnepts in the following link
SWT Snippets
http://dev.eclipse.org/viewcvs/index...Snippet16.java


www.eclaxy.com
let's perfect the eclipse!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
timer in swt anirudh_kaki@yahoo.com Eclipse 1 06-29-2009 04:42 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com