图像:
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* @version 1.33 2007-04-14
* @author Cay Horstmann
*/
public class ImageTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* A frame with an image component
*/
class ImageFrame extends JFrame {
public ImageFrame() {
setTitle("ImageTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add component to frame
ImageComponent component = new ImageComponent();
add(component);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
/**
* A component that displays a tiled image
*/
class ImageComponent extends JComponent {
public ImageComponent() {
// acquire the image
try { // 通过ImageIO类的静态方法read获得图片对象
image = ImageIO.read(new File("blue-ball.gif"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
if (image == null)
return;
// 获得图片的宽度
int imageWidth = image.getWidth(this);
// 获得图片高度
int imageHeight = image.getHeight(this);
// draw the image in the upper-left corner
// 描绘第一个图片
g.drawImage(image, 0, 0, null);
// tile the image across the component
for (int i = 0; i * imageWidth <= getWidth(); i++) {
for (int j = 0; j * imageHeight <= getHeight(); j++) {
if (i + j > 0) {// 描绘其他图片
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j
* imageHeight);
}
}
}
}
private Image image;
}
为文本设定特殊字符:
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* @version 1.33 2007-04-14
* @author Cay Horstmann
*/
public class FontTest
{
public static void main(String[] args)
{//事件调度
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* A frame with a text message component
*/
class FontFrame extends JFrame
{
public FontFrame()
{
setTitle("FontTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add component to frame
FontComponent component = new FontComponent();
add(component);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
/**
* A component that shows a centered message in a box.
*/
class FontComponent extends JComponent
{
public void paintComponent(Graphics g)
{ //获得Graphics2D对象
Graphics2D g2 = (Graphics2D) g;
String message = "Hello, World!";
//创建Font对象
Font f = new Font("Serif", Font.BOLD, 36);
//设置字体
g2.setFont(f);
// measure the size of the message
/*FontRenderContext 类是正确测量文本所需的信息容器。
* 因为将轮廓映射到像素的规则不同,而且应用程序提供的呈现提示不同,
* 所以文本的测量也有所不同。 */
FontRenderContext context = g2.getFontRenderContext();
/* 回指定 FontRenderContext 中指定 String 的逻辑边界。
逻辑边界包含 origin、ascent、advance 和 height,其中包括了 leading。
逻辑边界并不总是包围所有文本。例如,在某些语言和字体中,
accent 标记可以位于 ascent 之上,或 descent 之下。
要得到可视的边界框(它包围了所有文本),可以使用 TextLayout 的 getBounds 方法。 */
Rectangle2D bounds = f.getStringBounds(message, context);
// set (x,y) = top left corner of text
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
// add ascent to y to reach the baseline
double ascent = -bounds.getY();
double baseY = y + ascent;
// draw the message
//描绘mesaage
g2.drawString(message, (int) x, (int) baseY);
//设置画笔颜色
g2.setPaint(Color.LIGHT_GRAY);
// draw the baseline
//描绘基线
g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY));
// draw the enclosing rectangle
//描绘长方形框
Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
}
}