Total members 11893 |It is currently Tue Nov 05, 2024 11:56 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hey everybody. I'm kinda new to the Java programming game and I just now found this site. It's cool. I come from a C++ background and I gotta say Java is easier to use but there's a steep learning curve in programming stuff in Swing. I decided to work with Swing components to learn more about swing and I've build some widgets that you guys might like to use. This first widget is a moving graph sort of widget and it comes as an eclipse project with three separate files.



This is the first file:

java code
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


and here's the third of three files:

java code
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; }
}


All this code is in the open domain and you can modify it, use it, ignore it, whatever.
Enjoy.




Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

Very Good 2D Graph ,Thanks for Sharing the code :lol:

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thanks for this good topic.

_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : a moving graph sortof widget build using Swing
 an ammeter ( round gauge ) sortof widget built using Swing     -  
 Bar Graph using java swings     -  
 Changing the color of a graph draw in java     -  
 moving files in php     -  
 Moving between frames     -  
 What is Swing?!!     -  
 moving glasspane between frames     -  
 HOW TO KEEP OBJECT MOVING FOLLOW.     -  
 Moving Background in flash     -  
 Create a moving text banner     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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