-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFooterFormat.java
More file actions
executable file
·78 lines (67 loc) · 2.38 KB
/
FooterFormat.java
File metadata and controls
executable file
·78 lines (67 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* FooterFormat.java
*
* Created on April 18, 2006, 11:24 AM
*/
/**
*
* @author Chen-Fu Liao
*/
import java.awt.*;
import java.awt.font.*;
import java.awt.print.*;
import java.text.*;
import java.util.*;
public class FooterFormat extends PageFormat implements Printable {
/**
* The font we use for the footer.
*/
private static final Font mFooterFont = new Font("Serif", Font.ITALIC, 10);
/**
* The amount of space at the bottom of the imageable area that we
* reserve for the footer.
*/
private static final float mFooterHeight = (float) (0.25 * 72);
/**
* The format for the date string shown in the footer.
*/
private static final DateFormat mDateFormat = new SimpleDateFormat();
/**
* A formatted string describing when this instance was
* created.
*/
private String mDateStr = mDateFormat.format(new Date()).toString();
/**
* Tell the caller that the imageable area of the paper is shorter
* than it actually is. We use the extra room at the bottom of the
* page for our footer text.
*/
public double getImageableHeight() {
double imageableHeight = super.getImageableHeight() - mFooterHeight;
if (imageableHeight < 0) imageableHeight = 0;
return imageableHeight;
}
/**
* Draws the footer text which has the following format:
* <date>
*/
public int print(Graphics g, PageFormat format, int pageIndex) {
/* Make a copy of the passed in Graphics instance so
* that we do not upset the caller's current Graphics
* settings such as the current color and font.
*/
Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(Color.black);
g2d.setFont(mFooterFont);
LineMetrics metrics = mFooterFont.getLineMetrics(mDateStr, g2d.getFontRenderContext());
/* We will draw the footer at the bottom of the imageable
* area. We subtract off the font's descent so that the bottoms
* of descenders remain visable.
*/
float y = (float) (super.getImageableY() + super.getImageableHeight()- metrics.getDescent() - metrics.getLeading());
// Cast to an int because of printing bug in drawString(String, float, float)!
g2d.drawString(mDateStr, (int) super.getImageableX(), (int)y);
g2d.dispose();
return Printable.PAGE_EXISTS;
}
}