Hello DG
This sample code snippet shows how i can accomplish scrolling with Swing package. Could you please guide me to achieve this in JFreechart?
I had earlier posted query regarding" Scrolling and Scaling issues".
My graph class "NewGraphPanel.java" is:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class NewGraphPanel extends JViewport {
public static int DOT = 0;
public static int LINE = 1;
protected int dataSize;
protected int dataStep;
protected int graphType = DOT;
protected Vector dataVector;
public NewGraphPanel() {
dataSize = 10;
dataStep = 10;
dataVector = new Vector( dataSize );
}
public int getDataSize() {
return dataSize; }
public void setDataSize( int dataSize ) {
this.dataSize = dataSize;
repaint();
revalidate(); }
public void setDataStep( int stepSize ) {
dataStep = stepSize;
repaint(); }
public int getDataStep() {
return dataStep; }
public void add( int newValue ) {
dataVector.addElement( new Integer( newValue ) );
repaint();
revalidate();
System.out.println( toString() );
}
public void paintComponent( Graphics g ) {
super.paintComponent( g ) ;
Insets inset = getInsets();
for( int i = dataVector.size() - 1; i > 0; i-- ) {
if( graphType == DOT ) {
g.drawOval( getWidth() - inset.right - dataStep * i,getHeight() - inset.bottom - Integer.parseInt(dataVector.get( i ).toString() ),2, 2 );
}
else if( graphType == LINE ) {
if( i - 1 > 0 ) {
g.drawLine( getWidth() - inset.right - dataStep * i,getHeight() - inset.bottom - Integer.parseInt( dataVector.get( i ).toString() ), getWidth() - inset.right - dataStep * ( i - 1 ), getHeight() - inset.bottom - Integer.parseInt( dataVector.get( i - 1 ).toString() ) );
}
}
}//end for
}//end paint
public Dimension getPreferredScrollableViewportSize() {
return new Dimension( dataStep * dataSize, getHeight() ); }
public Dimension getPreferredSize() {
Dimension d = new Dimension( dataVector.size() * dataStep , getHeight() );
return d; }
public void setGraphType( int type ) {
if( type == DOT || type == LINE ) {
graphType = type; }
else { throw new IllegalArgumentException( type + " is not a valid parameter to setGraphType()." );
}
repaint();
revalidate();
}
public String toString() { return dataVector.toString(); }
}
// end NewGraphPanel
And a code for testing this is "NewGraphTester.java":
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import NewGraphPanel;
public class NewGraphTester extends JFrame implements Runnable {
private NewGraphPanel gp;
private Thread t;
public NewGraphTester() {
super( "Graph Tester" );
gp = new NewGraphPanel();
gp.setGraphType( NewGraphPanel.LINE );
JScrollPane scroll = new JScrollPane( gp );
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
getContentPane().add( scroll );
}
public void start() {
t = new Thread( this );
t.start();
}
public void run() {
while( true ) {
try {
Thread.sleep( 200 );
int value = (int)( Math.random() * 100 );
gp.add( value );
}
catch( InterruptedException x ) {
x. printStackTrace();
}
}
}
public static void main( String[] arg ) {
NewGraphTester gt = new NewGraphTester();
gt.setSize( 300, 300 );
gt.setDefaultCloseOperation( EXIT_ON_CLOSE );
gt.setVisible( true );
gt.start();
}
}
//end newGraphTester.java
SORRY for the length of the message. But i am sure this could help and i may in turn get some guidance.
Thanks in advance
Gaurav Kathotia
Scrolling with Swing
Re: Scrolling with Swing
Hello David Gilbert
In connection to my doubts on scrolling, did you check out the above example i send ? I really need some help fast.Please guide me.
Thanks in advance
Gaurav Kathotia
In connection to my doubts on scrolling, did you check out the above example i send ? I really need some help fast.Please guide me.
Thanks in advance
Gaurav Kathotia