Java制作渐变画笔
我做了一个java绘画应用程序,我做了一个彩虹画笔函数;但是,我想把随机的颜色变成平滑的渐变。它目前只是打印不同颜色的椭圆形,你可以注意到每个不同的椭圆形。有没有办法把它变成梯度 我的彩虹功能:Java制作渐变画笔,java,Java,我做了一个java绘画应用程序,我做了一个彩虹画笔函数;但是,我想把随机的颜色变成平滑的渐变。它目前只是打印不同颜色的椭圆形,你可以注意到每个不同的椭圆形。有没有办法把它变成梯度 我的彩虹功能: public void rainbow() { Random generator = new Random(); int r = generator.nextInt(256); int g = generator.nextInt(256); int b = genera
public void rainbow() {
Random generator = new Random();
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
Color color = new Color(r, g, b);
g2.setPaint(color);
}
我的鼠标侦听器:
public DrawArea() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// save coord x,y when mouse is pressed
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// coord x,y when drag mouse
currentX = e.getX();
currentY = e.getY();
if (g2 != null) {
// draw oval if g2 context not null
g2.drawOval(oldX, oldY, 40, 40);
g2.fillOval(oldX, oldY, 40, 40);
// refresh draw area to repaint
repaint();
// store current coords x,y as olds x,y
oldX = currentX;
oldY = currentY;
}
}
});
}
油漆成分:
public void paintComponent(Graphics g) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
g2 = (Graphics2D) image.getGraphics();
clear();
}
// enable antialiasing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, null);
}
我假设你每次都调用彩虹函数,这就是为什么你会得到这样的效果 要创建具有渐变的错觉,r、g、b值的变化速度必须足够慢,才能达到所需的效果 实现这一点的一种方法是存储您希望LERP(请参阅:)的值 解释: 1.在调用“随机化”函数时,跟踪您要选择的颜色、当前颜色以及原始颜色 2.当我们第一次调用彩虹函数时,当前颜色将设置为随机颜色 3.对于每个勾号,如果当前颜色不是目标颜色,我们将增加原始颜色和目标颜色之间差异的1/5 4.恒定的增量因子为0.2意味着我们需要5个刻度才能从一种颜色到另一种颜色,该变量越小,从原始颜色到目标颜色的时间就越长 5.如果我们已达到目标颜色,我们将选择另一种新的目标颜色
*未经测试,但我认为您可以找出其余的我假定您每次都在调用彩虹函数,这就是为什么您会得到这样的效果 要创建具有渐变的错觉,r、g、b值的变化速度必须足够慢,才能达到所需的效果 实现这一点的一种方法是存储您希望LERP(请参阅:)的值 解释: 1.在调用“随机化”函数时,跟踪您要选择的颜色、当前颜色以及原始颜色 2.当我们第一次调用彩虹函数时,当前颜色将设置为随机颜色 3.对于每个勾号,如果当前颜色不是目标颜色,我们将增加原始颜色和目标颜色之间差异的1/5 4.恒定的增量因子为0.2意味着我们需要5个刻度才能从一种颜色到另一种颜色,该变量越小,从原始颜色到目标颜色的时间就越长 5.如果我们已达到目标颜色,我们将选择另一种新的目标颜色
*没有测试,但我想你可以找出剩下的到这里:你希望每个椭圆都是一个梯度,还是你希望画的线看起来像一个梯度?我希望画的线看起来像一个颜色梯度Go这里:你希望每个椭圆都是一个梯度,还是你希望画的线看起来像一个梯度?我希望画的线看起来像一个梯度颜色梯度
//declare a variable to store the destinated Color;
public Color colorToBeLerped = null;
public Color currentColor = null;
public Color originalColor = null;
public final float deltaFactor = 0.2;
public void rainbow() {
if(currentColor==null||currentColor.equals(colorToBeLerped)){
Random generator = new Random();
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
colorToBeLerped = new Color(r, g, b);
originalColor = colorToBeLerped;
}
if(currentColor==null){
currentColor=colorToBeLerped;
}else{
//using the Color constructor that accepts float arguments, divide 255
currentColor= new Color((colorToBeLerped.getRed()-originalColor.getRed()*deltaFactor)/255,
(colorToBeLerped.getGreen()-originalColor.getGreen()*deltaFactor)/255,
(colorToBeLerped.getBlue()-originalColor.getBlue()*deltaFactor)/255);
}
g2.setPaint(currentColor);
}