我试图在我的用户界面中为某些内容实现draw(Graphics2D g)
方法。它不是摆动组件,我自己在做绘图。我正在尝试创建一个像这个图像的渐变边框
如何绘制类似于那个的渐变?
答案 0 :(得分:3)
你并没有指定你想要的东西,但如果它是我想的它是什么,那么它有点类似于我在{{3中所写的那样的东西}}
这里的技巧是不创建单个渐变绘画,并且不填充单个形状。相反,必须用渐变填充的区域分为8个部分:四个边缘和四个角落。边缘填充简单的渐变涂料。角落充满径向渐变涂料。然后它只是摆弄坐标,但当内部形状以Rectangle2D
给出时,这可以通过相当通用的方式完成。
但是,结果可能会如下所示:
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));
}
}