Java code problem ...
From: jimmy (hat_et_m_at_yahoo.fr)
Date: 05/31/04
- Next message: Andrey Fedorov: "DnD application problems"
- Previous message: jimmy: "Java code - problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 31 May 2004 19:08:17 +0200
Hello,
Can you help me to debogg this java program .
The problems are on class SystemLinear (perhaps java type errors).
-----------------------------
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
//import java.awt.Label;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Optique extends Applet
{
// all methods for doing this are missing.
// implements ActionListener,
// KeyListener, MouseListener, MouseMotionListener
Droite axe1 = new Droite( 40,280,0,1,0,0,880 );
Droite axe2 = new Droite( 60,520,0,0,-1,0,2*(520-280) );
public void init()
{
double tab[]= new double[2];
tab[0]= new double[3];tab[1]= new double[3];
tab[0][0]=1;tab[0][1]=1;tab[0][2]=1;
tab[1][0]=0;tab[1][1]=1;tab[1][2]=1;
SystemLinear syst = new SystemLinear();
double sol[]=syst.solution(tab);
size_sol=sol.length;
for(int i=0;i<size_sol;i++){
System.out.println(sol[i]);
}
}
public void paint( Graphics g )
{
g.setColor( Color.RED );
axe1.traceSegment(g);
axe2.traceSegment(g);
/* String text1=axe1.toString();
g.setColor( Color.BLACK );
g.drawString(text1,0,10);*/
}
/**
* Allow this applet to run as as application as well for testing
*
* @param args not used
*/
public static void main( String args[] )
{
final Optique applet = new Optique( );
final Frame frame = new Frame( "Optique" );
frame.setSize( 1000, 1000 );
applet.init();
frame.add( applet );
frame.validate();
frame.setVisible( true );
applet.start();
frame.addWindowListener(
new WindowAdapter ()
{
/**
* handler on window close
*
* @param e event details
*/
public void windowClosing (
WindowEvent e )
{
applet.stop();
applet.destroy();
System.exit( 0 );
} // end WindowClosing
} // end anonymous class
);
} // end main
}
/**
* Point renamed to PointXYZ avoid confusion with java.awt.Point
* Defines a point in 3D cartesian space
*/
class PointXYZ
{
double x;
double y;
double z;
PointXYZ()
{
this.x=0;
this.y=0;
this.z=0;
}
PointXYZ(double x, double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
public double getx()
{
return this.x;
}
public double gety()
{
return this.y;
}
public double getz()
{
return this.z;
}
public void set(double x, double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
public void setx(double x)
{
this.x=x;
}
public void sety(double y)
{
this.y=y;
}
public void setz(double z)
{
this.z=z;
}
public PointXYZ intersection2D(Droite droite1,Droite droite2){
}
}
/**
* defines a direction with x,y,z
* Instead of duplicating code, we inherit it from PointXYZ
*/
class Direction extends PointXYZ //cosinus directeurs
{
Direction()
{
super( 1, 0, 0 );
} // x direction principale, axe optique
Direction( double x, double y,double z )
{
super( x, y, z );
}
}
/**
* defines a 3D line segment
*/
class Droite
{
double x1, y1, z1, x2, y2, z2;
Droite()
{
this.x1=0;this.y1=0;this.z1=0;
this.x2=0;this.y2=0;this.z2=0;
} //point (0,0,0)
Droite(PointXYZ p,Direction d,double dist)
{
this.x1=p.getx();
this.y1=p.gety();
this.z1=p.getz();
double dx=d.getx();
double dy=d.gety();
double dz=d.getz();
this.x2=x1+dist*dx;
this.y2=y1+dist*dy;
this.z2=z1+dist*dz;
}
Droite(double x1, double y1, double z1,
double l, double m, double n, double dist)
{
// is no p.
//this.x1=p.getx();
//this.y1=p.gety();
//this.z1=p.getz();
// I presume you mean
this.x1=x1;
this.y1=y1;
this.z1=z1;
this.x2=x1+dist*l;
this.y2=y1+dist*m;
this.z2=z1+dist*n;
}
Droite( PointXYZ p1, PointXYZ p2 )
{
this.x1=p1.getx();
this.y1=p1.gety();
this.z1=p1.getz();
this.x2=p2.getx();
this.y2=p2.gety();
this.z2=p2.getz();
}
public void traceSegment(Graphics g)
{
// drawLine takes int values
g.drawLine((int)this.x1,
(int)this.y1,
(int)this.x2,
(int)this.y2);//tracé en 2D
}
public Point extrem1(){
return new Point(this.x1,this.y1,this.z1);
}
public Point extrem2(){
return new Point(this.x2,this.y2,this.z2);
}
/* debogging tool*/
public String toString()
{
return "x1:" + x1 + " y1:" + y1 + "z1:" + z1 + "\n x2:" + x2 + " y2:"
+ y2 + " z2:" + z2;
}
}
class SystemLinear{
int taille;
double[] a;
double[] x;
SystemLinear(){}
public double[] solution(double[] a) //tableau 2D ou matrice
this.taille=a.length;
int n=this.taille;
double[] x;
for(int k=0;k<n-1;k++)
{
double v=a[k][k];if (v==0) {this.a=a;v=permu(k); if (v==0) {return x;}}
for(int i=k;i<n;i++)
{
for(int j=k;j<n+1;j++)
{
a[i][j]=a[i][j]-a[i][k]*a[k][j]/v;
}}}
if (a[n][n]==0) {return x;}
x[n]=a[n][n+1]/a[n][n];
for (i=n-1;i>=0;i--)
{double s=0;
for (j=i;j<n;j++)
{s=s+a[i][j]*x[j];}
x[i]=(a[i][n+1]-s)/a[i][i];
}
this.a=a;this.x=x;
return x;
}
public double permu(k)
{int q=0;int li=k;int n=this.taille; double[] a=this.a;
int i;
while (a[li][k]==0)
{
q++;li=q+k;
if (li==n+1) {return 0;}
}
for(i=0;i<n+1;i++){ double aux=a[k][i];a[k][i]=a[li][i];a[li][i]=aux;}
return a[k][k];
}
}
- Next message: Andrey Fedorov: "DnD application problems"
- Previous message: jimmy: "Java code - problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|