⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamedialog.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
字号:
// GameDialog.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved

package com.symbian.devnet.whist.awt;

import java.awt.*;
import java.awt.event.*;
import com.symbian.devnet.quartz.awt.*;

/** 
* GameDialog extends the Dialog class and provides a descriptive 
* dialog with a Quartz Look and Feel.
* <p>
* Make the GameDialog sit above the status bar and have height 100.
* Centre the text horizontally and distribute the lines evenly across 
* the available space	with horizontal and vertical insets of 15.
* If there are more than 6 lines, each line has less than 16 points 
* vertical space which makes the text look cramped, extend the dialog
* height up to allow each line exactly 16 points. If the total dialog 
* height exceeds 182, the dialog is interfered with by the menubar so
* beyond that height there is no alternative but to let the lines be 
* squashed together.
* </p>
* @author Symbian Devnet
*/

public class GameDialog extends Dialog implements ActionListener
{
	/** The fixed width of the canvas. This allows for an X_OFFSET of 15. */
	private static final int WIDTH = 210; 
	/** The fixed height of the canvas. */
	private static final int DEFAULT_HEIGHT = 100; 
	/** The Y_OFFESET. */
	private static final int Y_OFFSET = 15;
	/** The limit for the description text. */
	private static final int MIN_LINE_HEIGHT = 16; 
	/** 
	 * The maximum number of lines in the dialog before the height 
	 * is forced to increase above DEFAULT_HEIGHT.  This is calculated by
	 * DEFAULT_HEIGHT / MIN_LINE_HEIGHT.
	 */
	private static final int PREF_NUMBER = 6; 		
	/** 
	 * The MAX_HEIGHT variable determines the maximum height of the Dialog
	 * before it interfere with the MenuBar.
	 */
	private final int MAX_HEIGHT = 182;
	/** A button to allow the GameDialog to be dismissed, i.e. made invisible */
	private Button okBtn = new Button("OK");
	
	/** An array containing the lines of text which will be displayed in the GameDialog. */
	private  String[] descriptionTexts; 

	/**
	 * Creates a Modal GameDialog Box
	 * @param aParent is the parent QFrame of this Dialog
	 * @param aText is title of the GameDialog
	 * @param aDescriptionTexts is Description of the GameDialog
	 */
	GameDialog(Frame aParent, String aText, String[] aDescription)
	{
		super(aParent, aText);
		descriptionTexts = aDescription;
		int descLength = descriptionTexts.length;
		
		// Change the canvas height depending on the number of lines in the description text.
		// This ensures that the Dialog will resize as needed.
		int canvasHeight;
		canvasHeight = (descLength <= PREF_NUMBER) ? DEFAULT_HEIGHT : 
			Math.min(MAX_HEIGHT, MIN_LINE_HEIGHT * descLength);
  
		setLayout(new FlowLayout());
		Canvas canvas = new Canvas()
		{
			public void paint(Graphics g)
			{
				paintText(g);
			}
		};
	
		canvas.setSize(WIDTH, canvasHeight);
		GridPanel panel = new GridPanel();
		panel.add(canvas, 0, 0);
		panel.add(okBtn, 0, 1);
		okBtn.addActionListener(this);
		panel.setLocation(0, Y_OFFSET);
		add(panel);
		pack();						
	}
		
	/**
	 * This dismisses the GameDialog (by setting it to invisible) in response to
     * okBtn events sent when the "OK" button is pressed. It can be
     * overridden to listen for other events, but the overriding function should end with a
     * call to super.actionPerformed(ae).
     */
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == okBtn) 
			setVisible(false);					
	}

	/** 
 	 * paintText ensures that the GameBox description text is appropriately laid 
	 * out in the canvas 
	 * @param g represents the graphics context of the Canvas 
	 */
	private void paintText(Graphics g)
	{
		FontMetrics fm = g.getFontMetrics();
		int lineCount = descriptionTexts.length;
		for(int i = 0; i < lineCount; i++)
		{
			String desc = descriptionTexts[i];
			// choose an xOffset which centres the text
			int xOffset = (WIDTH - fm.stringWidth(desc))/2;
			// choose a suitable yOffset for this line 
			int yOffset =  DEFAULT_HEIGHT * (i + 1) /	(lineCount + 1) + 1;
			g.drawString(desc, xOffset, yOffset);
		}
	}				
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -