1. 程式人生 > >Java圖形程式設計學習(2)對JFrame的使用

Java圖形程式設計學習(2)對JFrame的使用

目錄

1、頂層視窗類(框架)JFrame

2、 在框架中顯示資訊

3、繪製2D圖形

4、設定顏色

5、文字使用特殊字型

1、頂層視窗類(框架)JFrame

(1)JFrame類是用於描述頂層視窗,JFrame就代表Swing中的視窗元件

(2)JFrame的修飾部件(按鈕,標題欄,圖示等)是由使用者的視窗系統繪製,不由Swing繪製

閱讀下面例子,需要理解的幾個API

EventQueue.invokeLater(new Runnable(){ ... }) 只需要將其看作是啟動Swing程式的作用即可

Component的API:

boolean isVisible()

void setVisible(boolean b) 設定顯示與否,一般元件預設是可見的,只有JFrame例外

void setSize(int width, int height)

void setLocation(int x, int y) 設定元件的位置。JFrame則是相對螢幕左上角而言,其它元件則是相對包含它的容器左上角而言

void setBounds(int x, int y, int w, int h)

Dimension getSize() 獲得當前元件的size屬性,又可以通過size獲得width和height

void setSize(Dimension d) 設定當前元件的size屬性

Window的API:

void toFront() 將該視窗顯示在其它視窗前面

void toBack()

boolean isLocationByPlatform()

void setLocationByPlatform(boolean b) 這個屬性在視窗顯示之前被設定,由平臺選擇一個合適的位置放該視窗

Frame的API:

boolean isResizable()

void setResiable(boolean b) 設定該屬性後,使用者可以對框架變大變小

String getTitle()

void setTitle(String s) 設定框架標題欄中的文字

Image getIconImage()

void setIconImage(Image img) 這個屬性確定了框架的圖示

boolean isUndecorated()

void setUndecorated(boolean b) 框架顯示前呼叫,true則該框架不會顯示標題欄和關閉按鈕等裝飾

int getExtendedState()

void setExtendedState(int state) 設定視窗狀態(Frame.NORMAL等5種)

Toolkit的API:

static Toolkit getDefaultToolkit() 返回預設的工具箱

Dimension getScreenSize() 返回使用者螢幕的尺寸

ImageIcon的API:

ImageIcon(String filename) 根據一個檔案路徑構造一個圖示,其影象儲存在一個檔案中

Image getImage() 獲得該圖示的影象
 

package GUI;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class JFrameDemo {
	public static void main(String[] args) {
		/*所有的Swing元件必須由事件分派執行緒進行配置*/
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				SimpleFrame frame = new SimpleFrame();//初始化視窗
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定:關閉視窗,則程式退出
				frame.setVisible(true);//顯示視窗
			}
		});
	}
}

class SimpleFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;
	public SimpleFrame() {
		this.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		/*setLocation(x,y)僅設定視窗顯示位置,相對螢幕左上角*/
//		this.setLocation(500, 500);
		
		/*setBounds(x,y,width,height)重置視窗顯示位置和大小*/
//		this.setBounds(200, 200, 500, 400);
		
		/*讓視窗系統控制視窗的位置*/
		this.setLocationByPlatform(true);
		
		/*設定視窗標題*/
		setTitle("貪吃蛇");
		
		/*先獲得螢幕大小,再設定視窗大小是螢幕大小的一半*/
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		setSize(screenWidth/2, screenHeight/2);
		
		/*設定視窗圖示*/
//		Image img = new ImageIcon("src/icon.jpg").getImage();
//		setIconImage(img);
		
		
	}
}

2、 在框架中顯示資訊(先在JComponent中繪製資訊,再將JComponent新增到JFrame中)

閱讀下面例子需要掌握的API

JFrame的API:

Container getContent() 返回JFrame的內容窗格物件(JFrame的內部有許多層次,內容窗格就是用來展示內容的,其它元件

就是放到內容窗格中的)

Component add(Component c) 將一個給定的元件新增到該框架的內容窗格中(JDK 1.5以下版本中,該方法會報錯)

Component的API:

void repaint() 重新繪製元件

Dimension getPreferredSize() 需要重寫該方法,獲得當前元件的首選大小

JComponent的API:

void paintComponent(Grphics g) 重寫這個方法,描述應該如何繪製自己的元件

Window的API:

void pack() 會考慮元件的首選大小,調整視窗大小

package GUI;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class NotHelloWorld {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				NotHelloWorldFrame frame = new NotHelloWorldFrame();//初始化視窗
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定:關閉視窗,則程式退出
				frame.setVisible(true);//顯示視窗
			}
		});
	}
}

class NotHelloWorldFrame extends JFrame{
	private static final long serialVersionUID = 1L;

	/**
	 * 新增訊息面板到框架的內容窗格中去
	 */
	public NotHelloWorldFrame() {
		this.add(new NotHelloWorldComponent());
		pack();//根據面板的首選大小,調整視窗大小
	}
}

class NotHelloWorldComponent extends JComponent{
	private static final long serialVersionUID = 1L;
	public static final int MESSAGE_X = 75;
	public static final int MESSAGE_Y = 100;
	
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;
	
	/**
	 * 給面板繪製資訊
	 */
	@Override
	public void paintComponent(Graphics g) {
		g.drawString("Not a Hello World Program", MESSAGE_X, MESSAGE_Y);
	}
	
	/**
	 * 返回面板的首選大小
	 */
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
}

3、繪製2D圖形

可能會用到的API

double getCenterX() 獲得矩形中心x座標值

double getMinX() 獲得最小的x座標值

double getMaxX() 獲得最大的x座標值

double getWidth() 獲得矩形的寬

double getHeight() 獲得矩形的高

double getX() 獲得矩形左上角的x座標

Rectangle2D.Double(double x,double y,double w,double h)利用給定的左上角和寬高,構造一個矩形

Rectangle2D.Float(float x,float y,float w,float h) 利用給定的左上角和寬高,構造一個矩形

Ellipse2D.Double(double x,double y,double w,double h) 利用給定的左上角和寬高的外接矩形,構造一個橢圓(寬高相同時就是圓)

Point2D.Double(double x,double y) 利用給定座標構造一個

Line2D.Double(Point2D start, Point2D end) 構造一條直線

Line2D.Double(double startX,double startY,double endX,double endY) 構造一條直線

package GUI;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class DrawTest {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				DrawFrame frame = new DrawFrame();//初始化視窗
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定:關閉視窗,則程式退出
				frame.setVisible(true);//顯示視窗
			}
		});
	}
}

@SuppressWarnings("serial")
class DrawFrame extends JFrame{
	public DrawFrame() {
		add(new DrawComponent());
		pack();
	}
}

@SuppressWarnings("serial")
class DrawComponent extends JComponent{
	private static final int DEFAULT_WIDTH = 500;
	private static final int DEFAULT_HEIGHT = 400;
	
	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		
		//繪製一個矩形
		double leftX = 100;
		double topY = 100;
		double width = 200;
		double height = 150;
		Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
		g2.draw(rect);//描繪邊框
		
		g2.setPaint(Color.RED);//設定顏色
		g2.fill(rect);//填充顏色,右側和下方少繪製一個畫素的顏色
		
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
}

4、設定顏色

(1)獲取和改變當前顏色,所有後續的繪圖操作都使用這個新顏色

Color getColor( )

void setColor( Color c )

建立一個顏色物件:Color( int r, int g, int b )

(2)獲取和設定這個圖形環境的繪製屬性,即設定畫筆的顏色

Paint getPaint( )

void setPaint( Paint p )

void fill( Shape s )

(3)背景色和前景色

setBackground( Color c )

setForeground( Color c )

5、文字使用特殊字型

package GUI;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class FontTest {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				JFrame frame = new FontFrame();//初始化視窗
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定:關閉視窗,則程式退出
				frame.setVisible(true);//顯示視窗
			}
		});
	}
}

@SuppressWarnings("serial")
class FontFrame extends JFrame{
	public FontFrame() {
		add(new FontComponent());
		pack();
	}
}

@SuppressWarnings("serial")
class FontComponent extends JComponent{
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		
		String msg = "Hello World";
		
		//建立一個新字型物件,字型名,字型風格,字型大小
		Font f = new Font("Serif", Font.BOLD, 36);
		g2.setFont(f);
		
		//----要想字串在面板的中央位置
		//返回指定字型特徵的字型繪製環境
		FontRenderContext context = g2.getFontRenderContext();
		//返回包圍字串的矩形
		Rectangle2D bounds = f.getStringBounds(msg, context);
		double x = (getWidth() - bounds.getWidth()) / 2;
		double y = (getHeight() - bounds.getHeight()) / 2; 
		double ascent = -bounds.getY();
		double baseY = y + ascent;
		g2.drawString(msg, (int)x, (int)baseY);
		
		//---顯示基線
		g2.draw(new Line2D.Double(x, baseY, x+bounds.getWidth(), baseY));
		
	}
}