一、颜色
Graphics2D类的setPaint方法可以设置图形的绘制颜色。java.awt.Color类用于定义颜色。java.awt.SystemColor类中预定义了很多与当前系统颜色相关的颜色。
图形填充:
直接调用Graphics2D类的fill方法,可以直接使用颜色填充图形。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class SimpleFrameTest04
{
public static void main(String[] args)
{
SimpleFrame sp = new SimpleFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
MyPanel mp = new MyPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //设置组件宽度和高度
//setResizable(false); //设置组件大小是否可调节
add(mp);
}
public static final int DEFAULT_WIDTH = 750;
public static final int DEFAULT_HEIGHT = 350;
}
class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D gps2D = (Graphics2D)g ;
//图形颜色
gps2D.setPaint(Color.BLACK);
//背景色
gps2D.setBackground(Color.BLUE); //好像没成功
//绘制矩形
Rectangle2D r2d = new Rectangle2D.Double(leftX,topY,width,height);
gps2D.draw(r2d);
//绘制对角线
Line2D l2d = new Line2D.Double(leftX,topY,leftX+width,topY+height);
gps2D.draw(l2d);
//绘制椭圆
Ellipse2D e2d = new Ellipse2D.Double(leftX,topY,width,height);
gps2D.draw(e2d);
//绘制圆
double radius = Point2D.distance(leftX,topY,leftX+width,topY+height)/2;
double centX = r2d.getCenterX();
double centY = r2d.getCenterY();
Ellipse2D cyc = new Ellipse2D.Double();
cyc.setFrameFromCenter(centX,centY,centX+radius,centY+radius);
gps2D.setPaint(Color.LIGHT_GRAY);
gps2D.draw(cyc);
//图形填充
gps2D.setPaint(Color.LIGHT_GRAY);
gps2D.fill(cyc);
gps2D.setPaint(Color.BLACK);
gps2D.fill(r2d);
gps2D.setPaint(Color.GRAY);
gps2D.fill(e2d);
}
public static final double leftX = 200;
public static final double topY = 100;
public static final double width =200;
public static final double height = 100;
}
运行结果:
二、为文本设定字体
Graphics类的setFont方法可以设定想显示的字体类型。一台计算机上所允许使用的字体可以调用GraphicsEnvironment类的getAvailbleFontFamilyNames方法。
AWT中定义了五个逻辑字体名:SansSerif,Serif,Monospaced,Dialog,DialogInput 。
字体映射定义在Java安装目录下的jre/lib/fontconfig.properties文件中。
字体风格:Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC 对应常规、加粗、斜体、加粗斜体。
对于字体排版有上坡度、下坡度、基线、行间距。
一个字符串显示占据的宽和高的像素数量取决于三个因素:字体、字符串大小以及绘制字体的设备。
获得字体设备的属性描述对象,需调用Graphics2D类中的getFontRenderContext方法,返回一个FontRenderContext对象,将该对象和显示的字符串传给Font类的getStringBounds方法,将返回包围字符串的矩形。
FontRenderContext cont = gps2D.getFontRenderContext();
Rectangle2D rect = tempF.getStringBounds(message,cont);
获取字符串的宽度、高度和上坡度。
高度:double sHeight = rect.getHeight();
宽度:double sWidth = rect.getWidth();
上坡度:double ascent = -rect.getY();
获取字符串的下坡度以及行间距。
首先使用Font类的getLineMetrics方法获取LineMetrics类对象
LineMetrics lms = tempF.getLineMetrics(message,cont);
然后再获取下坡度和行间距
下坡度:float descent = lms.getDescent();
行间距:float lead = lms.getLeading();
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
public class SimpleFrameTest05
{
public static void main(String[] args)
{
SimpleFrame sp = new SimpleFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
MyPanel mp = new MyPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //设置组件宽度和高度
//setResizable(false); //设置组件大小是否可调节
add(mp);
}
public static final int DEFAULT_WIDTH = 650;
public static final int DEFAULT_HEIGHT = 450;
}
class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D gps2D = (Graphics2D)g ;
//图形颜色
gps2D.setPaint(Color.BLACK);
//背景色
gps2D.setBackground(Color.BLUE); //好像没成功
//绘制矩形
Rectangle2D r2d = new Rectangle2D.Double(leftX,topY,width,height);
gps2D.draw(r2d);
//绘制对角线
Line2D l2d = new Line2D.Double(leftX,topY,leftX+width,topY+height);
gps2D.draw(l2d);
//绘制椭圆
Ellipse2D e2d = new Ellipse2D.Double(leftX,topY,width,height);
gps2D.draw(e2d);
//绘制圆
double radius = Point2D.distance(leftX,topY,leftX+width,topY+height)/2;
double centX = r2d.getCenterX();
double centY = r2d.getCenterY();
Ellipse2D cyc = new Ellipse2D.Double();
cyc.setFrameFromCenter(centX,centY,centX+radius,centY+radius);
gps2D.setPaint(Color.LIGHT_GRAY);
gps2D.draw(cyc);
//图形填充
gps2D.setPaint(Color.LIGHT_GRAY);
gps2D.fill(cyc);
gps2D.setPaint(Color.BLACK);
gps2D.fill(r2d);
gps2D.setPaint(Color.GRAY);
gps2D.fill(e2d);
//设置字体
Font tempF = new Font("SansSerif",Font.BOLD,24);
gps2D.setFont(tempF);
String message = "Shape World";
FontRenderContext cont = gps2D.getFontRenderContext();
Rectangle2D rect = tempF.getStringBounds(message,cont);
//高度、宽度和上坡度
double sHeight = rect.getHeight();
double sWidth = rect.getWidth();
double ascent = -rect.getY();
LineMetrics lms = tempF.getLineMetrics(message,cont);
//下坡度和行间距
float descent = lms.getDescent();
float lead = lms.getLeading();
gps2D.drawString("Shape,World",225,50);
Rectangle2D rS2d = new Rectangle2D.Double(225,50-ascent,sWidth,sHeight);
gps2D.draw(rS2d);
Line2D lS2d = new Line2D.Double(225,50,225+sWidth,50);
gps2D.draw(lS2d);
Line2D lS2dd = new Line2D.Double(225,50+descent,225+sWidth,50+descent);
gps2D.draw(lS2dd);
}
public static final double leftX = 200;
public static final double topY = 150;
public static final double width =200;
public static final double height = 100;
}
运行结果:
如果图像存储在本地文件中,就调用
String fileName = "...";
Image image = new ImageIO.read(new File(fileName));
如果不在本地,就必须提供URL
String urlName = "...";
Image image = new ImageIO.read(new URL(urlName));
然后调用Graphics2D类的drawImage方法即可显示图片
gps2D.drawImage(image,0,0,null);
对于需要平铺的图片,可使用Graphics2D类的copyArea方法
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
public class SimpleFrameTest06
{
public static void main(String[] args)
{
SimpleFrame sp = new SimpleFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
MyPanel mp = new MyPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //设置组件宽度和高度
//setResizable(false); //设置组件大小是否可调节
add(mp);
}
public static final int DEFAULT_WIDTH = 1000;
public static final int DEFAULT_HEIGHT = 600;
}
class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
/*if(image == null)
return;*/
Graphics2D gps2D = (Graphics2D)g ;
String fileName = "2b.jpg";
System.out.println(getWidth());
System.out.println(getHeight());
try
{
image = ImageIO.read(new File(fileName));
gps2D.drawImage(image,0,0,null);
}
catch(IOException e)
{
e.printStackTrace();
}
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
System.out.println("imageWidth="+imageWidth);
System.out.println("imageHeight="+imageHeight);
for(int i=0;i*imageWidth<=getWidth();i++)
for(int j=0;j*imageHeight<=getHeight();j++)
if(i+j>0)
gps2D.copyArea(0,0,imageWidth,imageHeight,i*imageWidth,j*imageHeight);
}
private Image image;
}
运行结果: