import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Panel;
public class Text3DPanel extends Panel implements Runnable {
private Image image; //import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Panel;
public class Text3DPanel extends Panel implements Runnable {
private Image image; //繪製文字的Image對象
private Graphics graphics; //繪製文字的Graphics對象
private Thread thread; //顯示三維文字線程
private int width,height; //顯示寬度、高度
private String message; //顯示資訊
private int fontSize; //文字尺寸
private Font font; //字型
private String[] fonts;
public Text3DPanel(String message,int width,int height){
this.message = message;
this.width = width;
this.height = height;
init();
start();
}
public void init() {
image = createImage(width, height); //得到Image執行個體
// graphics= image.getGraphics(); //得到Grahpics執行個體
if (message == null) { //如果資訊為空白
message="三維文字"; //設定預設資訊
}
fontSize = 30; //設定字型大小
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = eq.getAvailableFontFamilyNames();
// fonts = Toolkit.getDefaultToolkit().getFontList();
}
public void start() {
if (thread == null) {
thread = new Thread(this); //執行個體化線程
thread.start(); //運行線程
}
}
public void run() { //線程運行主體
while (thread != null) {
try {
Thread.sleep(300L); //線程休眠
} catch (InterruptedException ex) {
}
repaint(); //重繪螢幕
}
}
public void update(Graphics g) {
int index = (int)(Math.random()*fonts.length);
font = new Font(fonts[index], 1, fontSize); //得到字型執行個體
// font = new Font("TimesRoman", 1, fontSize); //得到字型執行個體
g.setFont(font); //設定顯示字型
int j = (int) (255 * Math.random()); //變數,用於產生漸層顏色
int k = (int) (255 * Math.random());
int l = (int) (255 * Math.random());
// int j = 200;
// int k = 192;
// int l = 200;
System.out.println(j+","+k+","+l+","+index);
g.setColor(Color.black); //設定使用中色彩
g.fillRect(0, 0, width, height); //填充背景
//繪製了6層底色字串
for (int i = 0; i < 6; i++) { //三維深度
g.setColor( //設定漸層顏色
new Color(
255 - ((255 - j) * i) / 10,
255 - ((255 - k) * i) / 10,
255 - ((255 - l) * i) / 10));
g.drawString(message, 15 - i, height - 15-i); //繪製字串
}
//把字串覆蓋到螢幕
g.drawImage(image, 0, 0, this); //繪製Image到螢幕
}
public void paint(Graphics g) {
update(g);
}
}Image對象
private Graphics graphics; //繪製文字的Graphics對象
private Thread thread; //顯示三維文字線程
private int width,height; //顯示寬度、高度
private String message; //顯示資訊
private int fontSize; //文字尺寸
private Font font; //字型
private String[] fonts;
public Text3DPanel(String message,int width,int height){
this.message = message;
this.width = width;
this.height = height;
init();
start();
}
public void init() {
image = createImage(width, height); //得到Image執行個體
// graphics= image.getGraphics(); //得到Grahpics執行個體
if (message == null) { //如果資訊為空白
message="三維文字"; //設定預設資訊
}
fontSize = 30; //設定字型大小
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = eq.getAvailableFontFamilyNames();
// fonts = Toolkit.getDefaultToolkit().getFontList();
}
public void start() {
if (thread == null) {
thread = new Thread(this); //執行個體化線程
thread.start(); //運行線程
}
}
public void run() { //線程運行主體
while (thread != null) {
try {
Thread.sleep(300L); //線程休眠
} catch (InterruptedException ex) {
}
repaint(); //重繪螢幕
}
}
public void update(Graphics g) {
int index = (int)(Math.random()*fonts.length);
font = new Font(fonts[index], 1, fontSize); //得到字型執行個體
// font = new Font("TimesRoman", 1, fontSize); //得到字型執行個體
g.setFont(font); //設定顯示字型
int j = (int) (255 * Math.random()); //變數,用於產生漸層顏色
int k = (int) (255 * Math.random());
int l = (int) (255 * Math.random());
// int j = 200;
// int k = 192;
// int l = 200;
System.out.println(j+","+k+","+l+","+index);
g.setColor(Color.black); //設定使用中色彩
g.fillRect(0, 0, width, height); //填充背景
//繪製了6層底色字串
for (int i = 0; i < 6; i++) { //三維深度
g.setColor( //設定漸層顏色
new Color(
255 - ((255 - j) * i) / 10,
255 - ((255 - k) * i) / 10,
255 - ((255 - l) * i) / 10));
g.drawString(message, 15 - i, height - 15-i); //繪製字串
}
//把字串覆蓋到螢幕
g.drawImage(image, 0, 0, this); //繪製Image到螢幕
}
public void paint(Graphics g) {
update(g);
}
}