Sun Jun 17, 2007 4:17 am
package lgfGraph;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import lgfGraph.GraphPanel;
public class Graph {
String version = "2007.06.15.0017";
GraphPanel panel;
public Graph() { /**/ }
// change the value to be graphed using this update function
public void update( int i ) { panel.newValue( i); }
public void go() {
/* uncomment this if you have the windows classic L&F installed
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch( Exception ignored ){
System.out.println("Couldn't switch to windows Classic Look and Feel");
}
*/
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Graph Component Test version " + version );
Dimension dim = new Dimension( 508, 330 );
frame.setSize(dim);
frame.setMaximumSize(dim);
frame.setMinimumSize(dim);
frame.setPreferredSize(dim);
frame.setLocation( 100,100 );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(null);
frame.setBackground(Color.black);
frame.setForeground(Color.black);
panel = new GraphPanel();
frame.add(panel);
panel.setBackground(Color.black);
} // close go()
public static void main(String[] args) {
// we're not invoking this in some kind of invokeLater function
// because we don't have any text related components and we
// don't monkey with any components not on the EDT so....
Graph g = new Graph();
g.go();
g.update(100);
for( int i = 4; i pointsAL;
PlotPoints[] points;
//ImageIcon II;
//Image img;
int zeroX = 7;
int zeroY = 149;
int zeroValueX = 16;
int zeroValueY = 145;
int intervalX = 77;
int intervalY = 38;
int maxValueY = 120;
int valueToPlot = 145;
int secondsInGraphing = 0;
int numberOfLinesToDraw = 0;
Thread runThread;
public void newValue(int i ) { valueToPlot = i; }
public void run() {
while( true) {
try {
Thread.sleep( 1000); // one update per second
} catch( InterruptedException ie ){ /**/ }
secondsInGraphing++;
numberOfLinesToDraw++;
if ( numberOfLinesToDraw > 5 ) numberOfLinesToDraw = 5;
for ( int i = 5; i > 0; i-- ) {
points[i].setValue( points[i - 1].gimmeValue());
}
points[0].setValue( valueToPlot);
repaint();
} // close while block
}
Dimension size = new Dimension( 500, 300 );
public GraphPanel() {
setSize( size );
setPreferredSize(size);
setMinimumSize( size);
setMaximumSize( size);
setLayout(null);
pointsAL = new ArrayList();
PlotPoints p = new PlotPoints( 401, 145, valueToPlot );
pointsAL.add(p);
p = new PlotPoints( 324, 145, valueToPlot );
pointsAL.add(p);
p = new PlotPoints( 247, 145, valueToPlot );
pointsAL.add(p);
p = new PlotPoints( 170, 145, valueToPlot );
pointsAL.add(p);
p = new PlotPoints( 93, 145, valueToPlot );
pointsAL.add(p);
p = new PlotPoints( 16, 145, valueToPlot );
pointsAL.add(p);
points = pointsAL.toArray( new PlotPoints[0] );
runThread = new Thread( this );
runThread.setDaemon(true);
runThread.start();
}
public void drawTheBackground( Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.lightGray);
// first the horizontal lines
for( int i = 0; i ( secondsInGraphing - i)) break;
} // close for loop
}
public void paintComponent(Graphics g){
Color oldColor;
oldColor = g.getColor();
drawTheBackground( g);
g.setColor(Color.yellow );
int index = 0;
for( int i = 0; i numberOfLinesToDraw ) break;
} // close for loop
g.drawString("valueToPlot=" + valueToPlot, 40, 40);
// restore the old system color whatever it was:
g.setColor(oldColor);
}
} // close class GraphPanel
package lgfGraph;
public class PlotPoints {
int x;
int y;
int value;
public PlotPoints( int X, int Y, int Value ) {
x = X; y = Y; value = Value;
}
public void setValue( int i ) { value = i; }
public int gimmeX() { return x; }
public int gimmeY() { return y; }
public int gimmeValue(){ return value; }
}
Sun Jun 17, 2007 12:08 pm
Sun Jun 17, 2007 10:59 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.