我目前正在学习Java GUI开发,在这里我只能停留在Gradient上.
我想知道循环梯度和非循环梯度的区别吗?
最佳答案
>循环渐变
可以简单地将其作为图案在梯度区域上重复.
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class GradientPane extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
float width = 300;
float height = 50;
GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true); // Cyclic
// gradient
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
g2D.setPaint(g1); // Gradient color fill
g2D.fill(rect1); // Fill the rectangle
g2D.setPaint(Color.BLACK); // Outline in black
g2D.draw(rect1); // Fill the rectangle
g2D.draw(new Line2D.Float(p1, p2));
}
}
public class CyclicGradientPaint {
public static void main(String[] a) {
JFrame window = new JFrame("Cyclic Gradient Paint");
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new GradientPane());
window.setVisible(true);
}
}
>非循环渐变
模式不会重复出现,而是随着渐变区域的扩大而扩大.
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class GradientPane extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start
Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end
float width = 300;
float height = 50;
Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false); // Acyclic gradient
rect1.setRect(p1.x - 100, p1.y - 25, width, height);
g2D.setPaint(g2); // Gradient color fill
g2D.fill(rect1); // Fill the rectangle
g2D.setPaint(Color.BLACK); // Outline in black
g2D.draw(rect1); // Fill the rectangle
g2D.draw(new Line2D.Float(p1, p2));
}
}
public class AcyclicGradientPaint {
public static void main(String[] a) {
JFrame window = new JFrame("Acyclic Gradient Paint");
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new GradientPane());
window.setVisible(true);
}
}
Inm小程序商店
Inm小程序商店收录了最新,最热门的微信小程序和微信小游戏,是国内内容最丰富的集小程序游戏、小程序分发、小程序推广为一体的综合性小程序门户网站之一。
Vultr中文网
最低 $2.5/月 的VPS, 稳定, 可靠