The instructions below ask several questions of you. Be sure that
you
understand the answers, but there is nothing to hand in. I will,
however, assume that you know and can apply the techniques introduced
in this lab.
<applet code=FuntFun.class width=500 height=500> </applet>
import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.font.*;
public class FontFun extends Applet implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
repaint();
}
public void paint(Graphics gnu)
{
centeredText("This is my text to be centered", gnu, 300);
}
public void centeredText(String text, Graphics g, int yloc)
{
FontMetrics fm = g.getFontMetrics();
Rectangle2D r2d = fm.getStringBounds(text, g);
g.drawString(text, (int)((getSize().getWidth()/2 -r2d.getWidth()/2)), yloc);
}
}
|
Label aLabel;
|
public void init()
{
setLayout(new GridLayout(20,1));
aLabel = new Label("This is a label");
add(aLabel);
aLabel.setAlignment(Label.CENTER);
}
|
Scrollbar sizeScroller;
|
public void init()
{
setLayout(new GridLayout(20,1));
aLabel = new Label("This is a label");
add(aLabel);
aLabel.setAlignment(Label.CENTER);
sizeScroller = new Scrollbar(Scrollbar.HORIZONTAL, 12, 1, 6, 40);
add(sizeScroller);
sizeScroller.addAdjustmentListener(this);
}
|
public void changeFontSize(Graphics g, int textSize)
{
Font f = getFont();
Font ff = new Font(f.getFontName(), Font.PLAIN, textSize);
g.setFont(ff);
}
|
public void paint(Graphics gnu)
{
changeFontSize(gnu, sizeScroller.getValue());
centeredText("This is my text to be centered", g, 300);
}
|
public void changeLabelFont(Label label, int textSize)
{
Font f = label.getFont();
Font ff = new Font(f.getFontName(), Font.PLAIN, textSize);
label.setFont(ff);
}
|
public void paint(Graphics gnu)
{
changeFontSize(gnu, sizeScroller.getValue());
changeLabelFont(aLabel, sizeScroller.getValue());
centeredText("This is my text to be centered", g, 300);
}
|
Within changeLabelFont, try replacing PLAIN with BOLD or ITALIC. Are the results as you expect?
Scrollbar angleScroller;
|
public void init()
{
setLayout(new GridLayout(20,1));
aLabel = new Label("This is a label");
add(aLabel);
aLabel.setAlignment(Label.CENTER);
sizeScroller = new Scrollbar(Scrollbar.HORIZONTAL, 12, 1, 6, 40);
add(sizeScroller);
sizeScroller.addAdjustmentListener(this);
angleScroller = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 360);
add(angleScroller);
angleScroller.addAdjustmentListener(this);
}
|
public void rotatedText(String text, int angle, int x, int y, Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
TextLayout layout = new TextLayout(text,
g2d.getFont(),
g2d.getFontRenderContext());
Dimension dim = getSize();
g2d.translate(x,y);
g2d.rotate(Math.toRadians(angle));
layout.draw(g2d, -layout.getAdvance() / 2, 0);
}
|
public void paint(Graphics gnu)
{
changeFontSize(gnu, sizeScroller.getValue());
centeredText("This is my text to be centered", gnu, 300);
changeLabelFont(aLabel, sizeScroller.getValue());
rotatedText("This is rotated", angleScroller.getValue(), 200, 400, gnu);
}
|