java怎么更改椭圆的角度_java – 如何实现椭圆GradientPaint?

绘制RadialGradientPaint时使用AffineTransform.这将需要变换的缩放实例.它可能最终看起来像这样:

import java.awt.*;

import java.awt.MultipleGradientPaint.CycleMethod;

import java.awt.geom.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.EmptyBorder;

public class OvalGradientPaint {

public static void main(String[] args) {

Runnable r = new Runnable() {

@Override

public void run() {

// the GUI as seen by the user (without frame)

JPanel gui = new JPanel(new BorderLayout());

gui.setBorder(new EmptyBorder(2, 3, 2, 3));

gui.add(new OvalGradientPaintSurface());

gui.setBackground(Color.WHITE);

JFrame f = new JFrame("Oval Gradient Paint");

f.add(gui);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// See https://stackoverflow.com/a/7143398/418556 for demo.

f.setLocationByPlatform(true);

// ensures the frame is the minimum size it needs to be

// in order display the components within it

f.pack();

// should be done last, to avoid flickering, moving,

// resizing artifacts.

f.setVisible(true);

}

};

// Swing GUIs should be created and updated on the EDT

// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

SwingUtilities.invokeLater(r);

}

}

class OvalGradientPaintSurface extends JPanel {

public int yScale = 150;

public int increment = 1;

RadialGradientPaint paint;

AffineTransform moveToOrigin;

OvalGradientPaintSurface() {

Point2D center = new Point2D.Float(100, 100);

float radius = 90;

float[] dist = {0.05f, .95f};

Color[] colors = {Color.RED, Color.MAGENTA.darker()};

paint = new RadialGradientPaint(center, radius, dist, colors,CycleMethod.REFLECT);

moveToOrigin = AffineTransform.

getTranslateInstance(-100d, -100d);

ActionListener listener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

if (increment < 0) {

increment = (yScale < 50 ? -increment : increment);

} else {

increment = (yScale > 150 ? -increment : increment);

}

yScale += increment;

repaint();

}

};

Timer t = new Timer(15, listener);

t.start();

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

AffineTransform moveToCenter = AffineTransform.

getTranslateInstance(getWidth()/2d, getHeight()/2d);

g2.setPaint(paint);

double y = yScale/100d;

double x = 1/y;

AffineTransform at = AffineTransform.getScaleInstance(x, y);

// We need to move it to the origin, scale, and move back.

// Counterintutitively perhaps, we concatentate 'in reverse'.

moveToCenter.concatenate(at);

moveToCenter.concatenate(moveToOrigin);

g2.setTransform(moveToCenter);

// fudge factor of 3 here, to ensure the scaling of the transform

// does not leave edges unpainted.

g2.fillRect(-getWidth(), -getHeight(), getWidth()*3, getHeight()*3);

}

@Override

public Dimension getPreferredSize() {

return new Dimension(500, 200);

}

}

原始图片:应用程序的原始静态(无聊)’屏幕截图’.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值