JAVA筆記【20131216】

一、顏色

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;
}

運行結果:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章