- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在使用 Java 的绘画工具时,我遇到了 java.awt.GradientPaint
类,它允许在绘制形状时进行渐变着色。我决定尝试一下,在 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
(重点是我加的)
x2
和y2
参数不是宽高,而是实际坐标
这意味着像...
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
这样的东西因为它有更多选项,但只有我自己 ;)
关于java - GradientPaint 工作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229100/
在 Java 中是否有使用 GradientPaint 的替代方法? 基本上,当我用纯色填充应用程序中的所有矩形时,此时我的代码速度是可以接受的。 但是,一旦我开始使用 GradientPaint,某
在使用 Java 的绘画工具时,我遇到了 java.awt.GradientPaint 类,它允许在绘制形状时进行渐变着色。我决定尝试一下,在 JPanel 上绘制带有红绿渐变的填充矩形,并立即注意到
我正在尝试绘制一个从红色到蓝色的渐变矩形。我有以下代码: g2d.setPaint(new GradientPaint(0, 0, Color.RED, 1000, 1000, Color.BLUE)
我有一个 View 对象,它是一个 jPanel,并包含其他 jPanel,而其他 jPanel 又包含 jLabels。我想在对象上绘制渐变叠加层,使其具有漂亮的时尚外观,而不是无聊的朴素外观。 到
我尝试对堆叠条形图使用 GradientPaint 来突出显示所选系列,但只有 GradientPaint 的第一个 Color 是显示。 GroupedStackedBarRenderer barR
我使用 GradientPaint 类在未修饰的 Jframe 上绘制阴影在 Windows 操作系统上一切正常。但是在 Mac OS 上,当我选择 java 7/8 时,阴影比 java 6 中的阴
我是一名优秀的程序员,十分优秀!