Can you show Java examples ?

From: Jeff Relf (Jeff_Relf__at__NCPlus.NET.Invalid)
Date: 06/16/04


Date: 16 Jun 2004 21:51:54 GMT

Hi Mark Thornton,

You asserted,
" I have been using Java for over seven years
  and know its strengths and weaknesses very well. "

The following C++ routines are used to print to any printer
  ( including Europe's A4 paper )
  as well as to any sized screen.
  ( Only the HDC and a few variables change )

  Can you show Java examples ?

#define Str sprintf

#define SetPtr SelectObject

typedef unsigned long ulong ;
typedef char * Line ;

#define LOOP while ( 1 )

#define Loop( _LLL ) int J = -1, LLL = _LLL ; \
  while ( ++ J < LLL )

int rv ; // rv is a very temporary holding place.

int Rnd ( float F ) { return ( int ) floor ( F + .5 ); }

// The following two lines are in some routines somewhere ...
// CharH, a float, is scaled to fit a screen.
// Land is for landscape.
// CharH = ( Way == Land ? 9 : 6.5 ) * Scale ;
// or a *** of paper.
// CharH = Way == Land ? CharH_L : CharH_P ;

// CF() creates a font for a printer or a screen.
// The aspect Ratio _Must_Be_ determined first.
// Tries for Bold.
// F, for font, is a bound reference,
// one of the few C++ features that I use.
CF ( HDC DC, HFONT & F, float Width ) {
  F = CreateFont( - Rnd ( 81 * Width / 49 ),
    0,0,0, 700,0,0,0,0,0,0,0, FIXED_PITCH, 0 );
  int Tmp =! DC ; if ( Tmp ) DC = GetDC ( 0 );
  HFONT OF = ( HFONT ) SetPtr ( DC, F ); SIZE Sz;
  GetTextExtentPoint ( DC,"XXXXX", 5, & Sz );
  Sz.cx /= 5 ; SetPtr ( DC, OF );
  if( Sz.cx > Width ) { DeleteObject( F );
    F = CreateFont( rv= -Sz.cy * Width / Sz.cx,
      0,0,0, 700,0,0,0,0,0,0,0, FIXED_PITCH, 0);
    SetPtr( DC, F);
    GetTextExtentPoint( DC,"XXXXX", 5, & Sz); Sz.cx /= 5;
    SetPtr( DC, OF );
    if ( Sz.cx > Width) { DeleteObject( F );
      F = CreateFont( rv * ( int ) Width / Sz.cx,
        0,0,0, 0,0,0,0,0,0,0,
        DRAFT_QUALITY, FIXED_PITCH, 0 ); } }
  if ( Tmp ) ReleaseDC ( 0, DC ); }

// Gets details about the selected printer.
GetPrinter ( ulong F, int Passive ) {
  HDC Old_DC = PnDC; PrnRec.Flags = F | PD_RETURNDC ;
  if( ! PrintDlg ( & PrnRec ) ) {
    if ( Passive ) return; int Err = CommDlgExtendedError();
    if ( Err ) {
      Str( Mess,"Printer Setup Dialog Error %d",
        Err ); Bow( Mess ); } if ( ! Err ) return ; }
  if ( ! ( PnDC = PrnRec.hDC ) )
    if ( Passive ) return ; else Bow("No Printer Selected");
  { DEVNAMES * Printers =
    ( DEVNAMES * ) GlobalLock ( PrnRec.hDevNames );
    Str ( PrnName,"%s",
      ( Line ) Printers + Printers->wDeviceOffset );
    Str ( PrnPort,
      ( Line ) Printers + Printers->wOutputOffset );
    SetDlgItemText ( Win, 1006, PrnName );
    SetDlgItemText ( Win, 1020, PrnPort );
    GlobalUnlock ( PrnRec.hDevNames ); }
    if ( PrnFon ) DeleteObject ( PrnFon );
    Line_Able = GetDeviceCaps ( PnDC, LINECAPS );
  if ( Old_DC && Old_DC != PnDC ) DeleteDC( Old_DC );
  SetBkMode( PnDC, TRANSPARENT );
  SetMapMode( PnDC, MM_ISOTROPIC );
  { DEVMODE * Printer =
    ( DEVMODE * ) GlobalLock( PrnRec.hDevMode);
    _CharH = GetDeviceCaps(PnDC,LOGPIXELSY) * 9.4 / 72 *
      ( Printer->dmPaperSize == DMPAPER_A4 ?
        21 / 2.54 / 8.5 : 1 );
    GlobalUnlock ( PrnRec.hDevMode ); }
      // CF Creates a font.
      CF ( PnDC, PrnFon, PrnCharW = _CharH * WHR );
      CF ( PnDC, BigPrnFon, BigPrnCharW =
        _CharH * WHR * BigSc );
  LM = 7 * PrnCharW ; // Sets various margins.
  MarginP = CharH_P = _CharH * 1.032 ;
  MarginL = ( CharH_L = _CharH * 1.06 ) * 2 ; }

// _Say() and Say() work the same,
// no matter if the HDC is a printer or a screen.
_Say ( float X, float Y, char * Bu, int L ) {
  if( L <= 0) return;
  int _Y = Y- CharH * .3, YY = Rnd( Y ), LL;
  char * P = Bu ;
  LOOP {
    Bu = P ;
    while ( L && ( uchar ) * P < 128 ) { L--; P++; }
    LL = P- Bu ;
    if ( LL > 0) {
      X -= ChW;
      Loop ( LL ) TextOut ( PnDC,
        Rnd ( X += ChW ), YY, Bu + J, 1 ); }
    if ( L ) { TextOut ( PnDC, Rnd ( X += ChW ), _Y, P, 1 );
      X += ChW ; L -- ; P ++ ; } if( ! L ) break; } }

#define FuSay \
  if( P > Bu) _Say ( LM + ( Bu- Old ) * ChW, Y, Bu,P- Bu); \
  Bu = P + 1 ;

#define FuFuSay \
  if ( P > Bu) { SetBkMode( PnDC, OPAQUE ); \
    float W = ChW ; ChW = BigChW ; \
    _Say( LM + ( Bu - Old ) * W, \
      Y - CharH * BigSc, Bu, P - Bu ); ChW = W ; \
    SetBkMode( PnDC, TRANSPARENT ); } Bu = P + 1 ;

Say ( ) { Resp ( ); // Resp() handles mouse events
  char C, * P = Old, * Bu = Old ;
  float Y = Row * CharH + Margin ;
  if ( Line_Able ) {
    while( P < Cur) {
      if ( * P == '_' && P [ 1 ] !='_') {
        FuSay while ( ++ P <= Cur && ( uchar ) * P > 32 );
        rv = FnC ( _Re ); SetPtr ( PnDC, OF );
        OF = ( HFONT ) SetPtr ( PnDC, BigPrnFon );
        FuFuSay SetPtr ( PnDC, OF );
        OF = ( HFONT ) SetPtr ( PnDC, PrnFon );
        FnC ( rv ); }
      else if ( * P == '|' ) { FuSay
        MoveToEx ( PnDC,
          rv = Rnd ( LM + ( P - Old ) * ChW + ChW / 2),
          Rnd ( Y ), 0 );
        LineTo ( PnDC, rv, Rnd ( Y + CharH ) ); }
      else if ( ( * P == '-' || * P == '=' )
        && P [ 1 ] == ( C = * P ) ) { FuSay
          OP = C == '-' ?
            0 : ( HPEN ) SetPtr( PnDC, WidePen );
          MoveToEx ( PnDC,
            Rnd ( LM + ( P- Old ) * ChW ),
            rv = Rnd ( Y + CharH / 2 ), 0 );
          while ( ++ P <= Cur && * P == C ); Bu = P ;
          LineTo ( PnDC,
            Rnd ( LM + ( P- Old ) * ChW ), rv );
          if ( OP ) SetPtr ( PnDC, OP ); continue; }
    P++; } } FuSay Old = Cur + 1; }