繪製三維文字—-java

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.