This issue presents tips, techniques, and sample code for the following topics:
Printing in Java
For printing in Java, try this. JDKTM 1.1
provides a standard way to print and improves on JDK 1.0 printing.
You print via the Abstract Window Toolkit (AWT). A simple example for
printing a filled-in oval looks like this:
import java.awt.*;
public class print {
public static void main(String args[])
{
Frame f = new Frame("test");
f.pack();
PrintJob pj =
f.getToolkit().getPrintJob(f, "print1", null);
if (pj != null) {
Graphics g = pj.getGraphics();
g.fillOval(5, 5, 150, 100);
g.dispose();
pj.end();
}
System.exit(0);
}
}
This example is a standalone Java program that creates an AWT frame, a
top-level GUI component for running an application. The program next
retrieves the toolkit for the frame. The toolkit for the AWT is the interface
between the abstract window layer and an actual windowing implementation such
as Windows 95 or X Windows.
Once you create the toolkit, you can initiate a print job via getPrintJob,
which causes a window to pop up on the screen asking the user for information
about which printer is being used, the number of copies to print, and so on.
This process is similar to printing in conventional Windows applications.
Given a PrintJob object, you can obtain a graphics context and draw to it.
Such drawing, using normal graphics primitives such as fillOval, goes to the
printer instead of the screen. Calling dispose on the graphics context object
sends the page to the printer, and the print job is terminated by calling end.
If you're using your own custom AWT components, you can use the paint methods
you define for printing without change--by passing them the graphics context
returned by getGraphics. But if you want different behavior when you print,
specialized methods can also be defined for components as necessary.
Printing from applets
The example given above is a standalone Java program. But what about printing
from applets? The Java security system contains a feature that may lock out
an applet from initiating its own print job, requiring that the initiation be
done via a Web browser or applet viewer. The security area is in a state of
flux at present, and your experiences with this process may be different.
Finally, another less portable approach to printing text is to open
the special device file that represents the printer, and do conventional
file I/O to that file. For example, "lpt1" can be used for
this purpose with Windows systems, and "/dev/lp" can be used
with UNIX.
Global variables
If you've programmed with languages such as C or C++ or Pascal, you've
probably used global variables and functions. For example, the following
simple code:
int x = 0;
void f()
{
x = 37;
}
in C/C++ establishes a global variable "x," and the function f sets
its value to 37.
Java does not have such variables and functions, however. Instead, you wrap
the code in class declarations. The Java equivalent of the code given above
is:
public class Globals {
public static int x = 0;
public static void f() {x = 37;}
}
And you refer to "x" with:
Globals.x = 59;
Note that the "static" keyword is used in declaring these class
members. This usage makes the members into class variables and methods, that
is, ones that do not reference or operate on specific class object instances.
You could say that the class structure in this example is most valuable for
packaging purposes--instead of as a basis for object-oriented design.
A common idiom is to use a Java class to group related constants as in the
following example:
public class TextConstants {
public static final int WIDTH = 80;
public static final int HEIGHT = 25;
public static final int POINTSIZE = 12;
public static final String FONT = "monospaced";
}
You can refer to individual constants with expressions such as:
int i = TextConstants.POINTSIZE;
Declaring a private constructor
There's one more useful tip that applies to this approach. If you're using a
class simply for packaging purposes, it really doesn't make sense to create a
new instance of the class. Instead, you can declare a private constructor:
private TextConstants() {}
The constructor must execute when an instance of the class is created, but
with a private constructor, this is impossible.
|