- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我们知道在Java中有一个名为RadialGradientPaint
的类,我们可以用它来绘制圆形的渐变。
但是我想要一个椭圆形(ellipse)的GradientPaint
。如何实现椭圆形GradientPaint
?
最佳答案
使用 AffineTransform
在绘制 RadialGradientPaint
时。这将需要转换的缩放实例。它最终可能看起来像这样:
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);
}
}
原始图像:应用程序的原始静态(乏味)“屏幕截图”。
关于java - 如何实现椭圆GradientPaint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14170213/
我有 n 个圆圈,它们必须完美地围绕着一个椭圆,如下图所示: 在这张图片中,我需要找出椭圆周围每个圆的位置,并且还能够计算出完全适合这些周围圆的椭圆。 我知道的信息是每个圆的半径(都一样),以及圆的个
如何在three.js 中创建一个椭圆? 我看过这个: Drawing an ellipse in THREE.js 但是如果有人可以提供一个工作示例会很酷。 我试过这个: ellipse = new
我将使用哪种 SKPhysicsBody 主体类型来创建椭圆物理主体? 我知道我可以用直线做一条曲线,只是让它不是一个真正的椭圆,但似乎必须有某种方法来挤压一个圆或创建一个圆? 最佳答案 Create
我有一堆二维点。您可以在左图中看到它们。它们形成了某种带有几只兔子耳朵的环。我的目标是找到大的内循环/椭圆,您可以在右侧看到它。 什么样的算法对这种情况有用。 我尝试了 RANSAC 算法的变体(取
这个问题在这里已经有了答案: Ellipsis in the middle of a text (Mac style) (16 个答案) 关闭 7 年前。
我是 XAML 新手。我遇到了这个问题,我需要将符号图标或字体图标放入椭圆形状(因此它显示为圆圈内的图标)。但是,似乎 Ellipse 的 Fill 属性只需要 ImageBrush 和 ColorB
我是 OpenCV 的新手,正在学习进行一些图像处理。作为我项目的一部分,我遇到了将椭圆形式的图像补丁变形为目标椭圆的问题。据我所知,我需要计算两个补丁之间的仿射变换,然后将此变换扭曲到目标补丁中。浏
我希望能够使用 Graphics2D 实例在 BufferedImage 上绘图,并在形状的外部 填充颜色。如果这是矩形之类的形状,那会很容易,但我需要使用的形状是圆形。 用颜色填充一个圆很容易,只需
我正在尝试在 box2D (Cocos2D) 中创建一个椭圆对象。到目前为止,我已经为此使用了 b2CircleShape,但我意识到它不会再削减它了,我必须拥有椭圆形的 body 。可能吗?我试过使
我只是想知道是否有一种方法可以像我在绘画中制作的这个例子那样用拇指制作椭圆 slider : 现在我正在使用 style 但只适用于水平的silders。这是示例代码:
本文讲述了java实现画线、矩形、椭圆、字符串功能的实例代码。分享给大家供大家参考,具体如下: ?
我一直在阅读有关将圆拟合到数据的一些方法(如 this )。我想看看这些方法如何处理真实数据并考虑使用 R 来实现这一点。我尝试在 rseek 中搜索可以帮助解决此问题的软件包,但没有找到任何有用的信
当椭圆不使用此公式1旋转时。如果value = 1-指向椭圆,如果value> 1-外部,如果value #include #include struct Pt {int x, y;
已尝试搜索,但找不到任何内容。 我正在尝试使用数组和 for 循环绘制多个 2D 椭圆,我每秒都会重新绘制框架。问题是,我每次重新绘制时只得到一个椭圆,有人可以告诉我我的代码有什么问题吗? impor
是否可以在 Altair 图表内按照图表 x 和 y 变量的测量单位绘制线条和几何形状?图表可能是多面的,形状和线条取决于每个特定面中的数据。 可重现的示例: import pandas as pd
我有两个系列及其交点。我想在图表上有一个椭圆形(椭圆形),中心位于交叉点。应根据轴单位设置椭圆半径,以显示每个轴的感兴趣区域。 Highcharts.chart('container', {
有没有办法调整 Highcharts 中轴标签的行高?有时,对于断行标签,可能会出现重叠/间距问题,如果可以降低行高,这些问题会得到缓解。 正如您在下图中较长的红色标签中所见,自定义行高会很有帮助。有
我想在 android 中画一个椭圆/圆,但我无法让它显示出来。我可以使用低效的循环和 glVertex 在 C 的 OpenGL 中(而不是在 android 中)做得很好,但是 OpenGL ES
本文实例讲述了Android编程开发之在Canvas中利用Path绘制基本图形的方法。分享给大家供大家参考,具体如下: 在Android中绘制基本的集合图形,本程序就是自定义一个View组件,程序
<ellipse> 元素可以画一个椭圆 SVG 椭圆 - <ellipse> <ellipse> 元素可以画一个椭圆 椭圆与圆很相似。不同之处在于椭圆有不同的
我是一名优秀的程序员,十分优秀!