GradientPaint 工作异常

GradientPaint Working Strangely

在使用 Java 的绘画工具时,我遇到了 java.awt.GradientPaint class,它允许在绘制形状时进行渐变着色。我决定尝试一下,在 JPanel 上绘制带有红色到绿色渐变的填充矩形,并立即注意到一些奇怪的东西。

当我开始画一个矩形时,它是纯红色的,当我将它的高度或宽度增加到一定值后,就会出现渐变。在某些情况下,在渐变出现之前,矩形的高度或宽度必须很大(即 200px)。然后我注意到,我开始绘制矩形时越靠近 JPanel 的右侧或底部,在应用渐变之前矩形的尺寸必须越大。下面的 GIF 应该(希望)显示发生了什么(用于创建此示例的代码在更下方):

那么为什么会出现这种情况呢?有没有一种方法可以在绘制矩形后立即将渐变应用于矩形?

(另外,有人知道在显示最后一帧后重新启动 GIF 动画的方法吗?)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JPanel implements MouseListener, MouseMotionListener {
    int downX, downY, dragX, dragY;
    JComboBox combobox;
    String[] directions = {"Left-Right","Up-Down","Right-Left","Down-Up"};

    public Test() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        setPreferredSize(new Dimension(300,300));
        frame.add(this,BorderLayout.CENTER);

        combobox = new JComboBox(directions);
        frame.add(combobox,BorderLayout.NORTH);

        addMouseListener(this);
        addMouseMotionListener(this);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        if (combobox.getSelectedItem().equals(directions[0]))
            g2.setPaint(new GradientPaint(downX,downY,Color.red,dragX-downX,downY,Color.green));
        else if (combobox.getSelectedItem().equals(directions[1]))
            g2.setPaint(new GradientPaint(downX,downY,Color.red,downX,dragY-downY,Color.green));
        else if (combobox.getSelectedItem().equals(directions[2]))
            g2.setPaint(new GradientPaint(dragX-downX,downY,Color.red,downX,downY,Color.green));
        else if (combobox.getSelectedItem().equals(directions[3]))
            g2.setPaint(new GradientPaint(downX,dragY-downY,Color.red,downX,downY,Color.green));

        g2.fill(new Rectangle(downX,downY,dragX-downX,dragY-downY));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    @Override
    public void mousePressed(MouseEvent ev) {
        downX = ev.getX();
        downY = ev.getY();
    }

    @Override
    public void mouseDragged(MouseEvent ev) {
        dragX = ev.getX();
        dragY = ev.getY();
        repaint();
    }

    @Override public void mouseClicked(MouseEvent ev) {} 
    @Override public void mouseEntered(MouseEvent ev) {}
    @Override public void mouseExited(MouseEvent ev) {}
    @Override public void mouseReleased(MouseEvent ev) {}
    @Override public void mouseMoved(MouseEvent ev) {}
}

所以,基于 JavaDocs

GradientPaint
public GradientPaint(float x1,
    float y1,
    Color color1,
    float x2,
    float y2,
    Color color2)

Constructs a simple acyclic GradientPaint object.

Parameters:
  x1 - x coordinate of the first specified Point in user space
  y1 - y coordinate of the first specified Point in user space
  color1 - Color at the first specified Point
  x2 - x coordinate of the second specified Point in user space
  y2 - y coordinate of the second specified Point in user space

  color2 - Color at the second specified Point

(重点是我加的)

x2y2参数不是宽高,而是实际坐标

这意味着像...

g2.setPaint(new GradientPaint(downX,downY,Color.red,dragX-downX,downY,Color.green));

实际上应该是...

g2.setPaint(new GradientPaint(downX, downY, Color.red, dragX, downY, Color.green));

就个人而言,我更喜欢使用 LinearGradientPaint 之类的东西,因为它有更多选项,但只有我自己 ;)