Run the following program.
1.2 beta4-A : It works, but if ANTIALIAS flag set to ON, drawString does not
display String.
1.2 beta3-N : It works correctly. srawString displays String at any
condiyions.
program:
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
class FontEffectTest extends Frame implements ActionListener, ItemListener {
String[] strName = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" };
String[] strSize = { "8", "12", "16", "20", "24", "32", "40", "48", "56", "64" };
String[] strStyle = { "Plain", "Bold", "Italic", "BoldItalic" };
int[] nStyle = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC };
String[] strRender = { "SPEED", "QUALITY", "DEFAULT" };
int[] nRender = { Graphics2D.RENDER_SPEED, Graphics2D.RENDER_QUALITY,
Graphics2D.RENDER_DEFAULT };
String[] strAntiA = { "ON", "OFF", "DEFAULT" };
int[] nAntiA = { Graphics2D.ANTIALIAS_ON, Graphics2D.ANTIALIAS_OFF,
Graphics2D.ANTIALIAS_DEFAULT };
Font ftDraw;
String strDraw;
MyCanvas cv;
TextField tf;
List lsName;
List lsSize;
List lsStyle;
List lsRender;
List lsAntiA;
public FontEffectTest() {
lsName = new List(5);
lsSize = new List(5);
lsStyle = new List(5);
lsRender = new List(5);
lsAntiA = new List(5);
initList(lsName, strName);
initList(lsSize, strSize);
initList(lsStyle, strStyle);
initList(lsRender, strRender);
initList(lsAntiA, strAntiA);
lsName.addItemListener(this);
lsSize.addItemListener(this);
lsStyle.addItemListener(this);
lsRender.addItemListener(this);
lsAntiA.addItemListener(this);
strDraw = new String("Output Field");
ftDraw = new Font(strName[0], nStyle[0], Integer.parseInt(strSize[0]));
tf = new TextField("Input String Here.");
tf.addActionListener(this);
cv = new MyCanvas();
cv.setSize(480, 200);
Panel p = new Panel(new FlowLayout());
p.add(lsName);
p.add(lsSize);
p.add(lsStyle);
p.add(lsRender);
p.add(lsAntiA);
add("North", cv);
add("Center", tf);
add("South", p );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
pack();
show();
}
public void actionPerformed(ActionEvent e) {
strDraw = tf.getText();
cv.repaint();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == lsName || e.getSource() == lsSize || e.getSource() == lsStyle ) {
ftDraw = new Font( (String)lsName.getSelectedItem(),
nStyle[lsStyle.getSelectedIndex()],
Integer.parseInt((String)lsSize.getSelectedItem()));
}
cv.repaint();
}
private void initList( List ls, String[] str ) {
for (int i=0; i < str.length; i++) {
ls.add(str[i]);
}
ls.select(0);
}
class MyCanvas extends Canvas {
public MyCanvas() {
setBackground(Color.white);
}
public void paint(Graphics g) {
g.clearRect(0, 0, getSize().width, getSize().height);
Graphics2D g2 = (Graphics2D)g;
Font ftTip = new Font("SansSerif", Font.PLAIN, 16);
TextLayout tips1 = new TextLayout("drawString(TextLayout):", ftTip);
g2.drawString(tips1, 0, 20);
TextLayout tips2 = new TextLayout("setFont & drawString(String):", ftTip);
g2.drawString(tips2, 0, 120);
g2.setRenderingHints(Graphics2D.RENDERING,
nRender[lsRender.getSelectedIndex()]);
g2.setRenderingHints(Graphics2D.ANTIALIASING,
nAntiA[lsAntiA.getSelectedIndex()]);
g2.setFont(ftDraw);
g2.drawString(strDraw, 0, 175);
TextLayout tl = new TextLayout(strDraw, ftDraw);
g2.drawString(tl, 0, 75);
}
}
public static void main(String[] args) {
FontEffectTest obj = new FontEffectTest();
}
}
1.2 beta4-A : It works, but if ANTIALIAS flag set to ON, drawString does not
display String.
1.2 beta3-N : It works correctly. srawString displays String at any
condiyions.
program:
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
class FontEffectTest extends Frame implements ActionListener, ItemListener {
String[] strName = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" };
String[] strSize = { "8", "12", "16", "20", "24", "32", "40", "48", "56", "64" };
String[] strStyle = { "Plain", "Bold", "Italic", "BoldItalic" };
int[] nStyle = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC };
String[] strRender = { "SPEED", "QUALITY", "DEFAULT" };
int[] nRender = { Graphics2D.RENDER_SPEED, Graphics2D.RENDER_QUALITY,
Graphics2D.RENDER_DEFAULT };
String[] strAntiA = { "ON", "OFF", "DEFAULT" };
int[] nAntiA = { Graphics2D.ANTIALIAS_ON, Graphics2D.ANTIALIAS_OFF,
Graphics2D.ANTIALIAS_DEFAULT };
Font ftDraw;
String strDraw;
MyCanvas cv;
TextField tf;
List lsName;
List lsSize;
List lsStyle;
List lsRender;
List lsAntiA;
public FontEffectTest() {
lsName = new List(5);
lsSize = new List(5);
lsStyle = new List(5);
lsRender = new List(5);
lsAntiA = new List(5);
initList(lsName, strName);
initList(lsSize, strSize);
initList(lsStyle, strStyle);
initList(lsRender, strRender);
initList(lsAntiA, strAntiA);
lsName.addItemListener(this);
lsSize.addItemListener(this);
lsStyle.addItemListener(this);
lsRender.addItemListener(this);
lsAntiA.addItemListener(this);
strDraw = new String("Output Field");
ftDraw = new Font(strName[0], nStyle[0], Integer.parseInt(strSize[0]));
tf = new TextField("Input String Here.");
tf.addActionListener(this);
cv = new MyCanvas();
cv.setSize(480, 200);
Panel p = new Panel(new FlowLayout());
p.add(lsName);
p.add(lsSize);
p.add(lsStyle);
p.add(lsRender);
p.add(lsAntiA);
add("North", cv);
add("Center", tf);
add("South", p );
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
pack();
show();
}
public void actionPerformed(ActionEvent e) {
strDraw = tf.getText();
cv.repaint();
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == lsName || e.getSource() == lsSize || e.getSource() == lsStyle ) {
ftDraw = new Font( (String)lsName.getSelectedItem(),
nStyle[lsStyle.getSelectedIndex()],
Integer.parseInt((String)lsSize.getSelectedItem()));
}
cv.repaint();
}
private void initList( List ls, String[] str ) {
for (int i=0; i < str.length; i++) {
ls.add(str[i]);
}
ls.select(0);
}
class MyCanvas extends Canvas {
public MyCanvas() {
setBackground(Color.white);
}
public void paint(Graphics g) {
g.clearRect(0, 0, getSize().width, getSize().height);
Graphics2D g2 = (Graphics2D)g;
Font ftTip = new Font("SansSerif", Font.PLAIN, 16);
TextLayout tips1 = new TextLayout("drawString(TextLayout):", ftTip);
g2.drawString(tips1, 0, 20);
TextLayout tips2 = new TextLayout("setFont & drawString(String):", ftTip);
g2.drawString(tips2, 0, 120);
g2.setRenderingHints(Graphics2D.RENDERING,
nRender[lsRender.getSelectedIndex()]);
g2.setRenderingHints(Graphics2D.ANTIALIASING,
nAntiA[lsAntiA.getSelectedIndex()]);
g2.setFont(ftDraw);
g2.drawString(strDraw, 0, 175);
TextLayout tl = new TextLayout(strDraw, ftDraw);
g2.drawString(tl, 0, 75);
}
}
public static void main(String[] args) {
FontEffectTest obj = new FontEffectTest();
}
}