2016-10-17 20 views
0

所以我有一個小問題,我試着解決它,因爲小時。 我有一個BufferedImage,我想以流利的方式f.e.改變顏色。從紅色到白色。 我的主:如何在沒有Javafx/AWT的情況下創建顏色漸變?

public static void main(String[] args) { 
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

    for (int x = 0; x != width; x++) { 
     for (int y = 0; y != height; y++) { 
      image.setRGB(x, y, color12(x, y)); 



    try { 
     ,,, 

我的方法來改變顏色:

static int color12(int x, int y) {  
    int size = 100; 
    if (Math.abs(width/2 - x) < size/2 && Math.abs(height/2 - y) < size/2) 
     return new Color(255, 0, 0).getRGB(); 
    else 
     return new Color(200, 200, 255).getRGB(); 
} 

}

所以我的方法發揮各地,但我不能把它做完。 我最好的 「解決方案」 是這樣的:

int r = 0 ;  
    int b = 0; 
    int g = 0; 
    for (int i = 1; i < 255; i++) 
r++; 

否則返回新的顏色(R,G,B).getRGB();

任何人都可以幫助我嗎?

回答

1

我不確定你想要的梯度(例如水平,垂直或對角),但這裏是一個使用水平或垂直梯度的線性插值的例子。

import java.awt.Color; 
import java.awt.image.BufferedImage; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class ExampleFrame extends JFrame { 

    private static enum GradientOrientation { 
     HORIZONTAL, VERTICAL 
    }; 

    private static BufferedImage createGradientImg(int width, int height, Color startColor, Color endColor, GradientOrientation o) { 
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     for (int x = 0; x < width; x++) { 
      for (int y = 0; y < height; y++) { 
       int pos = o.equals(GradientOrientation.HORIZONTAL) ? x : y; 
       int size = o.equals(GradientOrientation.HORIZONTAL) ? width : height; 
       image.setRGB(x, y, getGradientRGB(startColor, endColor, pos, size)); 
      } 
     } 
     return image; 
    } 

    private static int getGradientRGB(Color startColor, Color endColor, int pos, int size) { 
     double perc = (double) pos/size; 
     int newRed = (int) (endColor.getRed() * perc + startColor.getRed() * (1 - perc)); 
     int newGreen = (int) (endColor.getGreen() * perc + startColor.getGreen() * (1 - perc)); 
     int newBlue = (int) (endColor.getBlue() * perc + startColor.getBlue() * (1 - perc)); 
     return new Color(newRed, newGreen, newBlue).getRGB(); 
    } 

    public void createAndShow() { 
     BufferedImage img1 = createGradientImg(200, 100, Color.RED, 
       Color.WHITE, GradientOrientation.HORIZONTAL); 
     BufferedImage img2 = createGradientImg(200, 100, Color.BLUE, 
       Color.YELLOW, GradientOrientation.HORIZONTAL); 
     BufferedImage img3 = createGradientImg(200, 100, Color.WHITE, 
       Color.YELLOW, GradientOrientation.VERTICAL); 
     BufferedImage img4 = createGradientImg(200, 100, Color.BLACK, 
       Color.WHITE, GradientOrientation.VERTICAL); 

     BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS); 
     getContentPane().setLayout(layout); 
     getContentPane().add(new JLabel(new ImageIcon(img1))); 
     getContentPane().add(new JLabel(new ImageIcon(img2))); 
     getContentPane().add(new JLabel(new ImageIcon(img3))); 
     getContentPane().add(new JLabel(new ImageIcon(img4))); 
     pack(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       ExampleFrame ef = new ExampleFrame(); 
       ef.createAndShow(); 
      } 
     }); 
    } 
} 
+0

但我不想使用swing或awt :)是否有可能通過編輯靜態int顏色方法來完成任務? –

+0

我不完全確定你想要達到什麼目的,但是你可以改變你的'getGradientRGB()'方法的'color12()'方法。只需將它傳遞給正確的參數即可。在我的示例中,用於創建漸變圖像的唯一類是根據原始問題的「BufferedImage」和「Color」;這兩個都是'awt'包的一部分。其餘部分純粹是爲了演示,即將其作爲示例顯示,爲了將其顯示爲GUI,您需要使用awt/Swing/JavaFX/swt /等。除非你想從頭開始建立一個庫。 –

+0

嗨。所以我想舉個例子,我的顏色以紅色開始,它從步驟(255,0,0)到(255,255,255),以流暢的方式從紅色變成白色。我的color12方法已經創建了一種顏色,但只有1種,並不多。我想要很多顏色(從紅色到白色)。我玩過主要和方法,但無法以正確的方式完成。我想我只需要在color12中添加一個for或while方法,但我不知道..也許你現在明白我的意思(我知道它很難,因爲我的英語不流利) –

相關問題