[英]4 Color Gradient using Java awt
I´m trying to create a 4 color gradient to fill a rectangle. 我正在尝试创建一个4色渐变来填充矩形。 Each corner will have a diferent color.
每个角落都有不同的颜色。
The result image I´m trying to achieve is something like this: 我试图实现的结果图像是这样的:
https://www.dropbox.com/s/vu3yxbaenu2cnwg/Screenshot%202014-08-29%2012.53.43.png?dl=0 https://www.dropbox.com/s/vu3yxbaenu2cnwg/Screenshot%202014-08-29%2012.53.43.png?dl=0
There is a lot of helpful resources on how to make a 2 color gradient, but none for more than 3. 关于如何制作2色渐变有很多有用的资源,但是没有超过3个。
Something like this? 像这样的东西?
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ThreeWayGradient {
public static void main(String[] args) {
final BufferedImage image = new BufferedImage(
200, 200, BufferedImage.TYPE_INT_RGB);
Runnable r = new Runnable() {
@Override
public void run() {
Graphics2D g = image.createGraphics();
GradientPaint primary = new GradientPaint(
0f, 0f, Color.WHITE, 200f, 0f, Color.ORANGE);
GradientPaint shade = new GradientPaint(
0f, 0f, new Color(0, 0, 0, 0),
0f, 200f, new Color(0, 0, 0, 255));
g.setPaint(primary);
g.fillRect(0, 0, 200, 200);
g.setPaint(shade);
g.fillRect(0, 0, 200, 200);
JLabel l = new JLabel(new ImageIcon(image));
JOptionPane.showMessageDialog(null, l);
File f = new File(System.getProperty("user.home"),
"ThreeWayGradient.png");
try {
ImageIO.write(image, "png", f);
} catch (IOException ex) {
ex.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
}
}
..because it is prettier. 因为它更漂亮。
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ThreeWayGradient {
public static BufferedImage getThreeWayGradient(
int size,
Color primaryLeft,
Color primaryRight,
Color shadeColor) {
BufferedImage image = new BufferedImage(
size, size, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
GradientPaint primary = new GradientPaint(
0f, 0f, primaryLeft, size, 0f, primaryRight);
int rC = shadeColor.getRed();
int gC = shadeColor.getGreen();
int bC = shadeColor.getBlue();
GradientPaint shade = new GradientPaint(
0f, 0f, new Color(rC, gC, bC, 0),
0f, size, shadeColor);
g.setPaint(primary);
g.fillRect(0, 0, size, size);
g.setPaint(shade);
g.fillRect(0, 0, size, size);
g.dispose();
return image;
}
/**
* Presumed to have a layout that shows multiple components.
*/
public static void addGradient(
JPanel p, int s, Color pL, Color pR, Color sh) {
JLabel l = new JLabel(new ImageIcon(getThreeWayGradient(s, pL, pR, sh)));
p.add(l);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new GridLayout(2,4,1,1));
addGradient(gui,100,Color.YELLOW,Color.RED,Color.GREEN);
addGradient(gui,100,Color.GREEN,Color.YELLOW,Color.RED);
addGradient(gui,100,Color.RED,Color.GREEN,Color.YELLOW);
addGradient(gui,100,Color.BLUE,Color.MAGENTA,Color.PINK);
addGradient(gui,100,Color.WHITE,Color.RED,Color.BLACK);
addGradient(gui,100,Color.RED,Color.GREEN,Color.BLACK);
addGradient(gui,100,Color.BLUE,Color.PINK,Color.BLACK);
addGradient(gui,100,Color.BLUE,Color.CYAN,Color.BLACK);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
Here is one solution 这是一个解决方案
package foursidesgradient;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class FourSidesGradient{
public static void main(String[] args) {
JFrame frame = new JFrame("My frame");
frame.add(new MyLabel(200, Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private static class MyLabel extends JLabel{
private BufferedImage image0;
private BufferedImage image1;
private BufferedImage image2;
private Color noColor = new Color(255, 255, 255, 0);
private int size;
public MyLabel(int size, Color leftTop, Color rightTop, Color leftBottom, Color rightBottom){
super();
image0 = getTwoWayGradient(size, rightTop,leftBottom);
image1 = getLeftGradient(size, leftTop);
image2 = getRightGradient(size, rightBottom);
this.size = size;
this.setPreferredSize(new Dimension(size, size));
}
private BufferedImage getTwoWayGradient(int size,Color rightTop,Color leftBottom) {
image0 = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image0.createGraphics();
GradientPaint twoColorGradient = new GradientPaint(size, 0f, rightTop, 0, size, leftBottom);
g2d.setPaint(twoColorGradient);
g2d.fillRect(0, 0, size, size);
return image0;
}
private BufferedImage getLeftGradient(int size, Color RED) {
image1 = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image1.createGraphics();
float radius = size;
float[] dist = {0f, 1.0f};
Point2D center = new Point2D.Float(0, 0);
Color[] colors2 = {RED, noColor};
RadialGradientPaint two = new RadialGradientPaint(center, radius, dist, colors2);
g2d.setPaint(two);
g2d.fillRect(0, 0, size, size);
return image1;
}
private BufferedImage getRightGradient(int size, Color CYAN) {
image2 = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image2.createGraphics();
float radius = size;
float[] dist = {0f, 1.0f};
Point2D center = new Point2D.Float(size, size);
Color[] colors2 = {CYAN, noColor};
RadialGradientPaint three = new RadialGradientPaint(center, radius, dist, colors2);
g2d.setPaint(three);
g2d.fillRect(0, 0, size, size);
return image2;
}
@Override
protected void paintComponent(Graphics g){
g.drawImage(image0, 0, 0, null);
g.drawImage(image2, 0, 0, null);
g.drawImage(image1, 0, 0, null);
}
}
}
The result: 结果:
Here is another solution: 这是另一个解决方案:
package foursidesgradient;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FourSidesGradient {
public static void main(String[] args) {
JFrame frame = new JFrame("My frame");
JPanel myPanel = new JPanel();
frame.add(myPanel);
myPanel.add(new MyLabel(200, Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private static class MyLabel extends JLabel{
int size;
Color leftTop;
Color rightTop;
Color leftBottom;
Color rightBottom;
public MyLabel(int size, Color leftTop, Color rightTop, Color leftBottom, Color rightBottom){
super();
this.size = size;
this.leftTop = leftTop;
this.rightTop = rightTop;
this.leftBottom = leftBottom;
this.rightBottom = rightBottom;
this.setPreferredSize(new Dimension(size, size));
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint twoColorGradient = new GradientPaint(
size, 0f, rightTop, 0, size, leftBottom);
float radius = size-(size/4);
float[] dist = {0f, 1.0f};
Point2D center = new Point2D.Float(0f, 0f);
Color noColor = new Color(0f, 0f, 0f, 0f);
Color[] colors = {leftTop, noColor};
RadialGradientPaint thirdColor = new RadialGradientPaint(center, radius, dist, colors);
center = new Point2D.Float(size, size);
Color[] colors2 = {rightBottom, noColor};
RadialGradientPaint fourthColor = new RadialGradientPaint(center, radius, dist, colors2);
g2d.setPaint(twoColorGradient);
g2d.fillRect(0, 0, size, size);
g2d.setPaint(thirdColor);
g2d.fillRect(0, 0, size, size);
g2d.setPaint(fourthColor);
g2d.fillRect(0, 0, size, size);
}
}
}
Second result: 第二个结果:
The pictures will be a little bit different in spite of usage the same methods. 尽管使用相同的方法,图片会有点不同。
I was able to solve my problem like this. 我能够像这样解决我的问题。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.lang.Math;
public class main extends JPanel{
public void paintComponent(Graphics g){
//super.paintComponent(g);
int ancho = getWidth();
int alto = getHeight();
int r, gg, b;
double pitagorazo, hypotenuse;
Color miColor;
for(int i=0; i<800; i++){
for(int j=0; j<600; j++){
pitagorazo = Math.pow(i,2)+Math.pow(j,2);
hypotenuse = Math.sqrt(pitagorazo);
hypotenuse=hypotenuse/3;
if(hypotenuse > 255)
hypotenuse = 255;
System.out.println(hypotenuse);
r = 255-(int)hypotenuse;
pitagorazo = Math.pow((255-i),2)+Math.pow(j,2);
hypotenuse = Math.sqrt(pitagorazo);
hypotenuse=hypotenuse/3;
if(hypotenuse > 255)
hypotenuse = 255;
gg = 255-(int)hypotenuse;
pitagorazo = Math.pow(i,2)+Math.pow((255-j),2);
hypotenuse = Math.sqrt(pitagorazo);
hypotenuse=hypotenuse/3;
if(hypotenuse > 255)
hypotenuse = 255;
b = 255-(int)hypotenuse;
miColor = new Color(r,gg,b);
g.setColor(miColor);
g.fillRect(i,j,1,1);
}
}
}
public static void main(String args[]){
main panel = new main();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(800, 600);
application.setVisible(true);
}
}
Here is the result : https://www.dropbox.com/s/ptsbiz0lxehcta8/Screenshot%202014-01-30%2023.35.23.png 结果如下: https : //www.dropbox.com/s/ptsbiz0lxehcta8/Screenshot%202014-01-30%2023.35.23.png
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.