package cn;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 會(huì)變色的文字
*/
public class ChangeColorTextFrame extends JFrame {
private ChangeColorTextPanel changeColorTextPanel = new ChangeColorTextPanel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeColorTextFrame frame = new ChangeColorTextFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
ChangeColorTextFrame frame=new ChangeColorTextFrame();
frame.setVisible(true);
}
public ChangeColorTextFrame() {
super();
setBounds(100, 100, 400, 205);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("會(huì)變色的文字");
getContentPane().add(changeColorTextPanel);
Thread thread = new Thread(changeColorTextPanel);// 創(chuàng)建線程對(duì)象
thread.start();// 啟動(dòng)線程對(duì)象
}
class ChangeColorTextPanel extends JPanel implements Runnable { // 創(chuàng)建內(nèi)部面板類(lèi)
Color color = new Color(0, 0, 255);
public void paint(Graphics g) { // 重寫(xiě)paint()方法
Graphics2D g2 = (Graphics2D) g;// 轉(zhuǎn)換為Graphics2D類(lèi)型
String value = "《視頻學(xué)Java編程》";// 繪制的文本
int x = 2; // 文本位置的橫坐標(biāo)
int y = 90; // 文本位置的縱坐標(biāo)
Font font = new Font("楷體", Font.BOLD, 40); // 創(chuàng)建字體對(duì)象
g2.setFont(font); // 設(shè)置字體
g2.setColor(color);// 設(shè)置顏色
g2.drawString(value, x, y); // 繪制文本
}
public void run() {
Random random = new Random();// 創(chuàng)建隨機(jī)數(shù)對(duì)象
while (true) {
int R = random.nextInt(256);// 隨機(jī)產(chǎn)生顏色的R值
int G = random.nextInt(256);// 隨機(jī)產(chǎn)生顏色的G值
int B = random.nextInt(256);// 隨機(jī)產(chǎn)生顏色的B值
color = new Color(R, G, B);// 創(chuàng)建顏色對(duì)象
repaint();// 調(diào)用paint()方法
try {
Thread.sleep(300);// 休眠300毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package cn;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 漸變效果的文字
*/
public class GradientTextFrame extends JFrame {
private GradientTextPanel gradientTextPanel = new GradientTextPanel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GradientTextFrame frame = new GradientTextFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GradientTextFrame() {
super();
setBounds(100, 100, 365, 205);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("漸變效果的文字");
getContentPane().add(gradientTextPanel);
}
class GradientTextPanel extends JPanel { // 創(chuàng)建內(nèi)部面板類(lèi)
public void paint(Graphics g) { // 重寫(xiě)paint()方法
Graphics2D g2 = (Graphics2D) g;// 轉(zhuǎn)換為Graphics2D類(lèi)型
String value = "Java全能";// 繪制的文本
int x = 15; // 文本位置的橫坐標(biāo)
int y = 60; // 文本位置的縱坐標(biāo)
Font font = new Font("楷體", Font.BOLD, 60); // 創(chuàng)建字體對(duì)象
g2.setFont(font); // 設(shè)置字體
// 創(chuàng)建循環(huán)漸變的GraphientPaint對(duì)象
GradientPaint paint = new GradientPaint(20, 20, Color.BLUE, 100,120, Color.RED, true);
g2.setPaint(paint);// 設(shè)置漸變
g2.drawString(value, x, y); // 繪制文本
font = new Font("華文行楷", Font.BOLD, 60); // 創(chuàng)建新的字體對(duì)象
g2.setFont(font); // 設(shè)置字體
x = 80; // 文本位置的橫坐標(biāo)
y = 130; // 文本位置的縱坐標(biāo)
value = "編程詞典";// 繪制的文本
g2.drawString(value, x, y); // 繪制文本
}
}
}
package cn;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 陰影效果的文字
*/
public class ShadowTextFrame extends JFrame {
private ShadowTextPanel shadowTextPanel = new ShadowTextPanel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShadowTextFrame frame = new ShadowTextFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShadowTextFrame() {
super();
setBounds(100, 100, 354, 205);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("陰影效果的文字");
getContentPane().add(shadowTextPanel);
}
class ShadowTextPanel extends JPanel { // 創(chuàng)建內(nèi)部面板類(lèi)
public void paint(Graphics g) { // 重寫(xiě)paint()方法
String value = "編程詞典";
int x = 16; // 文本位置的橫坐標(biāo)
int y = 100; // 文本位置的縱坐標(biāo)
Font font = new Font("華文行楷", Font.BOLD, 72); // 創(chuàng)建字體對(duì)象
g.setFont(font); // 設(shè)置字體
g.setColor(Color.GRAY);// 設(shè)置顏色為灰色
int i = 0;// 循環(huán)變量
g.drawString(value, x, y); // 繪制文本
x -= 3;// 調(diào)整繪制點(diǎn)的橫坐標(biāo)值
y -= 3;// 調(diào)整繪制點(diǎn)的縱坐標(biāo)值
g.setColor(Color.BLACK);// 設(shè)置顏色黑色
g.drawString(value, x, y); // 繪制文本
}
}
}
package cn;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 傾斜效果文字
*/
public class ShearTextFrame extends JFrame {
private ShearTextPanel shearTextPanel = new ShearTextPanel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShearTextFrame frame = new ShearTextFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShearTextFrame() {
super();
setBounds(100, 100, 365, 205);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("傾斜效果的文字");
getContentPane().add(shearTextPanel);
}
class ShearTextPanel extends JPanel { // 創(chuàng)建內(nèi)部面板類(lèi)
public void paint(Graphics g) { // 重寫(xiě)paint()方法
Graphics2D g2 = (Graphics2D)g;// 轉(zhuǎn)換為Graphics2D類(lèi)型
String value = "編程詞典";// 繪制的文本
int x = 10; // 文本位置的橫坐標(biāo)
int y = 170; // 文本位置的縱坐標(biāo)
Font font = new Font("華文行楷", Font.BOLD, 72); // 創(chuàng)建字體對(duì)象
g2.setFont(font); // 設(shè)置字體
g2.shear(0.1, -0.4);// 傾斜畫(huà)布
g2.drawString(value, x, y); // 繪制文本
}
}
}
package cn;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 立體效果文字
*/
public class SolidTextFrame extends JFrame {
private SolidTextPanel solidTextPanel = new SolidTextPanel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SolidTextFrame frame = new SolidTextFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SolidTextFrame() {
super();
setBounds(100, 100, 354, 205);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("立體效果的文字");
getContentPane().add(solidTextPanel);
}
class SolidTextPanel extends JPanel { // 創(chuàng)建內(nèi)部面板類(lèi)
public void paint(Graphics g) { // 重寫(xiě)paint()方法
String value = "明日科技";
int x = 16; // 文本位置的橫坐標(biāo)
int y = 100; // 文本位置的縱坐標(biāo)
Font font = new Font("宋體", Font.BOLD, 72); // 創(chuàng)建字體對(duì)象
g.setFont(font); // 設(shè)置字體
g.setColor(Color.GRAY);// 設(shè)置顏色為灰色
int i = 0;// 循環(huán)變量
while (i<8){
g.drawString(value, x, y); // 繪制文本
x+=1;// 調(diào)整繪制點(diǎn)的橫坐標(biāo)值
y+=1;// 調(diào)整繪制點(diǎn)的縱坐標(biāo)值
i++;// 調(diào)整循環(huán)變量的值
}
g.setColor(Color.BLACK);// 設(shè)置顏色黑色
g.drawString(value, x, y); // 繪制文本
}
}
}