public class Countable {
/**
* Instance variables
*/
private int ID ;
/**
* Class variables
*/
public static int ObjectCounter ;
/**
* Constructor
*/
public Countable() {
ID = ObjectCounter++ ;
/**
* continue...
*/
}//---> end of the constructor <-----
/**
* continue...
*/
}//---> end of the Countable class <-----
Here you provide a static variable which is incremented each
time you create a Countable object (Countable class instance).
This way all your objects will have a unique ID. You can then
create your accessor methods for ID variable...
5° How to draw
a String vertically
Java 1.2
Graphics2D methods are used.
public void paintComponent( Graphics _G ) {
super.paintComponent( _G ) ;
Graphics2D _G2 = ( Graphics2D )_G ;
Dimension _D = getSize() ;
_G2.setColor( Color.red ) ;
_G2.fillRect( 0, 0, _D.width, _D.height ) ;
/**
* Draw the string
*/
_G2.setColor( Color.white ) ;
_G2.rotate( 3 * Math.PI/2, 100, 100 ) ;
_G2.drawString( "My String", 100, 100 ) ;
/**
* Rotate the string
*/
_G2.rotate( -3 * Math.PI/2, 100, 100 ) ;
}//>>> end of the paintComponent method <<<<<
|