Java实现渐变和旋转效果

package com;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;

import javax.swing.JFrame;

public class Demo extends JFrame {

 public Demo(String title) throws HeadlessException {
  super(title);
  this.setSize(500, 500);
  this.setDefaultCloseOperation(3);
  this.setLocationRelativeTo(null);
  this.setVisible(true);
 }

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

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

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

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

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

  for (int i = 0; i < 12; i++) {
   g2d.rotate(30 * Math.PI / 180);
   // g2d.setPaint(colors[i % 2]);
   if (i % 2 == 0) {
    g2d.setPaint(g1);
   } else {
    g2d.setPaint(g2);
   }
   g2d.drawString("Java 2D 旋转效果 ", 0, 0);
  }
 }

 public static void main(String[] args) {
  new Demo("Graphics");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值