gpt4 book ai didi

java - 矩形渐变

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:30 27 4
gpt4 key购买 nike

我正在尝试为我的用户界面中的某些内容实现一个draw(Graphics2D g) 方法。不是swing组件,是我自己画的。我正在尝试创建像这张图片一样的渐变边框

gradient border

我怎样才能画出类似于那个的渐变?

最佳答案

你并没有真正指定你想要什么,但如果它是我认为的,那么它有点类似于我在my answer to "How to make gradient border of an image using java?"中写的.

这里的技巧是不要创建单一的渐变颜料,也不要填充单一的形状。相反,必须用渐变填充的区域分为 8 个部分:四个边和四个角。边缘充满了简单的渐变颜料。角落里充满了径向渐变颜料。然后它只是关于摆弄坐标,但是当内部形状被指定为 Rectangle2D 时,这可以通过一种相当通用的方式来完成。

但是,结果可能如下所示:

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

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

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new RectangularGradientTestPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}


class RectangularGradientTestPanel extends JPanel
{
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
Rectangle2D r = new Rectangle2D.Double(100,100,200,100);
draw(g, r, 75);

Shape rr = new RoundRectangle2D.Double(80,80,240,140,20,20);
g.setColor(Color.BLACK);
g.fill(rr);
}

@Override
public Dimension getPreferredSize()
{
if (isPreferredSizeSet())
{
return super.getPreferredSize();
}
return new Dimension(400,300);
}


private static void draw(Graphics2D g, Rectangle2D r, double s)
{
Color c0 = new Color(255,0,0);
Color c1 = new Color(255,0,0,0);

double x0 = r.getMinX();
double y0 = r.getMinY();
double x1 = r.getMaxX();
double y1 = r.getMaxY();
double w = r.getWidth();
double h = r.getHeight();

// Left
g.setPaint(new GradientPaint(
new Point2D.Double(x0, y0), c0,
new Point2D.Double(x0 - s, y0), c1));
g.fill(new Rectangle2D.Double(x0 - s, y0, s, h));

// Right
g.setPaint(new GradientPaint(
new Point2D.Double(x1, y0), c0,
new Point2D.Double(x1 + s, y0), c1));
g.fill(new Rectangle2D.Double(x1, y0, s, h));

// Top
g.setPaint(new GradientPaint(
new Point2D.Double(x0, y0), c0,
new Point2D.Double(x0, y0 - s), c1));
g.fill(new Rectangle2D.Double(x0, y0 - s, w, s));

// Bottom
g.setPaint(new GradientPaint(
new Point2D.Double(x0, y1), c0,
new Point2D.Double(x0, y1 + s), c1));
g.fill(new Rectangle2D.Double(x0, y1, w, s));

float fractions[] = new float[] { 0.0f, 1.0f };
Color colors[] = new Color[] { c0, c1 };

// Top Left
g.setPaint(new RadialGradientPaint(
new Rectangle2D.Double(x0 - s, y0 - s, s + s, s + s),
fractions, colors, CycleMethod.NO_CYCLE));
g.fill(new Rectangle2D.Double(x0 - s, y0 - s, s, s));

// Top Right
g.setPaint(new RadialGradientPaint(
new Rectangle2D.Double(x1 - s, y0 - s, s + s, s + s),
fractions, colors, CycleMethod.NO_CYCLE));
g.fill(new Rectangle2D.Double(x1, y0 - s, s, s));

// Bottom Left
g.setPaint(new RadialGradientPaint(
new Rectangle2D.Double(x0 - s, y1 - s, s + s, s + s),
fractions, colors, CycleMethod.NO_CYCLE));
g.fill(new Rectangle2D.Double(x0 - s, y1, s, s));

// Bottom Right
g.setPaint(new RadialGradientPaint(
new Rectangle2D.Double(x1 - s, y1 - s, s + s, s + s),
fractions, colors, CycleMethod.NO_CYCLE));
g.fill(new Rectangle2D.Double(x1, y1, s, s));
}

}

关于java - 矩形渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com