免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3437 | 回复: 2
打印 上一主题 下一主题

Java实现渐变和旋转效果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-08 15:24 |只看该作者 |倒序浏览
本帖最后由 willyomg 于 2011-03-08 20:29 编辑
  1. package com;

  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.GradientPaint;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Headless Exception;

  8. import javax.swing.JFrame;

  9. public class Demo extends JFrame {

  10. public Demo(String title) throws Headless Exception {
  11.   super(title);
  12.   this.setSize(500, 500);
  13.   this.setDefaultCloseOperation(3);
  14.   this.setLocationRelativeTo(null);
  15.   this.setVisible(true);
  16. }

  17. @Override
  18. public void paint(Graphics g) {
  19.   super.paint(g);
  20.   Graphics2D g2d = (Graphics2D) g;


  21. // 绘制渐变
  22.   // g2d.setPaint(new GradientPaint(0, 0, Color.RED,
  23.   // getWidth(),getHeight(), Color.YELLOW));
  24.   // g2d.fillRect(0, 0, getWidth(),getHeight());
  25.   // g2d.fillOval(250, 250, 200, 200);
  26.   g2d.setPaint(new GradientPaint(0, 0, Color.BLUE, 500, 0, Color.WHITE));

  27.   // g2d.fillOval(50, 50, 200, 200);
  28.   // 平移原点到图形环境的中心
  29.   g2d.translate(250, 250);
  30.   // g2d.setPaint(Color.RED);
  31.   // g2d.rotate();
  32.   g2d.setFont(new Font("宋体", Font.BOLD, 28));
  33.   // g2d.rotate(-20 * Math.PI / 180);
  34.   g2d.drawString("Java Graphics2D 实现文字渐变效果", -250, -200);
  35.   // g2d.fillRect(0, 0, getWidth(), getHeight());

  36.   // g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
  37.   // // 旋转文本

  38.   GradientPaint g1 = new GradientPaint(0, 0, Color.RED, 500, 0, Color.YELLOW);
  39.   GradientPaint g2 = new GradientPaint(0, 0, Color.BLUE, 500, 0, Color.GREEN);

  40.   for (int i = 0; i < 12; i++) {
  41.    g2d.rotate(30 * Math.PI / 180);
  42.    // g2d.setPaint(colors[i % 2]);
  43.    if (i % 2 == 0) {
  44.     g2d.setPaint(g1);
  45.    } else {
  46.     g2d.setPaint(g2);
  47.    }
  48.    g2d.drawString("Java 2D 旋转效果 ", 0, 0);
  49.   }
  50. }

  51. public static void main(String[] args) {
  52.   new Demo("Graphics");
  53. }
  54. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-03-08 19:23 |只看该作者
{:3_196:} 能不能以代码的形式贴 这样贴出来的有些符号被表情代替了》。。。

论坛徽章:
0
3 [报告]
发表于 2011-03-08 20:29 |只看该作者
回复 2# wb112200


    已改正,谢谢啦
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP