下面列出了java.awt.Font#BOLD 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
CompositeStrike(CompositeFont font2D, FontStrikeDesc desc) {
this.compFont = font2D;
this.desc = desc;
this.disposer = new FontStrikeDisposer(compFont, desc);
if (desc.style != compFont.style) {
algoStyle = true;
if ((desc.style & Font.BOLD) == Font.BOLD &&
((compFont.style & Font.BOLD) == 0)) {
boldness = 1.33f;
}
if ((desc.style & Font.ITALIC) == Font.ITALIC &&
(compFont.style & Font.ITALIC) == 0) {
italic = 0.7f;
}
}
strikes = new PhysicalStrike[compFont.numSlots];
}
@Override
public boolean useAAForPtSize(int ptsize) {
char[] gasp = getGaspTable();
if (gasp.length > 0) {
for (int i=0;i<gasp.length;i+=2) {
if (ptsize <= gasp[i]) {
return ((gasp[i+1] & 0x2) != 0); // bit 2 means DO_GRAY;
}
}
return true;
}
if (style == Font.BOLD) {
return true;
} else {
return ptsize <= 8 || ptsize >= 18;
}
}
public Font2D getFontWithExactStyleMatch(int style) {
switch (style) {
case Font.PLAIN:
return plain;
case Font.BOLD:
return bold;
case Font.ITALIC:
return italic;
case Font.BOLD|Font.ITALIC:
return bolditalic;
default:
return null;
}
}
private void styleListValueChanged(ListSelectionEvent e) {
int style = -1;
String selectedStyle = styleList.getSelectedValue();
if (selectedStyle == PLAIN) {
style = Font.PLAIN;
}
if (selectedStyle == BOLD) {
style = Font.BOLD;
}
if (selectedStyle == ITALIC) {
style = Font.ITALIC;
}
if (selectedStyle == BOLD_ITALIC) {
style = Font.BOLD + Font.ITALIC;
}
font = FontTools.getFont(font.getFamily(), style, font.getSize());
previewLabel.setFont(font);
}
@Override
public boolean useAAForPtSize(int ptsize) {
char[] gasp = getGaspTable();
if (gasp.length > 0) {
for (int i=0;i<gasp.length;i+=2) {
if (ptsize <= gasp[i]) {
return ((gasp[i+1] & 0x2) != 0); // bit 2 means DO_GRAY;
}
}
return true;
}
if (style == Font.BOLD) {
return true;
} else {
return ptsize <= 8 || ptsize >= 18;
}
}
public Font2D getFontWithExactStyleMatch(int style) {
switch (style) {
case Font.PLAIN:
return plain;
case Font.BOLD:
return bold;
case Font.ITALIC:
return italic;
case Font.BOLD|Font.ITALIC:
return bolditalic;
default:
return null;
}
}
public static void printTree(Cluster a,String file) {
int depth = getDepth(a);
width = wunit*(depth+1);
height = hunit*(depth+1);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image .createGraphics();
g.setColor(new Color(0,0,0));
g.setStroke(new BasicStroke(1));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("宋体", Font.BOLD, 20);
g.setFont(font);
drawTree(a, g, width/2, 0 , 1);
//释放对象
g.dispose();
// 保存文件
try {
ImageIO.write(image, "png", new File(file));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public CFont createItalicVariant() {
CFont font = new CFont(this, familyName);
font.fullName =
fullName + (style == Font.BOLD ? "" : "-") + "Italic-Derived";
font.style |= Font.ITALIC;
font.isFakeItalic = true;
return font;
}
public TitleStrip(String title, String subTitle, Icon icon) {
this.title = new JLabel(title);
this.subTitle = new JLabel(subTitle);
this.icon = new JLabel(icon);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Font font = this.title.getFont();
Font boldFont = new Font(font.getFontName(), Font.BOLD, font.getSize() + 4);
this.title.setFont(boldFont);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.insets.top = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.bottom = 5;
add(this.title, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0;
gbc.insets.left = 15;
add(this.subTitle, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.anchor = GridBagConstraints.CENTER;
add(this.icon, gbc);
}
private Font buildFont() {
// Font labelFont = previewLabel.getFont();
String fontName = (String)fontList.getSelectedValue();
if (fontName == null) {
return null;
// fontName = labelFont.getName();
}
Integer sizeInt = (Integer)sizeList.getSelectedValue();
if (sizeInt == null) {
// size = labelFont.getSize();
return null;
}
// create the font
// // first create the font attributes
// HashMap map = new HashMap();
// map.put(TextAttribute.BACKGROUND, Color.white);
// map.put(TextAttribute.FAMILY, fontName);
// map.put(TextAttribute.FOREGROUND, Color.black);
// map.put(TextAttribute.SIZE , new Float(size));
// map.put(TextAttribute.UNDERLINE, italicCheckBox.isSelected() ? TextAttribute.UNDERLINE_LOW_ONE_PIXEL : TextAttribute.UNDERLINE_LOW_TWO_PIXEL);
// map.put(TextAttribute.STRIKETHROUGH, italicCheckBox.isSelected() ? TextAttribute.STRIKETHROUGH_ON : Boolean.FALSE);
// map.put(TextAttribute.WEIGHT, boldCheckBox.isSelected() ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
// map.put(TextAttribute.POSTURE,
// italicCheckBox.isSelected() ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR);
//
// return new Font(map);
return new Font(fontName,
(italicCheckBox.isSelected() ? Font.ITALIC : Font.PLAIN)
| (boldCheckBox.isSelected() ? Font.BOLD : Font.PLAIN),
sizeInt);
}
@Override
protected FontUIResource getInstance() {
return new DerivedUIResourceFont(MAC_DEFAULT_FONT_NAME, Font.BOLD, 13);
}
public MarsView(MarsModel model) {
super(model, "Mars World", 600);
defaultFont = new Font("Arial", Font.BOLD, 18); // change default font
setVisible(true);
repaint();
}
/**
* Returns the font for the values in the passed in AttributeSet.
* It is assumed the keys will be CSS.Attribute keys.
* <code>sc</code> is the StyleContext that will be messaged to get
* the font once the size, name and style have been determined.
*/
Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {
ss = getStyleSheet(ss);
int size = getFontSize(a, defaultSize, ss);
/*
* If the vertical alignment is set to either superscirpt or
* subscript we reduce the font size by 2 points.
*/
StringValue vAlignV = (StringValue)a.getAttribute
(CSS.Attribute.VERTICAL_ALIGN);
if ((vAlignV != null)) {
String vAlign = vAlignV.toString();
if ((vAlign.indexOf("sup") >= 0) ||
(vAlign.indexOf("sub") >= 0)) {
size -= 2;
}
}
FontFamily familyValue = (FontFamily)a.getAttribute
(CSS.Attribute.FONT_FAMILY);
String family = (familyValue != null) ? familyValue.getValue() :
Font.SANS_SERIF;
int style = Font.PLAIN;
FontWeight weightValue = (FontWeight) a.getAttribute
(CSS.Attribute.FONT_WEIGHT);
if ((weightValue != null) && (weightValue.getValue() > 400)) {
style |= Font.BOLD;
}
Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);
if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {
style |= Font.ITALIC;
}
if (family.equalsIgnoreCase("monospace")) {
family = Font.MONOSPACED;
}
Font f = sc.getFont(family, style, size);
if (f == null
|| (f.getFamily().equals(Font.DIALOG)
&& ! family.equalsIgnoreCase(Font.DIALOG))) {
family = Font.SANS_SERIF;
f = sc.getFont(family, style, size);
}
return f;
}
/**
* Confirm that the equals method can distinguish all the required fields.
*/
@Test
public void testEquals() {
PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
new SimpleDateFormat("d"));
PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(Day.class,
new SimpleDateFormat("d"));
assertTrue(info1.equals(info2));
assertTrue(info2.equals(info1));
Class c1 = Day.class;
Class c2 = Month.class;
DateFormat df1 = new SimpleDateFormat("d");
DateFormat df2 = new SimpleDateFormat("MMM");
RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1);
RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2);
Font lf1 = new Font("SansSerif", Font.PLAIN, 10);
Font lf2 = new Font("SansSerif", Font.BOLD, 9);
Paint lp1 = Color.black;
Paint lp2 = Color.blue;
boolean b1 = true;
boolean b2 = false;
Stroke s1 = new BasicStroke(0.5f);
Stroke s2 = new BasicStroke(0.25f);
Paint dp1 = Color.red;
Paint dp2 = Color.green;
info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
assertTrue(info1.equals(info2));
info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
assertFalse(info1.equals(info2));
info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
assertTrue(info1.equals(info2));
}
private void initNames() throws FontFormatException {
/* Valid XLFD has exactly 14 "-" chars.
* First run over the string to verify have at least this many
* At the same time record the locations of the hyphens
* so we can just pick the right substring later on
*/
int[] hPos = new int[14];
int hyphenCnt = 1;
int pos = 1;
String xlfd = platName.toLowerCase(Locale.ENGLISH);
if (xlfd.startsWith("-")) {
while (pos != -1 && hyphenCnt < 14) {
pos = xlfd.indexOf('-', pos);
if (pos != -1) {
hPos[hyphenCnt++] = pos;
pos++;
}
}
}
if (hyphenCnt == 14 && pos != -1) {
/* Capitalise words in the Family name */
String tmpFamily = xlfd.substring(hPos[1]+1, hPos[2]);
StringBuilder sBuffer = new StringBuilder(tmpFamily);
char ch = Character.toUpperCase(sBuffer.charAt(0));
sBuffer.replace(0, 1, String.valueOf(ch));
for (int i=1;i<sBuffer.length()-1; i++) {
if (sBuffer.charAt(i) == ' ') {
ch = Character.toUpperCase(sBuffer.charAt(i+1));
sBuffer.replace(i+1, i+2, String.valueOf(ch));
}
}
familyName = sBuffer.toString();
String tmpWeight = xlfd.substring(hPos[2]+1, hPos[3]);
String tmpSlant = xlfd.substring(hPos[3]+1, hPos[4]);
String styleStr = null;
if (tmpWeight.indexOf("bold") >= 0 ||
tmpWeight.indexOf("demi") >= 0) {
style |= Font.BOLD;
styleStr = "Bold";
}
if (tmpSlant.equals("i") ||
tmpSlant.indexOf("italic") >= 0) {
style |= Font.ITALIC;
if (styleStr == null) {
styleStr = "Italic";
} else {
styleStr = styleStr + " Italic";
}
}
else if (tmpSlant.equals("o") ||
tmpSlant.indexOf("oblique") >= 0) {
style |= Font.ITALIC;
if (styleStr == null) {
styleStr = "Oblique";
} else {
styleStr = styleStr + " Oblique";
}
}
if (styleStr == null) {
fullName = familyName;
} else {
fullName = familyName + " " + styleStr;
}
encoding = xlfd.substring(hPos[12]+1);
if (encoding.startsWith("-")) {
encoding = xlfd.substring(hPos[13]+1);
}
if (encoding.indexOf("fontspecific") >= 0) {
if (tmpFamily.indexOf("dingbats") >= 0) {
encoding = "dingbats";
} else if (tmpFamily.indexOf("symbol") >= 0) {
encoding = "symbol";
} else {
encoding = "iso8859-1";
}
}
} else {
throw new FontFormatException("Bad native name " + platName);
// familyName = "Unknown";
// fullName = "Unknown";
// style = Font.PLAIN;
// encoding = "iso8859-1";
}
}
public void setFont(Font2D font, int style) {
if (FontUtilities.isLogging()) {
String msg;
if (font instanceof CompositeFont) {
msg = "Request to add " + font.getFamilyName(null) +
" with style " + style + " to family " + familyName;
} else {
msg = "Request to add " + font +
" with style " + style + " to family " + this;
}
FontUtilities.getLogger().info(msg);
}
/* Allow a lower-rank font only if its a file font
* from the exact same source as any previous font.
*/
if ((font.getRank() > familyRank) && !isFromSameSource(font)) {
if (FontUtilities.isLogging()) {
FontUtilities.getLogger()
.warning("Rejecting adding " + font +
" of lower rank " + font.getRank() +
" to family " + this +
" of rank " + familyRank);
}
return;
}
switch (style) {
case Font.PLAIN:
if (preferredWidth(font) && closerWeight(plain, font, style)) {
plain = font;
}
break;
case Font.BOLD:
if (preferredWidth(font) && closerWeight(bold, font, style)) {
bold = font;
}
break;
case Font.ITALIC:
if (preferredWidth(font) && closerWeight(italic, font, style)) {
italic = font;
}
break;
case Font.BOLD|Font.ITALIC:
if (preferredWidth(font) && closerWeight(bolditalic, font, style)) {
bolditalic = font;
}
break;
default:
break;
}
}
public Font2D getFont(int style) {
switch (style) {
case Font.PLAIN:
return plain;
case Font.BOLD:
if (bold != null) {
return bold;
} else if (plain != null && plain.canDoStyle(style)) {
return plain;
} else {
return null;
}
case Font.ITALIC:
if (italic != null) {
return italic;
} else if (plain != null && plain.canDoStyle(style)) {
return plain;
} else {
return null;
}
case Font.BOLD|Font.ITALIC:
if (bolditalic != null) {
return bolditalic;
} else if (bold != null && bold.canDoStyle(style)) {
return bold;
} else if (italic != null && italic.canDoStyle(style)) {
return italic;
} else if (plain != null && plain.canDoStyle(style)) {
return plain;
} else {
return null;
}
default:
return null;
}
}
Font2D getClosestStyle(int style) {
switch (style) {
/* if you ask for a plain font try to return a non-italic one,
* then a italic one, finally a bold italic one */
case Font.PLAIN:
if (bold != null) {
return bold;
} else if (italic != null) {
return italic;
} else {
return bolditalic;
}
/* if you ask for a bold font try to return a non-italic one,
* then a bold italic one, finally an italic one */
case Font.BOLD:
if (plain != null) {
return plain;
} else if (bolditalic != null) {
return bolditalic;
} else {
return italic;
}
/* if you ask for a italic font try to return a bold italic one,
* then a plain one, finally an bold one */
case Font.ITALIC:
if (bolditalic != null) {
return bolditalic;
} else if (plain != null) {
return plain;
} else {
return bold;
}
case Font.BOLD|Font.ITALIC:
if (italic != null) {
return italic;
} else if (bold != null) {
return bold;
} else {
return plain;
}
}
return null;
}
public void setFont(Font2D font, int style) {
if (FontUtilities.isLogging()) {
String msg;
if (font instanceof CompositeFont) {
msg = "Request to add " + font.getFamilyName(null) +
" with style " + style + " to family " + familyName;
} else {
msg = "Request to add " + font +
" with style " + style + " to family " + this;
}
FontUtilities.getLogger().info(msg);
}
/* Allow a lower-rank font only if its a file font
* from the exact same source as any previous font.
*/
if ((font.getRank() > familyRank) && !isFromSameSource(font)) {
if (FontUtilities.isLogging()) {
FontUtilities.getLogger()
.warning("Rejecting adding " + font +
" of lower rank " + font.getRank() +
" to family " + this +
" of rank " + familyRank);
}
return;
}
switch (style) {
case Font.PLAIN:
if (preferredWidth(font) && closerWeight(plain, font, style)) {
plain = font;
}
break;
case Font.BOLD:
if (preferredWidth(font) && closerWeight(bold, font, style)) {
bold = font;
}
break;
case Font.ITALIC:
if (preferredWidth(font) && closerWeight(italic, font, style)) {
italic = font;
}
break;
case Font.BOLD|Font.ITALIC:
if (preferredWidth(font) && closerWeight(bolditalic, font, style)) {
bolditalic = font;
}
break;
default:
break;
}
}
/**
* Returns the font for the values in the passed in AttributeSet.
* It is assumed the keys will be CSS.Attribute keys.
* <code>sc</code> is the StyleContext that will be messaged to get
* the font once the size, name and style have been determined.
*/
Font getFont(StyleContext sc, AttributeSet a, int defaultSize, StyleSheet ss) {
ss = getStyleSheet(ss);
int size = getFontSize(a, defaultSize, ss);
/*
* If the vertical alignment is set to either superscirpt or
* subscript we reduce the font size by 2 points.
*/
StringValue vAlignV = (StringValue)a.getAttribute
(CSS.Attribute.VERTICAL_ALIGN);
if ((vAlignV != null)) {
String vAlign = vAlignV.toString();
if ((vAlign.indexOf("sup") >= 0) ||
(vAlign.indexOf("sub") >= 0)) {
size -= 2;
}
}
FontFamily familyValue = (FontFamily)a.getAttribute
(CSS.Attribute.FONT_FAMILY);
String family = (familyValue != null) ? familyValue.getValue() :
Font.SANS_SERIF;
int style = Font.PLAIN;
FontWeight weightValue = (FontWeight) a.getAttribute
(CSS.Attribute.FONT_WEIGHT);
if ((weightValue != null) && (weightValue.getValue() > 400)) {
style |= Font.BOLD;
}
Object fs = a.getAttribute(CSS.Attribute.FONT_STYLE);
if ((fs != null) && (fs.toString().indexOf("italic") >= 0)) {
style |= Font.ITALIC;
}
if (family.equalsIgnoreCase("monospace")) {
family = Font.MONOSPACED;
}
Font f = sc.getFont(family, style, size);
if (f == null
|| (f.getFamily().equals(Font.DIALOG)
&& ! family.equalsIgnoreCase(Font.DIALOG))) {
family = Font.SANS_SERIF;
f = sc.getFont(family, style, size);
}
return f;
}