Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 341|回复: 0

[Swing学习]Swing 酷炫文字

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-8 23:57:17 | 显示全部楼层 |阅读模式
    前两天玩三国,看到开头的文字有个灯光划过的艺术效果,又联想到前段时间看到的TextLayout,就来感觉了,顺手实现了下。

    原理介绍:
             其实很简单,分以下几步
    1、通过Shape shape = textLayout.getOutline(transform)取得一个text的shape信息。
    2、将这个shape设置为剪辑区域g2d.setClip(shape);
    3、使用一个渐进Paint填充当前区域GradientPaint paint = new GradientPaint(0, 0, Color.BLACK, 0, y, Color.WHITE, true);
             g2d.fill(this.getBounds());

    主要是2,将一个text的shape设置成当前Graphics的剪辑区域,这样每次填充的内容只有shape内的才会显示出来。
    至于闪烁效果就很简单了,可以和我一样,启动一个timer每次修改paint参数,repaint即可。
    根据paint的不同,就会有不同的发光效果,有兴趣的可以多试几种效果。
       
      
       
       
         
       

         
       
      
        效果图如下:


    代码如下:
      

    package
      text;


    import
      java.awt.BorderLayout;

    import
      java.awt.Color;

    import
      java.awt.Dimension;

    import
      java.awt.Font;

    import
      java.awt.GradientPaint;

    import
      java.awt.Graphics;

    import
      java.awt.Graphics2D;

    import
      java.awt.RenderingHints;

    import
      java.awt.Shape;

    import
      java.awt.event.ActionEvent;

    import
      java.awt.event.ActionListener;

    import
      java.awt.event.KeyEvent;

    import
      java.awt.font.FontRenderContext;

    import
      java.awt.font.TextLayout;

    import
      java.awt.geom.AffineTransform;

    import
      java.awt.geom.Rectangle2D;


    import
      javax.swing.BorderFactory;

    import
      javax.swing.JComponent;

    import
      javax.swing.JFrame;

    import
      javax.swing.JLabel;

    import
      javax.swing.JPanel;

    import
      javax.swing.JScrollPane;

    import
      javax.swing.JSplitPane;

    import
      javax.swing.JTextArea;

    import
      javax.swing.JTree;

    import
      javax.swing.KeyStroke;

    import
      javax.swing.Timer;


    /** */
    /**
      *
      * @author  zhangtao
      * @msn        zht_dream@hotmail.com
      * @mail    zht_dream@hotmail.com
      * Let"s Swing together.
      */


    public
      
    class
      GradientText
    extends
      JComponent

    {

        public static void main(String[] args) {
             GradientText gText = new GradientText("zht_dream");

             JPanel panel = new JPanel(new BorderLayout());
             JSplitPane splitPane = new JSplitPane();
             splitPane.setLeftComponent(new JScrollPane(new JTree()));
             JTextArea textArea = new JTextArea();
             textArea.setEnabled(false);
             StringBuffer text = new StringBuffer();
             text.append("good good study
    ");
             text.append("day day up

    ");
             text.append("MSN:zht_dream@hotmail.com
    ");
             text.append("Mail:zht_dream@hotmail.com
    ");
             text.append("                     ----zhangtao");
             textArea.setText(text.toString());
             textArea.setFont(new Font("Dialog", Font.ITALIC, 20));

             splitPane.setRightComponent(new JScrollPane(textArea));
             splitPane.setDividerLocation(100);

             panel.add(gText, BorderLayout.NORTH);
             panel.add(splitPane, BorderLayout.CENTER);

             JFrame frame = new JFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setContentPane(panel);
             frame.setSize(600, 500);
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
         }

         private int y = 0;
         private Font font = new Font("Times", Font.BOLD, 40);
         private String text = null;//new String("MSN:zht_dream@hotmail.com");

        ActionListener anAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 System.exit(-1);
             }
         };

        public GradientText(String text) {
             this.text = text;
             this.registerKeyboardAction(anAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, JComponent.WHEN_IN_FOCUSED_WINDOW);
             this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
             putClientProperty("order", true);
            Timer timer = new Timer(50, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                     boolean order = Boolean.parseBoolean(getClientProperty("order").toString());
                    if (order) {
                         y = y + getHeight() / 20;
                    } else {
                         y = y - getHeight() / 20;
                     }
                    if (y > getHeight()) {
                         putClientProperty("order", false);
                         y = getHeight();
                     }
                    if (y < 0) {
                         putClientProperty("order", true);
                         y = 0;
                     }
                     repaint();
                 }
             });
             timer.start();
         }

         @Override
        public Dimension getPreferredSize() {
             JLabel label = new JLabel(text);
             label.setFont(font);
             return label.getPreferredSize();
         }

         @Override
        protected void paintComponent(Graphics g) {
             super.paintComponent(g);
            if (text == null || font == null || text.trim().length() == 0) {
                 return;
             }
             Graphics2D g2d = (Graphics2D) g;
             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
             FontRenderContext frc = g2d.getFontRenderContext();
             TextLayout textLayout = new TextLayout(text, font, frc);
             Rectangle2D textBounds = textLayout.getBounds();
             double sw = textBounds.getWidth();
             double sh = textBounds.getHeight();
             Dimension componetSize = this.getSize();

             double sx = (componetSize.getWidth() - 5) / sw;
             double sy = (componetSize.getHeight() - 5) / sh;
             AffineTransform transform = new AffineTransform();
             transform.translate(2, (sh - textLayout.getDescent() + 5) * sy);
             transform.scale(sx, sy - 5 / componetSize.getWidth());
             Shape shape = textLayout.getOutline(transform);
             g2d.draw(shape);
             g2d.setClip(shape);
             GradientPaint paint = new GradientPaint(0, 0, Color.BLACK, 0, y, Color.WHITE, true);
             g2d.setPaint(paint);
             g2d.fill(this.getBounds());
         }

        public String getText() {
             return text;
         }

        public void setText(String text) {
             this.text = text;
             this.repaint();
         }
    }



      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/11/8/235717187.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 10:51 , Processed in 0.476923 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表