JAVA Graphics 文字繪制:變色、漸變、陰影、傾斜、立體


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;
/**
 * 會變色的文字
 */
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("會變色的文字");
        getContentPane().add(changeColorTextPanel);
        Thread thread = new Thread(changeColorTextPanel);// 創(chuàng)建線程對象
        thread.start();// 啟動線程對象
    }
 
    class ChangeColorTextPanel extends JPanel implements Runnable { // 創(chuàng)建內(nèi)部面板類
        Color color = new Color(0, 0, 255);
 
        public void paint(Graphics g) { // 重寫paint()方法
            Graphics2D g2 = (Graphics2D) g;// 轉(zhuǎn)換為Graphics2D類型
            String value = "《視頻學(xué)Java編程》";// 繪制的文本
            int x = 2; // 文本位置的橫坐標(biāo)
            int y = 90; // 文本位置的縱坐標(biāo)
            Font font = new Font("楷體", Font.BOLD, 40); // 創(chuàng)建字體對象
            g2.setFont(font); // 設(shè)置字體
            g2.setColor(color);// 設(shè)置顏色
            g2.drawString(value, x, y); // 繪制文本
        }
 
        public void run() {
            Random random = new Random();// 創(chuàng)建隨機(jī)數(shù)對象
            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)建顏色對象
                repaint();// 調(diào)用paint()方法
                try {
                    Thread.sleep(300);// 休眠300毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


image.png

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)部面板類
        public void paint(Graphics g) { // 重寫paint()方法
            Graphics2D g2 = (Graphics2D) g;// 轉(zhuǎn)換為Graphics2D類型
            String value = "Java全能";// 繪制的文本
            int x = 15; // 文本位置的橫坐標(biāo)
            int y = 60; // 文本位置的縱坐標(biāo)
            Font font = new Font("楷體", Font.BOLD, 60); // 創(chuàng)建字體對象
            g2.setFont(font); // 設(shè)置字體
            // 創(chuàng)建循環(huán)漸變的GraphientPaint對象
            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)建新的字體對象
            g2.setFont(font); // 設(shè)置字體
            x = 80; // 文本位置的橫坐標(biāo)
            y = 130; // 文本位置的縱坐標(biāo)
            value = "編程詞典";// 繪制的文本
            g2.drawString(value, x, y); // 繪制文本
        }
    }
}

image.png
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)部面板類
        public void paint(Graphics g) { // 重寫paint()方法
            String value = "編程詞典";
            int x = 16; // 文本位置的橫坐標(biāo)
            int y = 100; // 文本位置的縱坐標(biāo)
            Font font = new Font("華文行楷", Font.BOLD, 72); // 創(chuàng)建字體對象
            g.setFont(font); // 設(shè)置字體
            g.setColor(Color.GRAY);// 設(shè)置顏色為灰色
            int i = 0;// 循環(huán)變量
            g.drawString(value, x, y); // 繪制文本
            x -= 3;// 調(diào)整繪制點的橫坐標(biāo)值
            y -= 3;// 調(diào)整繪制點的縱坐標(biāo)值
            g.setColor(Color.BLACK);// 設(shè)置顏色黑色
            g.drawString(value, x, y); // 繪制文本
        }
    }
}

image.png
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)部面板類
        public void paint(Graphics g) { // 重寫paint()方法
            Graphics2D g2 = (Graphics2D)g;// 轉(zhuǎn)換為Graphics2D類型
            String value = "編程詞典";// 繪制的文本
            int x = 10; // 文本位置的橫坐標(biāo)
            int y = 170; // 文本位置的縱坐標(biāo)
            Font font = new Font("華文行楷", Font.BOLD, 72); // 創(chuàng)建字體對象
            g2.setFont(font); // 設(shè)置字體
            g2.shear(0.1, -0.4);// 傾斜畫布
            g2.drawString(value, x, y); // 繪制文本
        }
    }
}

image.png

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)部面板類
        public void paint(Graphics g) { // 重寫paint()方法
            String value = "明日科技";
            int x = 16; // 文本位置的橫坐標(biāo)
            int y = 100; // 文本位置的縱坐標(biāo)
            Font font = new Font("宋體", Font.BOLD, 72); // 創(chuàng)建字體對象
            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)整繪制點的橫坐標(biāo)值
                y+=1;// 調(diào)整繪制點的縱坐標(biāo)值
                i++;// 調(diào)整循環(huán)變量的值
            }
            g.setColor(Color.BLACK);// 設(shè)置顏色黑色
            g.drawString(value, x, y); // 繪制文本
        }
    }
}

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容