如何实现椭圆形的GradientPaint? [英] How to implement oval GradientPaint?

查看:1924
本文介绍了如何实现椭圆形的GradientPaint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,在Java命名类的RadialGradientPaint ,我们可以用它来有一个渐变绘画圈。

We know that there are a class named RadialGradientPaint in Java and we can use it to have a gradient painting for circle.

不过,我想有一个椭圆形(椭圆形)的GradientPaint 。如何实现椭圆的GradientPaint

But I want to have an oval (ellipse) GradientPaint. How to implement oval GradientPaint?

推荐答案

使用的<一个href=\"http://docs.oracle.com/javase/tutorial/2d/advanced/transforming.html\"><$c$c>AffineTransform绘制的RadialGradientPaint时。这将需要的变换尺度实例。它可能最终看起来是这样的:

Use an AffineTransform when drawing the RadialGradientPaint. This would require a scale instance of the transform. It might end up looking something like this:

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 http://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);
    }
}

原图:原来的静态(无聊)的应用程序屏幕截图

Original image: The original static (boring) 'screen shot' of the app.

这篇关于如何实现椭圆形的GradientPaint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆