我做了一個java繪畫應用程序,並且我製作了一個彩虹畫筆功能;然而,我想讓隨機化的顏色變成平滑的漸變。它目前只是印刷不同顏色的橢圓形,您可以注意到每個不同的橢圓形。有沒有辦法讓它漸變?Java - 製作漸變畫筆
Paint Project - CLICK HERE TO SEE PROGRAM
我的彩虹功用:
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);
}
去這裏:http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java –
你想每個橢圓形是漸變還是你想繪製的線看起來像一個漸變? –
我希望繪製的線看起來像一個漸變的顏色 – flowinwind