• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Can i Get This Work ???  RSS feed

 
c sl
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i�m having problems of passing arguments between classes in my program. In order to illustrate my problem better, let me state briefly about my application.
The application is basically to simulate the thrash sorting process. A file called ThrashWind.java consists of :
 public class ThrashWind : for setting up the GUI( the frame, button, canvas area, label & drop-down list )
 class ThrashCanvas : for running the canvas
 class Batch : for running threads in the canvas area

Then there are several classes to represent the hardware-control mechanism( it�s not the real hardware-ctrl code ). Eg. To represent the sensors, the abstract super class Sensor is in a file Sensor.java. A subclass of it, class MetalSensor, is in MetalSensor.java.
The GUI part looks like the following :-
A frame contains a canvas area at the left side, a label( to display status of the process ) at the top right corner, a drop-down list below the label, & some buttons at the bottom of the frame.
In the canvas area, there are 6 nodes, denoted by small square boxes. Each node is linked to the adjacent node( from left to right ) with a straight line. By hitting the �Start� button, the simulation starts off with a small circle moving out from the first node, then moving along the line with a constant speed. Each time when the circle reaches a node, it pauses for a while & continue moving along the line.
i want to create an effect that, the moment when the circle arrives at a node, not only it can pause for a while, but also the program should call the other class file( which defines a string ), and immediately displays the string from that external file in the label .
E.g. when the circle pauses at the 2nd node( that denotes MetalSensor ), the program should call the class MetalSensor to �extract� the string in it, then get it displayed on the label. After that the circle should continue moving along the line.
Before i tried to associate external class file to the ThrashWind.java, that is when i tried out only on the animation part, the animation worked out ok, as there�s no argument passing involved yet. However, when i attempted to associate external class file & make some changes, things seemed become complicated to me, & the circle can no more moving on. Despite numerous tries, i�m quite confused in figuring out about how actually it should work.

The following are part of the code :-
// ThrashWind.java
// classes contained in this file
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

/* public class ThrashWind: set up GUI */
public class ThrashWind extends Frame
implements ActionListener,ItemListener
{

private ThrashCanvas node; // ThrashCanvas extends Canvas
Batch b; // Batch extends Thread

// class var; try to accept values from Batch's move()
int x ;
// statement below is newly added; doesn�t exist in default version code
//x = new Batch( b.x );

����

Label status = new Label(" ");

/* Default Constructor */
public ThrashWind()
{

���..
// Canvas area for animation
node = new ThrashCanvas();
node.setSize( 500,70 ); // Size of canvas area
add( "West", node );
node.setBackground( Color.lightGray ); // ok ....
node.setForeground( Color.yellow );
}
/* boolean method: make use of x value from class Batch's move()
this method doesn�t exist in default version of code
*/
public boolean passSensor()
{

// Not sure if this line of condition is correct
if ( (b.x + 10 > 198) && (b.x + 10 < 201) )
{
MetalSensor m = new MetalSensor(); // Call class MetalSensor
//MetalSensor dms = new MetalSensor();
//m.detect();

// Access ThrashWind class's Label -- status
//statusL.setText(m.detect());
status.setText("Test ...");

}
return true;

} // boolean passSensor()
public void actionPerformed( ActionEvent e )
{
// Trigger start/stop
if ( e.getSource() == start )
{
if ( start.getLabel().equals(" Start ") )
{
// start.setEnabled(true);
start.setLabel(" Stop ");
b = new Batch( node );
b.start();
//x = b.x;
passSensor(); // call boolean method
}
public static void main( String args[] )
{
ThrashWind tw = new ThrashWind();
tw.setVisible( true );
} // main()
} //public class ThrashWind

/* class ThrashCanvas: Setting up canvas area */
class ThrashCanvas extends Canvas
{
public void paint( Graphics g )
{
��.
��..
}

/* class Batch: perform Animation */
class Batch extends Thread
{
private Canvas box;
private static final int XSIZE = 10, YSIZE = 10, X2_Size = 5, Y2_Size = 5;
// In default code : int is private
//private int x = 20, y = 450;

public int x = 20, y = 450; // Modified version code
private int x2 = 300, y2 = 294;

boolean move_x = true;

private Thread t2;


/* Default Constructor Batch() */
public Batch( Canvas c )
{
box = c;
}
/* Constructor Batch() with int arg ; newly added */

public Batch( int x )
{
this.x = x;
}
// For drawing animated circle
public void draw()
{
��
��.
} // draw()

/* Move circle */
// Default ....public void move()
public void move( int x )
{

if ( move_x )
{
// Passing 3rd node: Metal Sensor //

if ( (x + 10 > 198) && (x + 10 < 201) )
{
x -= 5;
try
{
System.out.println("Passing x = 198 ..."); // ok ...

//MetalSensor m = new MetalSensor(); // Call class MetalSensor
//MetalSensor dms = new MetalSensor();
//m.detect();

// Access ThrashWind class's Label -- status ???
//status.setText(m.detect());
//////////////////////////////////////////////////////////////////
Thread.sleep(4000); // ok ...
x += 5;
Thread.sleep( 200 );
}
catch( InterruptedException e)
{
System.out.println(" Have an exception ..." );
}

} // if
}
��..
} // move()

public void run( )
{
draw();

for ( int k = 1; k < 10000; k++ )
{
move( x ); // Default : move()
try
{

Thread.sleep(200);

}
catch( InterruptedException e )
{ }
} // for

} // run()

} // class Batch

The following classes are in separate files:-
// Sensor.java
public abstract class Sensor
{
String s;
//abstract Sensor()
//{ s = new String(" ");
}
abstract void detect();

public String toString()
{ return s; }
}
//MetalSensor.java
public class MetalSensor extends Sensor
{
public void detect()
{
String s = new String(" Try to detect ferrous ...");

}
}
Is there anything wrong with my approach ? What are the better approach ?
Any help & opinion are deeply appreciated. Thanks.

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"c sl",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!